def chatInbox(request): '''用户查看自己的 inbox''' context = {} user = util.user.getCurrentUser(request) if not user: return util.ctrl.infoMsg("您还没有登入,请先登入", title='请先登入', url='/user/signin') # get inputs chat_type = request.GET.get('type') # get chats if chat_type == 'unread': chat_list = user.getChats('unread') else: chat_list = user.getChats('received') # paginator paginator = Paginator(chat_list, per_page=15) page = request.GET.get('page') try: chats = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. chats = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. chats = paginator.page(paginator.num_pages) # add exps userexp, created = UserExp.objects.get_or_create(userid=user.id, category='chat') userexp.addExp(1, '查看收件箱') # render context['chats'] = chats return render(request, 'chat/inbox.html', context)
def userGetUnreadCount(request): # AJAX '''顶部用户栏:更新当前用户的未读消息数目''' # from session user = util.user.getCurrentUser(request) result = {} # from cookies if not user: user = getUserInCookie(request) # get user's gravatar if user: unread_count = user.getChats('unread').count() result['unreadcount'] = unread_count result['msgs'] = [] if unread_count: unread_chats = user.getChats('unread') for uc in unread_chats: sender = User.objects.get(id=uc.senderid) words = uc.title or uc.content if len(words) > 12: words = words[0:12] + '...' result['msgs'].append({ 'sender': sender.nickname, 'words': words, }) return util.ctrl.returnJson(result) else: return util.ctrl.returnJsonResult('nologinuser')
def getUnreadCount(request): # AJAX """顶部用户栏:更新当前用户的未读消息数目""" # from session user = util.user.getCurrentUser(request) result = {} # from cookies if not user: user = util.user.getCookieLogin(request) # get user's gravatar if user: unread_count = user.getChats('unread').count() result['unreadcount'] = unread_count result['msgs'] = [] if unread_count: unread_chats = user.getChats('unread') for uc in unread_chats: sender = uc.sender words = uc.title or uc.content words = django.utils.html.strip_tags(words) if len(words) > 12: words = words[0:12] + '...' result['msgs'].append({ 'sender': sender.nickname if sender else "USER_DELETED", 'words': words, }) return util.ctrl.returnJson(result) else: return util.ctrl.returnJsonResult('nologinuser')
def list(request): """进度列表:显示所有进行中、待开始、追剧中的进度""" user = util.user.getCurrentUser(request) # get user's progresses progresses = Progress.objects.filter(userid=user.id).order_by('-modified') prg_list = {} if len(progresses): for st, stzh in Progress.STATUSES.get('active'): prg_list[st] = progresses.filter(status=st) prg_list["inprogress_and_follow"] = progresses.filter( Q(status='inprogress') | Q(status='follow')) else: TITLE = _("欢迎使用") + " 「" + _("我的进度") + "」" if not user.getChats('received').filter( senderid=Chat.objects.getSyschatUser().id, title=TITLE).exists(): chat_content = loader.render_to_string("progress/msg-welcome.html", {'title': TITLE}) Chat.objects.sendBySys(user, title=TITLE, content=chat_content) # add exps util.userexp.addExp(user, "progress", 1, _("访问「{}」页面").format(_("进度列表"))) # render context = { "prglist": prg_list, } return render(request, "progress/list.html", context)
def userCheckLogin(request): # POST '''用户点击登入后:判断用户是否可以登入''' # get posts username = request.POST.get('username') answer = request.POST.get('answer') rememberme = request.POST.get('rememberme') or 'off' from_ = request.POST.get('from') or '' _failto = request.META.get('HTTP_REFERER', "/user/signin") if not username: messages.error(request, "登入失败:用户名不能为空") return redirect(_failto) if not answer: messages.error(request, "登入失败:答案不能为空") return redirect(_failto) # check username vs. answer try: user = User.objects.get(username=username) except User.DoesNotExist: messages.error(request, "登入失败:找不到用户名为 {} 的用户".format(username)) return redirect(_failto) if user.getUserpermission('signin') is False: # None is OK, True is OK, False is not OK messages.error(request, '登入失败:您已被禁止登入,请联系管理员') return redirect(_failto) if checkAnswer(user, answer): util.user.rememberLogin(request, user) else: messages.error(request, "登入失败:用户名与答案不匹配") return redirect(_failto) # redirections to_ = from_ or '/' response = redirect(to_) # add exp userexp, created = UserExp.objects.get_or_create(userid=user.id, category='user') userexp.addExp(1, '登入成功') # remove old msgs msg_title = 'Hi, @{user.nickname}'.format(user=user) sysuser = Chat.objects.getSyschatUser() old_msg = user.getChats('received').filter(senderid=sysuser.id, title=msg_title) has_old_msg = old_msg.exists() old_msg.delete() # send chat chat_content = ''' 欢迎您归来,开始您的网站之旅吧!<br/> <li>访问 <a href="/progress/list">我的进度</a> 查看进度列表</li> <li>访问 <a href="/user/profile">我的账号信息</a> 查看您的活跃度、进度统计</li> <li>访问 <a href="/chat/conversation?mode=quicknote">临时笔记</a> 随手记录您的想法</li> <li>遇到问题或想 #提建议 ,请发消息给 @系统消息 !</li> ''' new_msg = Chat.objects.sendBySys(user, title=msg_title, content=chat_content) if has_old_msg: new_msg.isread = True new_msg.save() # set cookie if rememberme == 'on': oneweek = 60 * 60 * 24 * 7 response.set_cookie('user_id', user.id, max_age=oneweek) response.set_cookie('user_answer', user.answer1, max_age=oneweek) return response
def inbox(request): """用户查看自己的 inbox""" context = {} # get inputs filter_type = request.GET.get('type') or 'received' # get chats user = util.user.getCurrentUser(request) MSG_FILTERS = collections.OrderedDict() MSG_FILTERS['received'] = _('所有') MSG_FILTERS['unread'] = _('未读') MSG_FILTERS['quicknote'] = _('临时笔记') MSG_FILTERS['fromsys'] = _('系统') MSG_FILTERS['fromhuman'] = _('好友') MSG_FILTERS['sent'] = _('已发送') chat_list = user.getChats(filter_type) if filter_type in MSG_FILTERS.keys() else user.getChats('received') # paginator paginator = Paginator(chat_list, per_page=15) page = request.GET.get('page') try: chats = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. chats = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. chats = paginator.page(paginator.num_pages) # users in sender or receiver but not both chaters = {chat.sender for chat in chat_list} | {chat.receiver for chat in chat_list} chaters -= {user} chater_and_counts = [(ctr, chat_list.filter( Q(senderid=ctr.id) | Q(receiverid=ctr.id) ).count()) for ctr in chaters] # add exp util.userexp.addExp(user, 'chat', 1, _('查看收信箱')) # render context['chats'] = chats context['chater_and_counts'] = chater_and_counts context['msg_filters'] = { 'this': filter_type, 'thiszh': MSG_FILTERS.get(filter_type), 'all': MSG_FILTERS, } return render(request, 'chat/inbox.html', context)
def checkLogin(request): # POST """用户点击登入后:判断用户是否可以登入""" # get posts username = request.POST.get('username') answer = request.POST.get('answer') rememberme = request.POST.get('rememberme') or 'off' next_ = request.POST.get('next') or '' _failto = request.META.get('HTTP_REFERER') or '/user/signin?next=' + next_ if not username: messages.error(request, _("登入失败") + _(":") + _("用户名不能为空")) return redirect(_failto) if not answer: messages.error(request, _("登入失败") + _(":") + _("密码/答案不能为空")) return redirect(_failto) # check username vs. answer user = User.objects.get_or_none(username=username) if not user: messages.error( request, _("登入失败") + _(":") + _("找不到用户名为 {} 的用户").format(username)) return redirect(_failto) if user.getUserpermission( 'signin') is False: # None is OK, True is OK, False is not OK messages.error(request, _("登入失败") + _(":") + _("您已被禁止登入,请联系管理员")) return redirect(_failto) if util.user.checkAnswer(user, answer): util.user.rememberLogin(request, user) else: messages.error(request, _("登入失败") + _(":") + _("用户名与答案不匹配")) return redirect(_failto) # redirections to_ = next_ or '/' response = redirect(to_) # add exp util.userexp.addExp(user, 'user', 1, _("登入成功")) # remove old msgs msg_title = 'Hi, @{user.nickname}'.format(user=user) sysuser = Chat.objects.getSyschatUser() old_msg = user.getChats('received').filter(senderid=sysuser.id, title=msg_title) has_old_msg = old_msg.exists() old_msg.delete() # send chat pages = [ { "name": _("我的进度"), "href": "/progress/list", "desc": _("查看进度列表"), }, { "name": _("个人信息"), "href": "/user/profile", "desc": _("查看您的活跃度、进度统计"), }, { "name": _("临时笔记"), "href": "/chat/conversation?mode=quicknote", "desc": _("随手记录您的想法"), }, ] content_links = "".join([ "<li><a href='{h}'>{n}</a> {d}</li>".format(h=p.get("href"), n=p.get("name"), d=p.get("desc")) for p in pages ]) chat_content = "<br>".join([ _("欢迎您归来,开始您的网站之旅吧!"), "{}".format(content_links), "{} @系统消息".format(_("遇到问题或想 #提建议 ,请发消息给")), ]) new_msg = Chat.objects.sendBySys(user, title=msg_title, content=chat_content) if has_old_msg: new_msg.isread = True new_msg.save() # set cookie if rememberme == 'on': response = util.user.addCookieLogin(response, user=user) return response