Show More
@@ -35,9 +35,42 b' RANK_MODERATOR = 10' | |||
|
35 | 35 | RANK_USER = 100 |
|
36 | 36 | |
|
37 | 37 | |
|
38 | class User(models.Model): | |
|
39 | ||
|
40 | user_id = models.CharField(max_length=50) | |
|
41 | rank = models.IntegerField() | |
|
42 | ||
|
43 | def save_setting(self, name, value): | |
|
44 | setting, created = Setting.objects.get_or_create(name=name, user=self) | |
|
45 | setting.value = value | |
|
46 | setting.save() | |
|
47 | ||
|
48 | return setting | |
|
49 | ||
|
50 | def get_setting(self, name): | |
|
51 | settings = Setting.objects.filter(name=name, user=self) | |
|
52 | if len(settings) > 0: | |
|
53 | setting = settings[0] | |
|
54 | else: | |
|
55 | setting = None | |
|
56 | ||
|
57 | if setting: | |
|
58 | setting_value = setting.value | |
|
59 | else: | |
|
60 | setting_value = None | |
|
61 | ||
|
62 | return setting_value | |
|
63 | ||
|
64 | def is_moderator(self): | |
|
65 | return RANK_MODERATOR >= self.rank | |
|
66 | ||
|
67 | def __unicode__(self): | |
|
68 | return self.user_id | |
|
69 | ||
|
70 | ||
|
38 | 71 | class PostManager(models.Manager): |
|
39 | 72 | def create_post(self, title, text, image=None, parent_id=NO_PARENT, |
|
40 | ip=NO_IP, tags=None): | |
|
73 | ip=NO_IP, tags=None, user=None): | |
|
41 | 74 | post = self.create(title=title, |
|
42 | 75 | text=text, |
|
43 | 76 | pub_time=timezone.now(), |
@@ -45,7 +78,8 b' class PostManager(models.Manager):' | |||
|
45 | 78 | image=image, |
|
46 | 79 | poster_ip=ip, |
|
47 | 80 | poster_user_agent=UNKNOWN_UA, |
|
48 |
last_edit_time=timezone.now() |
|
|
81 | last_edit_time=timezone.now(), | |
|
82 | user=user) | |
|
49 | 83 | |
|
50 | 84 | if tags: |
|
51 | 85 | map(post.tags.add, tags) |
@@ -224,6 +258,7 b' class Post(models.Model):' | |||
|
224 | 258 | parent = models.BigIntegerField() |
|
225 | 259 | tags = models.ManyToManyField(Tag) |
|
226 | 260 | last_edit_time = models.DateTimeField() |
|
261 | user = models.ForeignKey(User, null=True, default=None) | |
|
227 | 262 | |
|
228 | 263 | def __unicode__(self): |
|
229 | 264 | return '#' + str(self.id) + ' ' + self.title + ' (' + self.text.raw + \ |
@@ -293,39 +328,6 b' class Admin(models.Model):' | |||
|
293 | 328 | return self.name + '/' + '*' * len(self.password) |
|
294 | 329 | |
|
295 | 330 | |
|
296 | class User(models.Model): | |
|
297 | ||
|
298 | user_id = models.CharField(max_length=50) | |
|
299 | rank = models.IntegerField() | |
|
300 | ||
|
301 | def save_setting(self, name, value): | |
|
302 | setting, created = Setting.objects.get_or_create(name=name, user=self) | |
|
303 | setting.value = value | |
|
304 | setting.save() | |
|
305 | ||
|
306 | return setting | |
|
307 | ||
|
308 | def get_setting(self, name): | |
|
309 | settings = Setting.objects.filter(name=name, user=self) | |
|
310 | if len(settings) > 0: | |
|
311 | setting = settings[0] | |
|
312 | else: | |
|
313 | setting = None | |
|
314 | ||
|
315 | if setting: | |
|
316 | setting_value = setting.value | |
|
317 | else: | |
|
318 | setting_value = None | |
|
319 | ||
|
320 | return setting_value | |
|
321 | ||
|
322 | def is_moderator(self): | |
|
323 | return RANK_MODERATOR >= self.rank | |
|
324 | ||
|
325 | def __unicode__(self): | |
|
326 | return self.user_id | |
|
327 | ||
|
328 | ||
|
329 | 331 | class Setting(models.Model): |
|
330 | 332 | |
|
331 | 333 | name = models.CharField(max_length=50) |
@@ -9,6 +9,7 b' from PIL import Image' | |||
|
9 | 9 | from django.core.files.base import ContentFile |
|
10 | 10 | import cStringIO |
|
11 | 11 | |
|
12 | ||
|
12 | 13 | def generate_thumb(img, thumb_size, format): |
|
13 | 14 | """ |
|
14 | 15 | Generates a thumbnail image and returns a ContentFile object with the thumbnail |
@@ -22,10 +23,10 b' def generate_thumb(img, thumb_size, form' | |||
|
22 | 23 | format format of the original image ('jpeg','gif','png',...) |
|
23 | 24 | (this format will be used for the generated thumbnail, too) |
|
24 | 25 | """ |
|
25 | ||
|
26 | ||
|
26 | 27 | img.seek(0) # see http://code.djangoproject.com/ticket/8222 for details |
|
27 | 28 | image = Image.open(img) |
|
28 | ||
|
29 | ||
|
29 | 30 | # get size |
|
30 | 31 | thumb_w, thumb_h = thumb_size |
|
31 | 32 | # If you want to generate a square thumbnail |
@@ -33,12 +34,13 b' def generate_thumb(img, thumb_size, form' | |||
|
33 | 34 | # quad |
|
34 | 35 | xsize, ysize = image.size |
|
35 | 36 | # get minimum size |
|
36 | minsize = min(xsize,ysize) | |
|
37 | minsize = min(xsize, ysize) | |
|
37 | 38 | # largest square possible in the image |
|
38 | xnewsize = (xsize-minsize)/2 | |
|
39 | ynewsize = (ysize-minsize)/2 | |
|
39 | xnewsize = (xsize - minsize) / 2 | |
|
40 | ynewsize = (ysize - minsize) / 2 | |
|
40 | 41 | # crop it |
|
41 | image2 = image.crop((xnewsize, ynewsize, xsize-xnewsize, ysize-ynewsize)) | |
|
42 | image2 = image.crop( | |
|
43 | (xnewsize, ynewsize, xsize - xnewsize, ysize - ynewsize)) | |
|
42 | 44 | # load is necessary after crop |
|
43 | 45 | image2.load() |
|
44 | 46 | # thumbnail of the cropped image (with ANTIALIAS to make it look better) |
@@ -47,66 +49,70 b' def generate_thumb(img, thumb_size, form' | |||
|
47 | 49 | # not quad |
|
48 | 50 | image2 = image |
|
49 | 51 | image2.thumbnail(thumb_size, Image.ANTIALIAS) |
|
50 | ||
|
52 | ||
|
51 | 53 | io = cStringIO.StringIO() |
|
52 | 54 | # PNG and GIF are the same, JPG is JPEG |
|
53 | if format.upper()=='JPG': | |
|
55 | if format.upper() == 'JPG': | |
|
54 | 56 | format = 'JPEG' |
|
55 | ||
|
57 | ||
|
56 | 58 | image2.save(io, format) |
|
57 |
return ContentFile(io.getvalue()) |
|
|
59 | return ContentFile(io.getvalue()) | |
|
60 | ||
|
58 | 61 | |
|
59 | 62 | class ImageWithThumbsFieldFile(ImageFieldFile): |
|
60 | 63 | """ |
|
61 | 64 | See ImageWithThumbsField for usage example |
|
62 | 65 | """ |
|
66 | ||
|
63 | 67 | def __init__(self, *args, **kwargs): |
|
64 | 68 | super(ImageWithThumbsFieldFile, self).__init__(*args, **kwargs) |
|
65 | 69 | self.sizes = self.field.sizes |
|
66 | ||
|
70 | ||
|
67 | 71 | if self.sizes: |
|
68 | 72 | def get_size(self, size): |
|
69 | 73 | if not self: |
|
70 | 74 | return '' |
|
71 | 75 | else: |
|
72 | split = self.url.rsplit('.',1) | |
|
73 | thumb_url = '%s.%sx%s.%s' % (split[0],w,h,split[1]) | |
|
76 | split = self.url.rsplit('.', 1) | |
|
77 | thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1]) | |
|
74 | 78 | return thumb_url |
|
75 | ||
|
79 | ||
|
76 | 80 | for size in self.sizes: |
|
77 | (w,h) = size | |
|
78 | setattr(self, 'url_%sx%s' % (w,h), get_size(self, size)) | |
|
79 | ||
|
81 | (w, h) = size | |
|
82 | setattr(self, 'url_%sx%s' % (w, h), get_size(self, size)) | |
|
83 | ||
|
80 | 84 | def save(self, name, content, save=True): |
|
81 | 85 | super(ImageWithThumbsFieldFile, self).save(name, content, save) |
|
82 | ||
|
86 | ||
|
83 | 87 | if self.sizes: |
|
84 | 88 | for size in self.sizes: |
|
85 | (w,h) = size | |
|
86 | split = self.name.rsplit('.',1) | |
|
87 | thumb_name = '%s.%sx%s.%s' % (split[0],w,h,split[1]) | |
|
88 | ||
|
89 | (w, h) = size | |
|
90 | split = self.name.rsplit('.', 1) | |
|
91 | thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1]) | |
|
92 | ||
|
89 | 93 | # you can use another thumbnailing function if you like |
|
90 | 94 | thumb_content = generate_thumb(content, size, split[1]) |
|
91 | ||
|
92 |
thumb_name_ = self.storage.save(thumb_name, thumb_content) |
|
|
93 | ||
|
95 | ||
|
96 | thumb_name_ = self.storage.save(thumb_name, thumb_content) | |
|
97 | ||
|
94 | 98 | if not thumb_name == thumb_name_: |
|
95 |
raise ValueError( |
|
|
96 | ||
|
99 | raise ValueError( | |
|
100 | 'There is already a file named %s' % thumb_name) | |
|
101 | ||
|
97 | 102 | def delete(self, save=True): |
|
98 | name=self.name | |
|
103 | name = self.name | |
|
99 | 104 | super(ImageWithThumbsFieldFile, self).delete(save) |
|
100 | 105 | if self.sizes: |
|
101 | 106 | for size in self.sizes: |
|
102 | (w,h) = size | |
|
103 | split = name.rsplit('.',1) | |
|
104 | thumb_name = '%s.%sx%s.%s' % (split[0],w,h,split[1]) | |
|
107 | (w, h) = size | |
|
108 | split = name.rsplit('.', 1) | |
|
109 | thumb_name = '%s.%sx%s.%s' % (split[0], w, h, split[1]) | |
|
105 | 110 | try: |
|
106 | 111 | self.storage.delete(thumb_name) |
|
107 | 112 | except: |
|
108 | 113 | pass |
|
109 | ||
|
114 | ||
|
115 | ||
|
110 | 116 | class ImageWithThumbsField(ImageField): |
|
111 | 117 | attr_class = ImageWithThumbsFieldFile |
|
112 | 118 | """ |
@@ -151,10 +157,17 b' class ImageWithThumbsField(ImageField):' | |||
|
151 | 157 | Add method to regenerate thubmnails |
|
152 | 158 | |
|
153 | 159 | """ |
|
154 | def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, sizes=None, **kwargs): | |
|
155 | self.verbose_name=verbose_name | |
|
156 | self.name=name | |
|
157 | self.width_field=width_field | |
|
158 | self.height_field=height_field | |
|
160 | ||
|
161 | def __init__(self, verbose_name=None, name=None, width_field=None, | |
|
162 | height_field=None, sizes=None, **kwargs): | |
|
163 | self.verbose_name = verbose_name | |
|
164 | self.name = name | |
|
165 | self.width_field = width_field | |
|
166 | self.height_field = height_field | |
|
159 | 167 | self.sizes = sizes |
|
160 | super(ImageField, self).__init__(**kwargs) No newline at end of file | |
|
168 | super(ImageField, self).__init__(**kwargs) | |
|
169 | ||
|
170 | ||
|
171 | from south.modelsinspector import add_introspection_rules | |
|
172 | ||
|
173 | add_introspection_rules([], ["^boards\.thumbs\.ImageWithThumbsField"]) No newline at end of file |
@@ -140,6 +140,7 b' INSTALLED_APPS = (' | |||
|
140 | 140 | 'django_cleanup', |
|
141 | 141 | 'boards', |
|
142 | 142 | 'captcha', |
|
143 | 'south', | |
|
143 | 144 | ) |
|
144 | 145 | |
|
145 | 146 | # TODO: NEED DESIGN FIXES |
@@ -187,13 +188,14 b" SITE_NAME = 'Neboard'" | |||
|
187 | 188 | |
|
188 | 189 | THEMES = [ |
|
189 | 190 | ('md', 'Mystic Dark'), |
|
190 |
('sw', 'Snow White') |
|
|
191 | ('sw', 'Snow White') | |
|
192 | ] | |
|
191 | 193 | |
|
192 | 194 | DEFAULT_THEME = 'md' |
|
193 | 195 | |
|
194 | 196 | POPULAR_TAGS = 10 |
|
195 | 197 | LAST_REPLIES_COUNT = 3 |
|
196 | 198 | |
|
197 |
ENABLE_CAPTCHA = |
|
|
199 | ENABLE_CAPTCHA = False | |
|
198 | 200 | # if user tries to post before CAPTCHA_DEFAULT_SAFE_TIME. Captcha will be shown |
|
199 | 201 | CAPTCHA_DEFAULT_SAFE_TIME = 30 # seconds |
General Comments 0
You need to be logged in to leave comments.
Login now