src/cm/client.py
changeset 426 7c31b3a1284b
parent 400 b82f4f749b44
child 429 fc7477d34489
equal deleted inserted replaced
425:d70552fc1a66 426:7c31b3a1284b
    17 from time import mktime, sleep
    17 from time import mktime, sleep
    18 from cm.converters.pandoc_converters import pandoc_convert
    18 from cm.converters.pandoc_converters import pandoc_convert
    19 from cm.security import get_viewable_comments, list_viewable_comments, has_perm, has_perm_on_text, has_perm_on_comment, has_own_perm
    19 from cm.security import get_viewable_comments, list_viewable_comments, has_perm, has_perm_on_text, has_perm_on_comment, has_own_perm
    20 from cm.activity import register_activity
    20 from cm.activity import register_activity
    21 from cm.utils.date import datetime_to_user_str, datetime_to_epoch
    21 from cm.utils.date import datetime_to_user_str, datetime_to_epoch
    22 from cm.cm_settings import AUTO_CONTRIB_REGISTER
    22 from cm.cm_settings import AUTO_CONTRIB_REGISTER, DECORATED_CREATORS
    23 from settings import CLIENT_DATE_FMT
    23 from settings import CLIENT_DATE_FMT
    24 import re
    24 import re
    25 import time
    25 import time
    26 import operator
    26 import operator
    27 
    27 
   297     #print allowed_ids 
   297     #print allowed_ids 
   298 
   298 
   299     # authors
   299     # authors
   300 #    names = list(Comment.objects.filter(text_version__text__key=key).filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name'))
   300 #    names = list(Comment.objects.filter(text_version__text__key=key).filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name'))
   301     names = list(allowed_comments.filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name'))
   301     names = list(allowed_comments.filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name'))
   302     names += list(User.objects.filter(Q(comment__text_version=text_version),Q(comment__deleted=False), Q(comment__id__in=allowed_ids)).extra(select={'name': "username"}).values('name').annotate(nb_comments=Count('id'))) #.order_by('username'))
   302     if DECORATED_CREATORS:
   303     names.sort(key = lambda obj:obj["name"])
   303       names = list(allowed_comments.filter(user__isnull=False).values('name').annotate(nb_comments=Count('id'))) #.order_by('name'))
       
   304       author = text_version.name
       
   305     else:
       
   306       names += list(User.objects.filter(Q(comment__text_version=text_version),Q(comment__deleted=False), Q(comment__id__in=allowed_ids)).extra(select={'name': "username"}).values('name').annotate(nb_comments=Count('id'))) #.order_by('username'))
       
   307       author = User.objects.filter(id=text_version.user_id).values('username')[0]['username']
       
   308     if request.GET.get('name', None):
       
   309       me = request.GET.get('name', None)
       
   310     else:
       
   311       me = request.user.username
       
   312     for name in names:
       
   313       if name['name'] == me:
       
   314         name['display'] = _(u'me') + ' (' + name['name'] + ')'
       
   315       elif name['name'] == author:
       
   316         name['display'] = _(u'author') + ' (' + name['name'] + ')'
       
   317       else:
       
   318         name['display'] = name['name']
       
   319 
       
   320     def sort_with_author_or_me_first(x, y):
       
   321       if x.startswith(_(u'me')) or x.startswith(_(u'author')):
       
   322         return -1
       
   323       if y.startswith(_(u'me')) or y.startswith(_(u'author')):
       
   324         return 1
       
   325       else:
       
   326         return cmp(x, y)
       
   327 
       
   328     names.sort(cmp = sort_with_author_or_me_first, key = lambda obj:obj["display"])
   304 
   329 
   305     # dates
   330     # dates
   306     # TODO maybe optimize by comparing dates in python and saving these 'by day db requests'
   331     # TODO maybe optimize by comparing dates in python and saving these 'by day db requests'
   307     nb_days = [1, 3, 7, 30]
   332     nb_days = [1, 3, 7, 30]
   308     dates = []
   333     dates = []