1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.db import IntegrityError
from datetime import datetime
from settings import MEDIA_ROOT, MEDIA_URL
from django.core.files.storage import FileSystemStorage
class ImageManager(models.Manager):
"""We overwrite the defaults manager to make sure that the create function
checks for validity.
We also include some convenience functions here
"""
def has_image(self, image_name):
return bool(self.filter(name=image_name).count())
def create(self, **keyw):
"""Makes sure that no image/revision pair is already in the
database."""
if 'name' not in keyw or 'revision' not in keyw:
raise IntegrityError('needs name and revision as keywords')
if self.filter(name=keyw['name'], revision=keyw['revision']).count():
raise Image.AlreadyExisting()
return super(ImageManager, self).create(**keyw)
def create_and_save_image(self, user, image, content_type, object_id):
# Use Django's get_valid_name() to get a safe filename
storage = FileSystemStorage()
safe_filename = storage.get_valid_name(image.name)
im = self.create(content_type=content_type, object_id=object_id,
user=user, revision=1, name=image.name)
path = '%swlimages/%s' % (MEDIA_ROOT, safe_filename)
destination = open(path, 'wb')
for chunk in image.chunks():
destination.write(chunk)
im.image = 'wlimages/%s' % (safe_filename)
im.save()
class Image(models.Model):
class AlreadyExisting(IntegrityError):
def __str__(self):
return 'The combination of image/revision is already in the database'
"""
TODO
"""
# Generic Foreign Key Fields
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
name = models.CharField(max_length=100)
revision = models.PositiveIntegerField()
# User Field
user = models.ForeignKey(User)
# Date Fields
date_submitted = models.DateTimeField(
_('date/time submitted'), default=datetime.now)
image = models.ImageField(upload_to='wlimages/')
objects = ImageManager()
def __unicode__(self):
return self.name
def get_content_object(self):
"""
taken from threadedcomments:
Wrapper around the GenericForeignKey due to compatibility reasons
and due to ``list_display`` limitations.
"""
return self.content_object
class Meta:
ordering = ('-date_submitted',)
verbose_name = _('Image')
verbose_name_plural = _('Images')
get_latest_by = 'date_submitted'
|