2
2
from django .models import auth , core
3
3
4
4
class Comment (meta .Model ):
5
- db_table = 'comments'
6
- fields = (
7
- meta .ForeignKey (auth .User , raw_id_admin = True ),
8
- meta .ForeignKey (core .ContentType , name = 'content_type_id' , rel_name = 'content_type' ),
9
- meta .IntegerField ('object_id' , 'object ID' ),
10
- meta .CharField ('headline' , 'headline' , maxlength = 255 , blank = True ),
11
- meta .TextField ('comment' , 'comment' , maxlength = 3000 ),
12
- meta .PositiveSmallIntegerField ('rating1' , 'rating #1' , blank = True , null = True ),
13
- meta .PositiveSmallIntegerField ('rating2' , 'rating #2' , blank = True , null = True ),
14
- meta .PositiveSmallIntegerField ('rating3' , 'rating #3' , blank = True , null = True ),
15
- meta .PositiveSmallIntegerField ('rating4' , 'rating #4' , blank = True , null = True ),
16
- meta .PositiveSmallIntegerField ('rating5' , 'rating #5' , blank = True , null = True ),
17
- meta .PositiveSmallIntegerField ('rating6' , 'rating #6' , blank = True , null = True ),
18
- meta .PositiveSmallIntegerField ('rating7' , 'rating #7' , blank = True , null = True ),
19
- meta .PositiveSmallIntegerField ('rating8' , 'rating #8' , blank = True , null = True ),
20
- # This field designates whether to use this row's ratings in
21
- # aggregate functions (summaries). We need this because people are
22
- # allowed to post multiple review on the same thing, but the system
23
- # will only use the latest one (with valid_rating=True) in tallying
24
- # the reviews.
25
- meta .BooleanField ('valid_rating' , 'is valid rating' ),
26
- meta .DateTimeField ('submit_date' , 'date/time submitted' , auto_now_add = True ),
27
- meta .BooleanField ('is_public' , 'is public' ),
28
- meta .IPAddressField ('ip_address' , 'IP address' , blank = True , null = True ),
29
- meta .BooleanField ('is_removed' , 'is removed' ,
30
- help_text = 'Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.' ),
31
- meta .ForeignKey (core .Site ),
32
- )
33
- module_constants = {
34
- # min. and max. allowed dimensions for photo resizing (in pixels)
35
- 'MIN_PHOTO_DIMENSION' : 5 ,
36
- 'MAX_PHOTO_DIMENSION' : 1000 ,
37
-
38
- # option codes for comment-form hidden fields
39
- 'PHOTOS_REQUIRED' : 'pr' ,
40
- 'PHOTOS_OPTIONAL' : 'pa' ,
41
- 'RATINGS_REQUIRED' : 'rr' ,
42
- 'RATINGS_OPTIONAL' : 'ra' ,
43
- 'IS_PUBLIC' : 'ip' ,
44
- }
45
- ordering = ('-submit_date' ,)
46
- admin = meta .Admin (
47
- fields = (
48
- (None , {'fields' : ('content_type_id' , 'object_id' , 'site_id' )}),
49
- ('Content' , {'fields' : ('user_id' , 'headline' , 'comment' )}),
50
- ('Ratings' , {'fields' : ('rating1' , 'rating2' , 'rating3' , 'rating4' , 'rating5' , 'rating6' , 'rating7' , 'rating8' , 'valid_rating' )}),
51
- ('Meta' , {'fields' : ('is_public' , 'is_removed' , 'ip_address' )}),
52
- ),
53
- list_display = ('user_id' , 'submit_date' , 'content_type_id' , 'get_content_object' ),
54
- list_filter = ('submit_date' ,),
55
- date_hierarchy = 'submit_date' ,
56
- search_fields = ('comment' , 'user__username' ),
57
- )
5
+ user = meta .ForeignKey (auth .User , raw_id_admin = True )
6
+ content_type = meta .ForeignKey (core .ContentType )
7
+ object_id = meta .IntegerField ('object ID' )
8
+ headline = meta .CharField (maxlength = 255 , blank = True )
9
+ comment = meta .TextField (maxlength = 3000 )
10
+ rating1 = meta .PositiveSmallIntegerField ('rating #1' , blank = True , null = True )
11
+ rating2 = meta .PositiveSmallIntegerField ('rating #2' , blank = True , null = True )
12
+ rating3 = meta .PositiveSmallIntegerField ('rating #3' , blank = True , null = True )
13
+ rating4 = meta .PositiveSmallIntegerField ('rating #4' , blank = True , null = True )
14
+ rating5 = meta .PositiveSmallIntegerField ('rating #5' , blank = True , null = True )
15
+ rating6 = meta .PositiveSmallIntegerField ('rating #6' , blank = True , null = True )
16
+ rating7 = meta .PositiveSmallIntegerField ('rating #7' , blank = True , null = True )
17
+ rating8 = meta .PositiveSmallIntegerField ('rating #8' , blank = True , null = True )
18
+ # This field designates whether to use this row's ratings in aggregate
19
+ # functions (summaries). We need this because people are allowed to post
20
+ # multiple reviews on the same thing, but the system will only use the
21
+ # latest one (with valid_rating=True) in tallying the reviews.
22
+ valid_rating = meta .BooleanField ('is valid rating' )
23
+ submit_date = meta .DateTimeField ('date/time submitted' , auto_now_add = True )
24
+ is_public = meta .BooleanField ()
25
+ ip_address = meta .IPAddressField ('IP address' , blank = True , null = True )
26
+ is_removed = meta .BooleanField (help_text = 'Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.' )
27
+ site = meta .ForeignKey (core .Site )
28
+ class META :
29
+ db_table = 'comments'
30
+ module_constants = {
31
+ # min. and max. allowed dimensions for photo resizing (in pixels)
32
+ 'MIN_PHOTO_DIMENSION' : 5 ,
33
+ 'MAX_PHOTO_DIMENSION' : 1000 ,
34
+
35
+ # option codes for comment-form hidden fields
36
+ 'PHOTOS_REQUIRED' : 'pr' ,
37
+ 'PHOTOS_OPTIONAL' : 'pa' ,
38
+ 'RATINGS_REQUIRED' : 'rr' ,
39
+ 'RATINGS_OPTIONAL' : 'ra' ,
40
+ 'IS_PUBLIC' : 'ip' ,
41
+ }
42
+ ordering = ('-submit_date' ,)
43
+ admin = meta .Admin (
44
+ fields = (
45
+ (None , {'fields' : ('content_type' , 'object_id' , 'site' )}),
46
+ ('Content' , {'fields' : ('user' , 'headline' , 'comment' )}),
47
+ ('Ratings' , {'fields' : ('rating1' , 'rating2' , 'rating3' , 'rating4' , 'rating5' , 'rating6' , 'rating7' , 'rating8' , 'valid_rating' )}),
48
+ ('Meta' , {'fields' : ('is_public' , 'is_removed' , 'ip_address' )}),
49
+ ),
50
+ list_display = ('user' , 'submit_date' , 'content_type' , 'get_content_object' ),
51
+ list_filter = ('submit_date' ,),
52
+ date_hierarchy = 'submit_date' ,
53
+ search_fields = ('comment' , 'user__username' ),
54
+ )
58
55
59
56
def __repr__ (self ):
60
57
return "%s: %s..." % (self .get_user ().username , self .comment [:100 ])
@@ -156,32 +153,31 @@ def _module_user_is_moderator(user):
156
153
return False
157
154
158
155
class FreeComment (meta .Model ):
159
- "A FreeComment is a comment by a non-registered user"
160
- db_table = 'comments_free'
161
- fields = (
162
- meta .ForeignKey (core .ContentType , name = 'content_type_id' , rel_name = 'content_type' ),
163
- meta .IntegerField ('object_id' , 'object ID' ),
164
- meta .TextField ('comment' , 'comment' , maxlength = 3000 ),
165
- meta .CharField ('person_name' , "person's name" , maxlength = 50 ),
166
- meta .DateTimeField ('submit_date' , 'date/time submitted' , auto_now_add = True ),
167
- meta .BooleanField ('is_public' , 'is public' ),
168
- meta .IPAddressField ('ip_address' , 'IP address' ),
169
- # TODO: Change this to is_removed, like Comment
170
- meta .BooleanField ('approved' , 'approved by staff' ),
171
- meta .ForeignKey (core .Site ),
172
- )
173
- ordering = ('-submit_date' ,)
174
- admin = meta .Admin (
175
- fields = (
176
- (None , {'fields' : ('content_type_id' , 'object_id' , 'site_id' )}),
177
- ('Content' , {'fields' : ('person_name' , 'comment' )}),
178
- ('Meta' , {'fields' : ('submit_date' , 'is_public' , 'ip_address' , 'approved' )}),
179
- ),
180
- list_display = ('person_name' , 'submit_date' , 'content_type_id' , 'get_content_object' ),
181
- list_filter = ('submit_date' ,),
182
- date_hierarchy = 'submit_date' ,
183
- search_fields = ('comment' , 'person_name' ),
184
- )
156
+ # A FreeComment is a comment by a non-registered user.
157
+ content_type = meta .ForeignKey (core .ContentType )
158
+ object_id = meta .IntegerField ('object ID' )
159
+ comment = meta .TextField (maxlength = 3000 )
160
+ person_name = meta .CharField ("person's name" , maxlength = 50 )
161
+ submit_date = meta .DateTimeField ('date/time submitted' , auto_now_add = True )
162
+ is_public = meta .BooleanField ()
163
+ ip_address = meta .IPAddressField ()
164
+ # TODO: Change this to is_removed, like Comment
165
+ approved = meta .BooleanField ('approved by staff' )
166
+ site = meta .ForeignKey (core .Site )
167
+ class META :
168
+ db_table = 'comments_free'
169
+ ordering = ('-submit_date' ,)
170
+ admin = meta .Admin (
171
+ fields = (
172
+ (None , {'fields' : ('content_type' , 'object_id' , 'site' )}),
173
+ ('Content' , {'fields' : ('person_name' , 'comment' )}),
174
+ ('Meta' , {'fields' : ('submit_date' , 'is_public' , 'ip_address' , 'approved' )}),
175
+ ),
176
+ list_display = ('person_name' , 'submit_date' , 'content_type' , 'get_content_object' ),
177
+ list_filter = ('submit_date' ,),
178
+ date_hierarchy = 'submit_date' ,
179
+ search_fields = ('comment' , 'person_name' ),
180
+ )
185
181
186
182
def __repr__ (self ):
187
183
return "%s: %s..." % (self .person_name , self .comment [:100 ])
@@ -203,26 +199,25 @@ def get_content_object(self):
203
199
get_content_object .short_description = 'Content object'
204
200
205
201
class KarmaScore (meta .Model ):
206
- module_name = 'karma'
207
- fields = (
208
- meta .ForeignKey (auth .User ),
209
- meta .ForeignKey (Comment ),
210
- meta .SmallIntegerField ('score' , 'score' , db_index = True ),
211
- meta .DateTimeField ('scored_date' , 'date scored' , auto_now = True ),
212
- )
213
- unique_together = (('user_id' , 'comment_id' ),)
214
- module_constants = {
215
- # what users get if they don't have any karma
216
- 'DEFAULT_KARMA' : 5 ,
217
- 'KARMA_NEEDED_BEFORE_DISPLAYED' : 3 ,
218
- }
202
+ user = meta .ForeignKey (auth .User )
203
+ comment = meta .ForeignKey (Comment )
204
+ score = meta .SmallIntegerField (db_index = True )
205
+ scored_date = meta .DateTimeField (auto_now = True )
206
+ class META :
207
+ module_name = 'karma'
208
+ unique_together = (('user' , 'comment' ),)
209
+ module_constants = {
210
+ # what users get if they don't have any karma
211
+ 'DEFAULT_KARMA' : 5 ,
212
+ 'KARMA_NEEDED_BEFORE_DISPLAYED' : 3 ,
213
+ }
219
214
220
215
def __repr__ (self ):
221
216
return "%d rating by %s" % (self .score , self .get_user ())
222
217
223
218
def _module_vote (user_id , comment_id , score ):
224
219
try :
225
- karma = get_object (comment_id__exact = comment_id , user_id__exact = user_id )
220
+ karma = get_object (comment__id__exact = comment_id , user__id__exact = user_id )
226
221
except KarmaScoreDoesNotExist :
227
222
karma = KarmaScore (None , user_id , comment_id , score , datetime .datetime .now ())
228
223
karma .save ()
@@ -241,13 +236,12 @@ def _module_get_pretty_score(score):
241
236
return int (round ((4.5 * score ) + 5.5 ))
242
237
243
238
class UserFlag (meta .Model ):
244
- db_table = 'comments_user_flags'
245
- fields = (
246
- meta .ForeignKey (auth .User ),
247
- meta .ForeignKey (Comment ),
248
- meta .DateTimeField ('flag_date' , 'date flagged' , auto_now_add = True ),
249
- )
250
- unique_together = (('user_id' , 'comment_id' ),)
239
+ user = meta .ForeignKey (auth .User )
240
+ comment = meta .ForeignKey (Comment )
241
+ flag_date = meta .DateTimeField (auto_now_add = True )
242
+ class META :
243
+ db_table = 'comments_user_flags'
244
+ unique_together = (('user' , 'comment' ),)
251
245
252
246
def __repr__ (self ):
253
247
return "Flag by %r" % self .get_user ()
@@ -261,7 +255,7 @@ def _module_flag(comment, user):
261
255
if int (comment .user_id ) == int (user .id ):
262
256
return # A user can't flag his own comment. Fail silently.
263
257
try :
264
- f = get_object (user_id__exact = user .id , comment_id__exact = comment .id )
258
+ f = get_object (user__id__exact = user .id , comment__id__exact = comment .id )
265
259
except UserFlagDoesNotExist :
266
260
from django .core .mail import mail_managers
267
261
f = UserFlag (None , user .id , comment .id , None )
@@ -270,13 +264,12 @@ def _module_flag(comment, user):
270
264
f .save ()
271
265
272
266
class ModeratorDeletion (meta .Model ):
273
- db_table = 'comments_moderator_deletions'
274
- fields = (
275
- meta .ForeignKey (auth .User , verbose_name = 'moderator' ),
276
- meta .ForeignKey (Comment ),
277
- meta .DateTimeField ('deletion_date' , 'date deleted' , auto_now_add = True ),
278
- )
279
- unique_together = (('user_id' , 'comment_id' ),)
267
+ user = meta .ForeignKey (auth .User , verbose_name = 'moderator' )
268
+ comment = meta .ForeignKey (Comment )
269
+ deletion_date = meta .DateTimeField (auto_now_add = True )
270
+ class META :
271
+ db_table = 'comments_moderator_deletions'
272
+ unique_together = (('user' , 'comment' ),)
280
273
281
274
def __repr__ (self ):
282
275
return "Moderator deletion by %r" % self .get_user ()
0 commit comments