Exemple #1
0
def group_recommend(request):
    """
    Recommend a file or directory to a group.
    """
    if request.method != 'POST':
        raise Http404

    next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = SITE_ROOT
    
    form = GroupRecommendForm(request.POST)
    if form.is_valid():
        repo_id = form.cleaned_data['repo_id']
        attach_type = form.cleaned_data['attach_type']
        path = form.cleaned_data['path']
        message = form.cleaned_data['message']
        groups = request.POST.getlist('groups') # groups is a group_id list, e.g. [u'1', u'7']
        username = request.user.username

        # Check group id format
        for group_id in groups:
            try:
                group_id = int(group_id)
            except ValueError:
                messages.error(request, _(u'Error: wrong group id'))
                return HttpResponseRedirect(next)

            # Get that group
            group = get_group(group_id)

            # TODO: Check whether repo is in the group and Im in the group
            if not is_group_user(group_id, username):
                err_msg = _(u'Error: you are not in group %s.')
                messages.error(request, err_msg %  group.group_name)
                continue

            # save message to group
            gm = GroupMessage(group_id=group_id, from_email=username,
                              message=message)
            gm.save()

            # send signal
            grpmsg_added.send(sender=GroupMessage, group_id=group_id,
                              from_email=request.user.username)
                    
            # save attachment
            ma = MessageAttachment(group_message=gm, repo_id=repo_id,
                                   attach_type=attach_type, path=path,
                                   src='recommend')
            ma.save()

            group_url = reverse('group_info', args=[group_id])
            msg = _(u'Successfully recommended to <a href="%(url)s" target="_blank">%(name)s</a>.') %\
                {'url':group_url, 'name':group.group_name}
            messages.add_message(request, messages.INFO, msg)

    else:
        messages.add_message(request, messages.ERROR, _(u'Failed to recommend.'))
    return HttpResponseRedirect(next)
Exemple #2
0
def group_add_discussion(request, group):
    if not request.is_ajax() or request.method != 'POST':
        raise Http404

    # only login user can post to public group
    if group.view_perm == "pub" and not request.user.is_authenticated():
        raise Http404

    content_type = 'application/json; charset=utf-8'
    result = {}

    username = request.user.username

    #form = MessageForm(request.POST)
    msg = request.POST.get('message')
    selected = request.POST.getlist('selected') # files selected as attachment

    if not msg:
        result['error'] = _(u'Discussion is required.')
        return HttpResponse(json.dumps(result), status=400, content_type=content_type)

    gm = GroupMessage()
    gm.group_id = group.id
    gm.from_email = username
    gm.message = msg
    gm.save()

    # send signal
    grpmsg_added.send(sender=GroupMessage, group_id=group.id,
                      from_email=username, message=msg)

    gm.attachments = []
    if selected:
        for item in selected:
            if item[-1] == '/': # dir is not allowed, for now
                continue

            repo_id = item[0:36]
            path = item[36:]
            ma = MessageAttachment(group_message=gm, repo_id=repo_id,
                attach_type='file', path=path, src='recommend')
            ma.save()
            ma.name = os.path.basename(path)
            gm.attachments.append(ma)

    ctx = { 'msg': gm }
    msg_html = render_to_string("group/new_discussion_con.html", ctx,
         context_instance=RequestContext(request))
    return HttpResponse(json.dumps({'msg_id': gm.id, 'msg_con': msg_html}), content_type=content_type)
