def get_messages(self, obj): messages = obj.clarification_messages.all().order_by('created_at') obj_tmpl = '<div>{msg}</div><br/><div>{msg_docs}</div>' line_tmpl = '<b><{msg.user_repr}</b><br/>{msg_created}:<br/><br/>{msg.message}' line_doc_tmpl = '<div><a href="{doc_url}">{doc_name}</a></div>' ret_text = '' idx = 1 msgs_cnt = messages.count() for msg in messages: msg_text = line_tmpl.format( msg=msg, msg_created=localtime( msg.created_at).strftime('%d.%m.%Y %H:%M')) docs = '' if msg.documents_links.exists(): docs += '<b>Документы:</b><br/>' for msg_doc in msg.documents_links.all(): doc_name = msg_doc.name if msg_doc.document and msg_doc.document.file: doc_url = msg_doc.document.file.url else: doc_url = '#' docs += line_doc_tmpl.format( doc_name=doc_name, doc_url=doc_url, ) ret_text += obj_tmpl.format(msg=msg_text, msg_docs=docs) if idx < msgs_cnt: ret_text += '<hr/>' idx += 1 return ret_text
def msgcenter(request):#留言中心 cook = request.COOKIES.get("username") if cook == None: return render(request, 'index.html') else: trans_cook=cook.encode('latin-1').decode('utf-8') user = Users.objects.get(name = trans_cook) notice_list = Notices.objects.all().order_by('-time') if user.usertype==0: student=StudentInfos.objects.get(student=user) messages1=Messages.objects.filter(Q(fromuser=student)&Q(Q(reply=None)|Q(reply=""))).order_by('-updatetime') messages2=Messages.objects.filter(Q(fromuser=student)&(~Q(reply=None))&(~Q(reply=''))).order_by('-updatetime') content={'notice_list':notice_list,'message1_list':messages1,'message2_list':messages2} return render(request, 'msgcenter.html',content) elif user.usertype==1: teacher=TeacherInfos.objects.get(teacher=user) messages1=Messages.objects.filter(Q(teacher=teacher)&Q(Q(reply=None)|Q(reply=""))).order_by('-updatetime') messages2=Messages.objects.filter(Q(teacher=teacher)&(~Q(reply=None))&(~Q(reply=''))).order_by('-updatetime') content={'notice_list':notice_list,'message1_list':messages1,'message2_list':messages2} return render(request, 'msgcenter_t.html',content) elif user.usertype==2: messages=Messages.objects.all().order_by('-updatetime') print(messages.count()) content={'notice_list':notice_list,'message_list':messages} return render(request, 'msgcenter_a.html',content)
def get_context_data(self, **kwargs): user = self.object context = {} count_types = OrderedDict() fields_or_lookup = ( {'collaborators__contains': user.username}, {'fullname_and_username__contains': user.username}, ) counter_class = {} #{ # 'wiki': WikiCollabCount, # 'ticket': TicketCollabCount, #} types = ['thread'] #types.extend(['ticket', 'wiki', 'changeset', 'attachment']) messages = Message.objects.filter(from_address__user__pk=user.pk) for type in types: CounterClass = counter_class.get(type) if CounterClass: try: counter = CounterClass.objects.get(author=user.username) except CounterClass.DoesNotExist: count_types[trans(type)] = 0 else: count_types[trans(type)] = counter.count elif type == 'thread': count_types[trans(type)] = messages.count() else: sqs = SearchQuerySet() for filter_or in fields_or_lookup: sqs = sqs.filter_or(type=type, **filter_or) count_types[trans(type)] = sqs.count() context['type_count'] = count_types sqs = SearchQuerySet() for filter_or in fields_or_lookup: sqs = sqs.filter_or(**filter_or).exclude(type='thread') context['results'] = sqs.order_by('-modified', '-created')[:10] email_pks = [addr.pk for addr in user.emails.iterator()] query = Message.objects.filter(from_address__in=email_pks) query = query.order_by('-received_time') context['emails'] = query[:10] count_by = 'thread__mailinglist__name' context['list_activity'] = dict(messages.values_list(count_by)\ .annotate(Count(count_by))\ .order_by(count_by)) context.update(kwargs) return super(UserProfileDetailView, self).get_context_data(**context)
def get_context_data(self, **kwargs): user = self.object context = {} count_types = OrderedDict() fields_or_lookup = ( { 'collaborators__contains': user.username }, { 'fullname_and_username__contains': user.username }, ) counter_class = { 'wiki': WikiCollabCount, 'ticket': TicketCollabCount, } messages = Message.objects.filter(from_address__user__pk=user.pk) for type in ['thread', 'ticket', 'wiki', 'changeset', 'attachment']: CounterClass = counter_class.get(type) if CounterClass: try: counter = CounterClass.objects.get(author=user.username) except CounterClass.DoesNotExist: count_types[trans(type)] = 0 else: count_types[trans(type)] = counter.count elif type == 'thread': count_types[trans(type)] = messages.count() else: sqs = SearchQuerySet() for filter_or in fields_or_lookup: sqs = sqs.filter_or(type=type, **filter_or) count_types[trans(type)] = sqs.count() context['type_count'] = count_types sqs = SearchQuerySet() for filter_or in fields_or_lookup: sqs = sqs.filter_or(**filter_or).exclude(type='thread') context['results'] = sqs.order_by('-modified', '-created')[:10] email_pks = [addr.pk for addr in user.emails.iterator()] query = Message.objects.filter(from_address__in=email_pks) query = query.order_by('-received_time') context['emails'] = query[:10] count_by = 'thread__mailinglist__name' context['list_activity'] = dict(messages.values_list(count_by)\ .annotate(Count(count_by))\ .order_by(count_by)) context.update(kwargs) return super(UserProfileDetailView, self).get_context_data(**context)
def load_messages(request, talk): context = { 'request': request, } user = request.user favorites = request.GET.get('favorite', False) mines = request.GET.get('mine', False) showing = request.GET.get('showing', '') n_views = 0 if not favorites: if mines: messages = TalkMessages.objects.filter(talk__id=talk, user=user) else: messages = TalkMessages.objects.filter(talk__id=talk) else: if mines: messages = TalkMessages.objects.filter( talk__id=talk, chat_favorites_message__isnull=False, chat_favorites_message__user=user, user=user) else: messages = TalkMessages.objects.filter( talk__id=talk, chat_favorites_message__isnull=False, chat_favorites_message__user=user) if showing: #Exclude ajax creation messages results showing = showing.split(',') messages = messages.exclude(id__in=showing) has_page = request.GET.get('page', None) paginator = Paginator(messages.order_by("-create_date"), 20) try: page_number = int(request.GET.get('page', 1)) except ValueError: raise Http404 try: page_obj = paginator.page(page_number) except EmptyPage: raise Http404 context['messages'] = page_obj.object_list response = render_to_string("chat/_list_messages.html", context, request) return JsonResponse({ "messages": response, "count": messages.count(), "num_pages": paginator.num_pages, "num_page": page_obj.number })
def get_student_data(sid,st=None,et=None): student = get_object_or_404(Student,id=sid) user = student.user tile_content_type = ContentType.objects.get_by_natural_key("kinger", "tile") acc_logs = Access_log.objects.filter(user=user,type=2) comments = Comment.objects.filter(user=user,content_type=tile_content_type) daily_vistors = DailyRecordVisitor.objects.filter(visitor=user) sms_replaies = SmsReplay.objects.filter(sender=user) messages = MessageRecipient.objects.filter(message__sender=user) tiles = Tile.objects.filter(creator=user) if st: acc_logs = acc_logs.filter(send_time__gte=st) comments = comments.filter(submit_date__gte=st) daily_vistors = daily_vistors.filter(visit_time__gte=st) sms_replaies = sms_replaies.filter(target__deal_date__gte=st) messages = messages.filter(message__sent_at__gte=st) tiles = tiles.filter(start_time__gte=st) if et: acc_logs = acc_logs.filter(send_time__lt=et) comments = comments.filter(submit_date__lt=et) daily_vistors = daily_vistors.filter(visit_time__lt=et) sms_replaies = sms_replaies.filter(target__deal_date__lt=et) messages = messages.filter(message__sent_at__lt=et) tiles = tiles.filter(start_time__lt=et) web_num = acc_logs.exclude(url__startswith='/api').count() api_num = acc_logs.filter(url__startswith='/api').count() img_num = DailyRecordVisitor.objects.tile_img_count(user,stime=st,etime=et,type='img') word_num = DailyRecordVisitor.objects.tile_img_count(user,stime=st,etime=et,type='word') record_num = DailyRecordVisitor.objects.tile_img_count(user,stime=st,etime=et,type='record') word_comments = 0 img_comments = 0 for c in comments: try: if c.content_object.new_type_id in [1,2]: img_comments += 1 if c.content_object.new_type_id == 4: word_comments += 1 except: pass content_type = ContentType.objects.get_for_model(Activity) active_num = daily_vistors.filter(target_content_type=content_type).count() sms = sms_replaies.count() msg = messages.count() imgs = tiles.filter(new_type_id=1).count() words = tiles.filter(new_type_id=4).count() ctx = {'student':student,'web_num':web_num,'active_num':active_num,'record_num':record_num,\ 'sms':sms,'msg':msg,'api_num':api_num,'img_num':img_num,'word_comments':word_comments,\ 'img_comments':img_comments,'word_num':word_num,'img_num':img_num,'imgs':imgs,'words':words} return ctx
def get_context_data(self, **kwargs): user = self.object context = {} count_types = OrderedDict() fields_or_lookup = ( {"collaborators__contains": user.username}, {"fullname_and_username__contains": user.username}, ) counter_class = {"wiki": WikiCollabCount, "ticket": TicketCollabCount} messages = Message.objects.filter(from_address__user__pk=user.pk) for type in ["thread", "ticket", "wiki", "changeset", "attachment"]: CounterClass = counter_class.get(type) if CounterClass: try: counter = CounterClass.objects.get(author=user.username) except CounterClass.DoesNotExist: count_types[trans(type)] = 0 else: count_types[trans(type)] = counter.count elif type == "thread": count_types[trans(type)] = messages.count() else: sqs = SearchQuerySet() for filter_or in fields_or_lookup: sqs = sqs.filter_or(type=type, **filter_or) count_types[trans(type)] = sqs.count() context["type_count"] = count_types sqs = SearchQuerySet() for filter_or in fields_or_lookup: sqs = sqs.filter_or(**filter_or).exclude(type="thread") context["results"] = sqs.order_by("-modified", "-created")[:10] email_pks = [addr.pk for addr in user.emails.iterator()] query = Message.objects.filter(from_address__in=email_pks) query = query.order_by("-received_time") context["emails"] = query[:10] count_by = "thread__mailinglist__name" context["list_activity"] = dict(messages.values_list(count_by).annotate(Count(count_by)).order_by(count_by)) context.update(kwargs) return super(UserProfileDetailView, self).get_context_data(**context)
def get_conversation_messages(request, conversation_pk, message_count): try: conversation = Conversation.objects.get(pk=conversation_pk) except Exception as e: return JsonResponse({'error': str(e)}, status=404) messages = conversation.message_set.order_by('-pk')[:message_count] messages_serialized = serializers.serialize('json', messages) messages_json = json.loads(messages_serialized) all_messages_shown = messages.count() == conversation.message_set.count() result = { 'messages': messages_json, 'allMessagesShown': all_messages_shown } return JsonResponse(result, safe=False)
def detail(request, id): try: post = Article.objects.get(id=str(id)) post.viewed() # 更新浏览次数 tags = post.tags.all() messages = message_event(article=str(id)) count = messages.count() next_post = post.next_article() # 上一篇文章对象 prev_post = post.prev_article() # 下一篇文章对象 except Article.DoesNotExist: raise Http404 return render( request, 'post.html', { 'post': post, 'tags': tags, 'count': count, 'category_list': categories, 'next_post': next_post, 'prev_post': prev_post, 'months': months, 'messages': messages })