You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
5.1 KiB
142 lines
5.1 KiB
# -*- coding: utf-8 -*-
|
|
from django.db import models
|
|
from hvad.models import TranslatableModel, TranslatedFields
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.contenttypes import generic
|
|
from django.db.models.signals import post_delete
|
|
from django.dispatch.dispatcher import receiver
|
|
from django.utils.translation import ugettext_lazy as _
|
|
#functions
|
|
from functions.custom_fields import EnumField
|
|
#python
|
|
import os
|
|
|
|
FILE_TYPES = ('PDF', 'DOC', 'TXT', 'OTHER')
|
|
IMG_TYPES = ('JPG', 'BMP', 'PNG', 'GIF',)
|
|
|
|
PURPOSES = (('photo', _(u'Фото')),
|
|
('flat', _(u'Флаг')),
|
|
('logo',_(u'Лого')),
|
|
('map',_(u'Карта')),
|
|
('scheme teritory',_(u'Схема територии')),
|
|
('diplom',_(u'Дипломы')),
|
|
('preview',_(u'Превью')),
|
|
('preview2',_(u'Превью')),
|
|
)
|
|
|
|
|
|
class FileModel(TranslatableModel):
|
|
"""
|
|
Create FileModel model
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
Uses ContentType for connection FileModel with another models
|
|
content_type = model which linked FileModel object
|
|
object_id = specific object of model which linked FileModel object
|
|
|
|
"""
|
|
|
|
def get_upload_path(instance, filename):
|
|
"""
|
|
Returns path to file_path
|
|
|
|
if type of file image returns 'media/imgs/' else 'media/files/'
|
|
"""
|
|
type = filename.split('.')[-1]
|
|
if type.upper() in IMG_TYPES:
|
|
return 'imgs/%s' % filename
|
|
else:
|
|
return 'files/%s' % filename
|
|
|
|
content_type = models.ForeignKey(ContentType, null=True) #limit_choices_to={'model__in': ('Country', 'City')}
|
|
object_id = models.PositiveIntegerField(blank=True, null=True)
|
|
object = generic.GenericForeignKey(content_type, object_id)
|
|
|
|
file_path = models.FileField(upload_to=get_upload_path)
|
|
# file_type and purposes uses EnumField for creating Enum type field in Mysql database
|
|
file_type = EnumField(values=FILE_TYPES+IMG_TYPES, blank=True)
|
|
purpose = EnumField(values=[item1 for item1, item2 in PURPOSES])
|
|
|
|
img_width = models.PositiveIntegerField(blank=True, null=True)
|
|
img_height = models.PositiveIntegerField(blank=True, null=True)
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
# translations is translated fields
|
|
translations = TranslatedFields(
|
|
file_name = models.CharField(max_length=50, blank=True),
|
|
description = models.TextField(blank=True),
|
|
)
|
|
|
|
|
|
def __unicode__(self):
|
|
return str(self.file_path)
|
|
|
|
|
|
@receiver(post_delete, sender=FileModel)
|
|
def file_model_delete(sender, **kwargs):
|
|
"""
|
|
delete file from file system after deleting object
|
|
"""
|
|
file = kwargs['instance']
|
|
storage, path = file.file_path.storage, file.file_path.path
|
|
storage.delete(path)
|
|
|
|
|
|
class TmpFile(TranslatableModel):
|
|
"""
|
|
Create TmpFile model which uses for loading files in new models
|
|
|
|
Uses hvad.TranslatableModel which is child of django.db.models class
|
|
|
|
"""
|
|
file_path = models.FileField(upload_to='tmp_files/')
|
|
# file_type and purposes uses EnumField for creating Enum type field in Mysql database
|
|
file_type = EnumField(values=FILE_TYPES+IMG_TYPES, blank=True)
|
|
purpose = EnumField(values=[item1 for item1, item2 in PURPOSES])
|
|
|
|
img_width = models.PositiveIntegerField(blank=True, null=True)
|
|
img_height = models.PositiveIntegerField(blank=True, null=True)
|
|
# key uses for checking keys from new objects.
|
|
# (if keys the same as in form move Tmpfile to FileModel)
|
|
key = models.CharField(max_length=255, blank=True)
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
#translations is translated fields
|
|
translations = TranslatedFields(
|
|
file_name = models.CharField(max_length=50, blank=True),
|
|
description = models.TextField(blank=True),
|
|
)
|
|
|
|
def __unicode__(self):
|
|
return str(self.file_path)
|
|
|
|
|
|
|
|
class UserMark(models.Model):
|
|
user = models.ForeignKey('accounts.User')
|
|
top = models.PositiveSmallIntegerField()
|
|
left = models.PositiveSmallIntegerField()
|
|
height = models.PositiveSmallIntegerField()
|
|
width = models.PositiveSmallIntegerField()
|
|
|
|
class Photo(TranslatableModel):
|
|
content_type = models.ForeignKey(ContentType, null=True)
|
|
object_id = models.PositiveIntegerField(blank=True, null=True)
|
|
object = generic.GenericForeignKey(content_type, object_id)
|
|
|
|
file_path = models.ImageField(upload_to='photos/')
|
|
width = models.PositiveIntegerField(blank=True, null=True)
|
|
height = models.PositiveIntegerField(blank=True, null=True)
|
|
order = models.PositiveSmallIntegerField(default=0)
|
|
view_count = models.PositiveIntegerField(_('view count'), default=0)
|
|
file_type = EnumField(values=FILE_TYPES+IMG_TYPES, blank=True)
|
|
|
|
users = models.ManyToManyField(UserMark, null=True)
|
|
translations = TranslatedFields(
|
|
name = models.CharField(max_length=255, blank=True),
|
|
description = models.TextField(blank=True)
|
|
)
|
|
#
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|