60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
# coding: utf-8
|
|
|
|
# python
|
|
import os
|
|
import random
|
|
import string
|
|
|
|
# django
|
|
from django.db import models
|
|
from django.conf import settings
|
|
|
|
# 3rd party
|
|
from ckeditor.fields import RichTextField
|
|
|
|
|
|
#
|
|
# Work project
|
|
#
|
|
class Group(models.Model):
|
|
name = models.CharField(max_length=128)
|
|
slug = models.SlugField(max_length=128)
|
|
order = models.IntegerField(default=1)
|
|
|
|
def __unicode__(self):
|
|
return self.name
|
|
|
|
|
|
class Project(models.Model):
|
|
group = models.ForeignKey(Group, related_name='projects')
|
|
title = models.CharField(max_length=128)
|
|
slug = models.SlugField(max_length=128)
|
|
date = models.DateTimeField()
|
|
company = models.CharField(max_length=128, null=True, blank=True)
|
|
role = models.CharField(max_length=128)
|
|
description = RichTextField()
|
|
visible = models.BooleanField(default=True)
|
|
stack = models.CharField(max_length=256, null=True, blank=True,
|
|
default=None)
|
|
url = models.CharField(max_length=256, null=True, blank=True, default=None)
|
|
|
|
def __unicode__(self):
|
|
return self.title
|
|
|
|
class Meta:
|
|
ordering = ['-date']
|
|
|
|
|
|
def random_string(size=6, chars=string.ascii_lowercase + string.digits):
|
|
return ''.join(random.choice(chars) for _ in range(size))
|
|
|
|
|
|
def project_image_upload_to(ins, filename):
|
|
name, ext = os.path.splitext(filename)
|
|
return 'projects/{}_{}{}'.format(ins.project.slug, random_string(), ext)
|
|
|
|
|
|
class ProjectImage(models.Model):
|
|
project = models.ForeignKey(Project, related_name='images')
|
|
image = models.ImageField(upload_to=project_image_upload_to)
|