コード例 #1
0
ファイル: views.py プロジェクト: gnu-lorien/crapvine-online
def chronicle(request, group_slug=None, form_class=ChronicleUpdateForm, template_name="chronicles/chronicle.html"):
    chronicle = get_object_or_404(Chronicle, slug=group_slug)

    chronicle_form = form_class(request.POST or None, instance=chronicle)

    if not request.user.is_authenticated():
        is_member = False
    else:
        is_member = chronicle.user_is_member(request.user)

    action = request.POST.get("action")
    if action == "update" and chronicle_form.is_valid():
        chronicle = chronicle_form.save()
    elif action == "join":
        if not is_member:
            chronicle_member = ChronicleMember(chronicle=chronicle, user=request.user)
            chronicle.members.add(chronicle_member)
            chronicle_member.save()
            request.user.message_set.create(
                message=_("You have joined the chronicle %(chronicle_name)s") % {"chronicle_name": chronicle.name}
            )
            is_member = True
            if notification:
                notification.send(
                    [chronicle.creator], "chronicles_created_new_member", {"user": request.user, "chronicle": chronicle}
                )
                notification.send(
                    chronicle.member_users.all(),
                    "chronicles_new_member",
                    {"user": request.user, "chronicle": chronicle},
                )
        else:
            request.user.message_set.create(
                message=_("You have already joined chronicle %(chronicle_name)s") % {"chronicle_name": chronicle.name}
            )
    elif action == "leave":
        ChronicleMember.objects.filter(chronicle=chronicle, user=request.user)[0].delete()
        request.user.message_set.create(
            message="You have left the chronicle %(chronicle_name)s" % {"chronicle_name": chronicle.name}
        )
        is_member = False
        if notification:
            pass  # @@@ no notification on departure yet

    return render_to_response(
        template_name,
        {
            "chronicle_form": chronicle_form,
            "chronicle": chronicle,
            "sheets": chronicle.get_sheets_for_user(request.user),
            "group": chronicle,  # @@@ this should be the only context var for the chronicle
            "is_member": is_member,
        },
        context_instance=RequestContext(request),
    )
コード例 #2
0
ファイル: views.py プロジェクト: gnu-lorien/crapvine-online
def create(request, form_class=ChronicleForm, template_name="chronicles/create.html"):
    chronicle_form = form_class(request.POST or None)

    if chronicle_form.is_valid():
        chronicle = chronicle_form.save(commit=False)
        chronicle.creator = request.user
        chronicle.save()
        chronicle_member = ChronicleMember(chronicle=chronicle, user=request.user, membership_role=0)
        chronicle.members.add(chronicle_member)
        chronicle_member.save()
        if notification:
            # @@@ might be worth having a shortcut for sending to all users
            notification.send(User.objects.all(), "chronicles_new_chronicle", {"chronicle": chronicle}, queue=True)
        return HttpResponseRedirect(chronicle.get_absolute_url())

    return render_to_response(
        template_name, {"chronicle_form": chronicle_form}, context_instance=RequestContext(request)
    )