def default(self, obj): if isinstance(obj, Comment) : comment = obj #replies = list(comment.comment_set.order_by('created')) text=comment.text_version.text replies = get_viewable_comments(self.request, comment.comment_set.all(), text) # can_view == true because of get_viewable_comments filter can_moderate = has_perm(self.request, 'can_edit_comment', text) can_edit = has_perm(self.request, 'can_edit_comment', text) or has_own_perm(self.request, 'can_edit_comment_own', text, comment) can_delete = has_perm(self.request, 'can_delete_comment', text) or has_own_perm(self.request, 'can_delete_comment_own', text, comment) return {'id' : comment.id, 'key' : comment.key, 'id_key' : comment.id_key, 'created_user_str' : datetime_to_user_str(request_tz_convert(comment.created, self.request)), 'modified_user_str' : datetime_to_user_str(request_tz_convert(comment.modified, self.request)), # 'created_str' : datetime_to_str(comment.created), # TODO change to a simple number as modified if possible 'created' : datetime_to_epoch(comment.created), # TODO change to a simple number as modified if possible 'modified' : datetime_to_epoch(comment.modified), # 'modified' : time.mktime(comment.modified.timetuple()), # 'created' : datetime_to_js_date_str(comment.created), 'reply_to_id' : comment.reply_to_id, 'replies' : replies, 'name' : comment.get_name(), 'email' : comment.get_email(), 'logged_author' : (comment.user != None), 'title':comment.title, 'content':comment.content, 'content_html':comment.content_html, 'tags': ', '.join(parse_tag_input(comment.tags)), 'format': comment.format, 'start_wrapper' : comment.start_wrapper, 'end_wrapper' : comment.end_wrapper, 'start_offset' : comment.start_offset, 'end_offset' : comment.end_offset, 'url': comment.url, 'state' : comment.state, 'permalink' : reverse('text-view-show-comment', args=[text.key, comment.id_key]), # permission 'can_edit' : can_edit, 'can_delete' : can_delete, 'can_moderate' : can_moderate, } if isinstance(obj, Tag) : tag = obj # RBE each time issuing a db request to find comments related to this tag !!! TODO return { 'ids' : [t.id for t in tag.items.all()], 'name' : tag.name, 'font_size' : tag.font_size} return simplejson.JSONEncoder.default(self, obj)
def default(self, obj): if isinstance(obj, Comment) : comment = obj #replies = list(comment.comment_set.order_by('created')) text=comment.text_version.text replies = get_viewable_comments(self.request, comment.comment_set.all(), text) # can_view == true because of get_viewable_comments filter can_moderate = has_perm(self.request, 'can_edit_comment', text) can_edit = has_perm(self.request, 'can_edit_comment', text) or has_own_perm(self.request, 'can_edit_comment_own', text, comment) can_delete = has_perm(self.request, 'can_delete_comment', text) or has_own_perm(self.request, 'can_delete_comment_own', text, comment) return {'id' : comment.id, 'key' : comment.key, 'id_key' : comment.id_key, 'created_user_str' : datetime_to_user_str(request_tz_convert(comment.created, self.request)), 'modified_user_str' : datetime_to_user_str(request_tz_convert(comment.modified, self.request)), # 'created_str' : datetime_to_str(comment.created), # TODO change to a simple number as modified if possible 'created' : datetime_to_epoch(comment.created), # TODO change to a simple number as modified if possible 'modified' : datetime_to_epoch(comment.modified), # 'modified' : time.mktime(comment.modified.timetuple()), # 'created' : datetime_to_js_date_str(comment.created), 'reply_to_id' : comment.reply_to_id, 'replies' : replies, 'name' : comment.get_name(), 'email' : comment.get_email(), 'logged_author' : (comment.user != None), 'title':comment.title, 'content':comment.content, 'content_html':comment.content_html, 'tags': ', '.join(parse_tag_input(comment.tags)), 'category': comment.category, 'format': comment.format, 'start_wrapper' : comment.start_wrapper, 'end_wrapper' : comment.end_wrapper, 'start_offset' : comment.start_offset, 'end_offset' : comment.end_offset, 'state' : comment.state, 'permalink' : reverse('text-view-show-comment', args=[text.key, comment.id_key]), # permission 'can_edit' : can_edit, 'can_delete' : can_delete, 'can_moderate' : can_moderate, } if isinstance(obj, Tag) : tag = obj # RBE each time issuing a db request to find comments related to this tag !!! TODO return { 'ids' : [t.id for t in tag.items.all()], 'name' : tag.name, 'font_size' : tag.font_size} return simplejson.JSONEncoder.default(self, obj)
def get_filter_datas(request, text_version, text): from django.db.models import Count from datetime import datetime, timedelta allowed_ids = [c.id for c in comments_thread(request, text_version, text)] allowed_comments = Comment.objects.filter(Q(text_version=text_version),Q(deleted=False),Q(id__in=allowed_ids)) #print allowed_ids # authors # names = list(Comment.objects.filter(text_version__text__key=key).filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name')) names = list(allowed_comments.filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name')) if DECORATED_CREATORS: names = list(allowed_comments.filter(user__isnull=False).values('name').annotate(nb_comments=Count('id'))) #.order_by('name')) author = text_version.name else: 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')) has_author = User.objects.filter(id=text_version.user_id).values('username') if has_author: author = has_author[0]['username'] else: author = '' if request.GET.get('name', None): me = request.GET.get('name', None) else: me = request.user.username for name in names: if name['name']: if name['name'] == me: name['display'] = _(u'me') + ' (' + name['name'] + ')' elif name['name'] == author: name['display'] = _(u'author') + ' (' + name['name'] + ')' else: name['display'] = name['name'] else: name['display'] = '' def sort_with_author_or_me_first(x, y): if x and (x.startswith(_(u'me')) or x.startswith(_(u'author'))): return -1 if y and (y.startswith(_(u'me')) or y.startswith(_(u'author'))): return 1 else: return cmp(x, y) names.sort(cmp = sort_with_author_or_me_first, key = lambda obj:obj["display"]) # dates # TODO maybe optimize by comparing dates in python and saving these 'by day db requests' nb_days = [1, 3, 7, 30] dates = [] today = datetime.today() for nb_day in nb_days : day_date = today - timedelta(nb_day) dates.append({'nb_day' : nb_day, 'nb_day_date':datetime_to_epoch(day_date), 'nb_comments':allowed_comments.filter(modified__gt = day_date).count()}) # tags comment_ids = [c.id for c in allowed_comments] tags = list(Tag.objects.filter(items__content_type = ContentType.objects.get_for_model(Comment),items__object_id__in=comment_ids).values("name").annotate(nb_comments=Count('id')).distinct().order_by('name')) # states states = [] for state in comment_states : states.append({'state' : state, 'nb_comments':allowed_comments.filter(state = state).count()}) return {'names':names, 'dates':dates, 'tags':tags, 'states':states}
def get_filter_datas(request, text_version, text): from django.db.models import Count from datetime import datetime, timedelta allowed_ids = [c.id for c in comments_thread(request, text_version, text)] allowed_comments = Comment.objects.filter(Q(text_version=text_version), Q(deleted=False), Q(id__in=allowed_ids)) #print allowed_ids # authors # names = list(Comment.objects.filter(text_version__text__key=key).filter(user__isnull=True).values('name').annotate(nb_comments=Count('id'))) #.order_by('name')) names = list( allowed_comments.filter(user__isnull=True).values('name').annotate( nb_comments=Count('id'))) #.order_by('name')) if DECORATED_CREATORS: names = list( allowed_comments.filter( user__isnull=False).values('name').annotate( nb_comments=Count('id'))) #.order_by('name')) author = text_version.name else: 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')) has_author = User.objects.filter( id=text_version.user_id).values('username') if has_author: author = has_author[0]['username'] else: author = '' if request.GET.get('name', None): me = request.GET.get('name', None) else: me = request.user.username for name in names: if name['name']: if name['name'] == me: name['display'] = _(u'me') + ' (' + name['name'] + ')' elif name['name'] == author: name['display'] = _(u'author') + ' (' + name['name'] + ')' else: name['display'] = name['name'] else: name['display'] = '' def sort_with_author_or_me_first(x, y): if x and (x.startswith(_(u'me')) or x.startswith(_(u'author'))): return -1 if y and (y.startswith(_(u'me')) or y.startswith(_(u'author'))): return 1 else: return cmp(x, y) names.sort(cmp=sort_with_author_or_me_first, key=lambda obj: obj["display"]) # dates # TODO maybe optimize by comparing dates in python and saving these 'by day db requests' nb_days = [1, 3, 7, 30] dates = [] today = datetime.today() for nb_day in nb_days: day_date = today - timedelta(nb_day) dates.append({ 'nb_day': nb_day, 'nb_day_date': datetime_to_epoch(day_date), 'nb_comments': allowed_comments.filter(modified__gt=day_date).count() }) # tags comment_ids = [c.id for c in allowed_comments] tags = list( Tag.objects.filter( items__content_type=ContentType.objects.get_for_model(Comment), items__object_id__in=comment_ids).values("name").annotate( nb_comments=Count('id')).distinct().order_by('name')) # states states = [] for state in comment_states: states.append({ 'state': state, 'nb_comments': allowed_comments.filter(state=state).count() }) return {'names': names, 'dates': dates, 'tags': tags, 'states': states}