Exemple #3
0
def group_add_discussion(request, group):
    if not request.is_ajax() or request.method != 'POST':
        raise Http404

    # only login user can post to public group
    if group.view_perm == "pub" and not request.user.is_authenticated():
        raise Http404

    content_type = 'application/json; charset=utf-8'
    result = {}

    username = request.user.username

    #form = MessageForm(request.POST)
    msg = request.POST.get('message')
    selected = request.POST.getlist('selected') # files selected as attachment

    if not msg:
        result['error'] = _(u'Discussion is required.')
        return HttpResponse(json.dumps(result), status=400, content_type=content_type)

    gm = GroupMessage()
    gm.group_id = group.id
    gm.from_email = username
    gm.message = msg
    gm.save()

    # send signal
    grpmsg_added.send(sender=GroupMessage, group_id=group.id,
                      from_email=username, message=msg)

    gm.attachments = []
    if selected:
        for item in selected:
            if item[-1] == '/': # dir is not allowed, for now
                continue

            repo_id = item[0:36]
            path = item[36:]
            ma = MessageAttachment(group_message=gm, repo_id=repo_id,
                attach_type='file', path=path, src='recommend')
            ma.save()
            ma.name = os.path.basename(path)
            gm.attachments.append(ma)

    ctx = { 'msg': gm }
    msg_html = render_to_string("group/new_discussion_con.html", ctx,
         context_instance=RequestContext(request))
    return HttpResponse(json.dumps({'msg_id': gm.id, 'msg_con': msg_html}), content_type=content_type)
Exemple #4
0
def group_recommend(request):
    """
    Get or post file/directory discussions to a group.
    """
    content_type = "application/json; charset=utf-8"
    result = {}
    if request.method == "POST":

        form = GroupRecommendForm(request.POST)
        if form.is_valid():
            repo_id = form.cleaned_data["repo_id"]
            attach_type = form.cleaned_data["attach_type"]
            path = form.cleaned_data["path"]
            message = form.cleaned_data["message"]
            # groups is a group_id list, e.g. [u'1', u'7']
            groups = request.POST.getlist("groups")
            username = request.user.username

            groups_not_in = []
            groups_posted_to = []
            for group_id in groups:
                # Check group id format
                try:
                    group_id = int(group_id)
                except ValueError:
                    result["error"] = _(u"Error: wrong group id")
                    return HttpResponse(json.dumps(result), status=400, content_type=content_type)

                group = get_group(group_id)
                if not group:
                    result["error"] = _(u"Error: the group does not exist.")
                    return HttpResponse(json.dumps(result), status=400, content_type=content_type)

                # TODO: Check whether repo is in the group and Im in the group
                if not is_group_user(group_id, username):
                    groups_not_in.append(group.group_name)
                    continue

                # save message to group
                gm = GroupMessage(group_id=group_id, from_email=username, message=message)
                gm.save()

                # send signal
                grpmsg_added.send(sender=GroupMessage, group_id=group_id, from_email=username, message=message)

                # save attachment
                ma = MessageAttachment(
                    group_message=gm, repo_id=repo_id, attach_type=attach_type, path=path, src="recommend"
                )
                ma.save()

                # save discussion
                fd = FileDiscuss(group_message=gm, repo_id=repo_id, path=path)
                fd.save()

                group_url = reverse("group_discuss", args=[group_id])
                groups_posted_to.append(
                    u'<a href="%(url)s" target="_blank">%(name)s</a>' % {"url": group_url, "name": group.group_name}
                )

            if len(groups_posted_to) > 0:
                result["success"] = _(u"Successfully posted to %(groups)s.") % {"groups": ", ".join(groups_posted_to)}

            if len(groups_not_in) > 0:
                result["error"] = _(u"Error: you are not in group %s.") % (", ".join(groups_not_in))

        else:
            result["error"] = str(form.errors)
            return HttpResponse(json.dumps(result), status=400, content_type=content_type)

    # request.method == 'GET'
    else:
        repo_id = request.GET.get("repo_id")
        path = request.GET.get("path", None)
        repo = get_repo(repo_id)
        if not repo:
            result["error"] = _(u"Error: the library does not exist.")
            return HttpResponse(json.dumps(result), status=400, content_type=content_type)
        if path is None:
            result["error"] = _(u"Error: no path.")
            return HttpResponse(json.dumps(result), status=400, content_type=content_type)

    # get discussions & replies
    path_hash = calc_file_path_hash(path)
    discussions = FileDiscuss.objects.filter(path_hash=path_hash, repo_id=repo_id)
    msg_ids = [e.group_message_id for e in discussions]

    grp_msgs = GroupMessage.objects.filter(id__in=msg_ids).order_by("-timestamp")
    msg_replies = MessageReply.objects.filter(reply_to__in=grp_msgs)
    for msg in grp_msgs:
        msg.replies = []
        for reply in msg_replies:
            if msg.id == reply.reply_to_id:
                msg.replies.append(reply)
        msg.reply_cnt = len(msg.replies)
        msg.replies = msg.replies[-3:]

    ctx = {}
    ctx["messages"] = grp_msgs
    html = render_to_string("group/discussion_list.html", ctx)
    result["html"] = html
    return HttpResponse(json.dumps(result), content_type=content_type)
