def message_list(request): """List and group messages related to the user, including he/she send to others and others send to he/she. """ username = request.user.username messages = UserMessage.objects.get_messages_related_to_user(username) msgs = msg_info_list(messages, username) total_unread = 0 for msg in msgs: total_unread += msg[1]['not_read'] return render_to_response('message/all_msg_list.html', { 'msgs': msgs, 'total_unread': total_unread, }, context_instance=RequestContext(request))
def message_send(request): """Handle POST request to send message to user(s). """ if not request.is_ajax() or request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' result = {} username = request.user.username fr = request.GET.get('from') if not fr: result['error'] = [_(u'Argument missing')] return HttpResponse(json.dumps(result), content_type=content_type) mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist('mass_email') # e.g: [u'[email protected], u'*****@*****.**'] if not mass_msg: result['error'] = [_(u'message is required')] return HttpResponse(json.dumps(result), content_type=content_type) if not mass_emails: result['error'] = [_(u'contact is required')] return HttpResponse(json.dumps(result), content_type=content_type) # attachment selected = request.POST.getlist('selected') # selected files & dirs: [u'<repo_id><path>', ...] attached_items = [] if len(selected) > 0: for item in selected: if item[-1] == '/': # dir is not allowed, for now continue att = {} att['repo_id'] = item[0:36] att['path'] = item[36:] attached_items.append(att) email_sended = [] errors = [] msgs = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email or not is_valid_username(to_email): continue if to_email == username: errors.append(_(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): errors.append(_(u'Failed to send message to %s, user not found.') % to_email) continue usermsg = UserMessage.objects.add_unread_message(username, to_email, mass_msg) usermsg.attachments = [] if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, to_email, repo_id, path) att = UserMsgAttachment.objects.add_user_msg_attachment(usermsg, pfds) att.repo_id = repo_id att.path = path att.name = os.path.basename(path.rstrip('/')) att.token = pfds.token usermsg.attachments.append(att) msgs.append(usermsg) email_sended.append(to_email) html = '' if email_sended: ctx = {} if fr == 'all': # from 'all_msg_list' page ctx['msgs'] = msg_info_list(msgs, username) html = render_to_string('message/all_msg.html', ctx) else: ctx['msg'] = msgs[0] html = render_to_string('message/user_msg.html', ctx, context_instance=RequestContext(request)) return HttpResponse(json.dumps({"html": html, "error": errors}), content_type=content_type) else: return HttpResponse(json.dumps({"html": html, "error": errors}), status=400, content_type=content_type)
def message_send(request): """Handle POST request to send message to user(s). """ if not request.is_ajax() or request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' result = {} username = request.user.username fr = request.GET.get('from') if not fr: result['error'] = [_(u'Argument missing')] return HttpResponse(json.dumps(result), content_type=content_type) mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist( 'mass_email') # e.g: [u'[email protected], u'*****@*****.**'] if not mass_msg: result['error'] = [_(u'message is required')] return HttpResponse(json.dumps(result), content_type=content_type) if not mass_emails: result['error'] = [_(u'contact is required')] return HttpResponse(json.dumps(result), content_type=content_type) # attachment selected = request.POST.getlist( 'selected') # selected files & dirs: [u'<repo_id><path>', ...] attached_items = [] if len(selected) > 0: for item in selected: if item[-1] == '/': # dir is not allowed, for now continue att = {} att['repo_id'] = item[0:36] att['path'] = item[36:] attached_items.append(att) email_sended = [] errors = [] msgs = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email or not is_valid_username(to_email): continue if to_email == username: errors.append(_(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): errors.append( _(u'Failed to send message to %s, user not found.') % to_email) continue usermsg = UserMessage.objects.add_unread_message( username, to_email, mass_msg) msgs.append(usermsg) if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, to_email, repo_id, path) UserMsgAttachment.objects.add_user_msg_attachment( usermsg, pfds) email_sended.append(to_email) html = '' if email_sended: ctx = {} if fr == 'all': # from 'all_msg_list' page ctx['msgs'] = msg_info_list(msgs, username) html = render_to_string('message/all_msg.html', ctx) else: ctx['msg'] = msgs[0] html = render_to_string('message/user_msg.html', ctx) return HttpResponse(json.dumps({ "html": html, "error": errors }), content_type=content_type) else: return HttpResponse(json.dumps({ "html": html, "error": errors }), status=400, content_type=content_type)