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