Exemple #5
0
def group_recommend(request):
    """
    Get or post file/directory discussions to a group.
    """
    content_type = 'application/json; charset=utf-8'
    result = {}
    if request.method == 'POST':

        form = GroupRecommendForm(request.POST)
        if form.is_valid():
            repo_id = form.cleaned_data['repo_id']
            attach_type = form.cleaned_data['attach_type']
            path = form.cleaned_data['path']
            message = form.cleaned_data['message']
            # groups is a group_id list, e.g. [u'1', u'7']
            groups = request.POST.getlist('groups')
            username = request.user.username

            groups_not_in = []
            groups_posted_to = []
            for group_id in groups:
                # Check group id format
                try:
                    group_id = int(group_id)
                except ValueError:
                    result['error'] = _(u'Error: wrong group id')
                    return HttpResponse(json.dumps(result), status=400,
                                        content_type=content_type)

                group = get_group(group_id)
                if not group:
                    result['error'] = _(u'Error: the group does not exist.')
                    return HttpResponse(json.dumps(result), status=400,
                                        content_type=content_type)

                # TODO: Check whether repo is in the group and Im in the group
                if not is_group_user(group_id, username):
                    groups_not_in.append(group.group_name)
                    continue

                # save message to group
                gm = GroupMessage(group_id=group_id, from_email=username,
                                  message=message)
                gm.save()

                # send signal
                grpmsg_added.send(sender=GroupMessage, group_id=group_id,
                                  from_email=username, message=message)

                # save attachment
                ma = MessageAttachment(group_message=gm, repo_id=repo_id,
                                       attach_type=attach_type, path=path,
                                       src='recommend')
                ma.save()

                # save discussion
                fd = FileDiscuss(group_message=gm, repo_id=repo_id, path=path)
                fd.save()

                group_url = reverse('group_discuss', args=[group_id])
                groups_posted_to.append(u'<a href="%(url)s" target="_blank">%(name)s</a>' % \
                {'url':group_url, 'name':group.group_name})

            if len(groups_posted_to) > 0:
                result['success'] = _(u'Successfully posted to %(groups)s.') % {'groups': ', '.join(groups_posted_to)}

            if len(groups_not_in) > 0:
                result['error'] = _(u'Error: you are not in group %s.') % (', '.join(groups_not_in))

        else:
            result['error'] = str(form.errors)
            return HttpResponse(json.dumps(result), status=400, content_type=content_type)

    # request.method == 'GET'
    else:
        repo_id = request.GET.get('repo_id')
        path = request.GET.get('path', None)
        repo = get_repo(repo_id)
        if not repo:
            result['error'] = _(u'Error: the library does not exist.')
            return HttpResponse(json.dumps(result), status=400, content_type=content_type)
        if path is None:
            result['error'] = _(u'Error: no path.')
            return HttpResponse(json.dumps(result), status=400, content_type=content_type)

    # get discussions & replies
    path_hash = calc_file_path_hash(path)
    discussions = FileDiscuss.objects.filter(path_hash=path_hash, repo_id=repo_id)
    msg_ids = [ e.group_message_id for e in discussions ]

    grp_msgs = GroupMessage.objects.filter(id__in=msg_ids).order_by('-timestamp')
    msg_replies = MessageReply.objects.filter(reply_to__in=grp_msgs)
    for msg in grp_msgs:
        msg.replies = []
        for reply in msg_replies:
            if msg.id == reply.reply_to_id:
                msg.replies.append(reply)
        msg.reply_cnt = len(msg.replies)
        msg.replies = msg.replies[-3:]

    ctx = {}
    ctx['messages'] = grp_msgs
    html = render_to_string("group/discussion_list.html", ctx)
    result['html'] = html
    return HttpResponse(json.dumps(result), content_type=content_type)