Ejemplo n.º 1
0
    def get_queryset(self):
        vars = self.request.GET
        sort = vars.get('sort', None)
        if sort:
            if sort not in self.model._meta.get_all_field_names():
                raise Http404("%s is not a valid sorting field" % sort)

        self.owner = get_object_or_404(User, username=self.kwargs.get("owner"))
        owner_lookup = dict([(self.owner_field, self.owner)])
        list = self.model.objects.filter(**owner_lookup)

        if self.permission:
            f = PermissionsRegistry.get_filter(self.permission,
                                               self.request.user)
            list = list.filter(f)

        if self.related:
            list = list.select_related(*self.related)

        if self.defer:
            list = list.defer(*self.defer)
        if sort:
            dir = vars.get('dir', "desc")
            order_by = (dir == "desc" and "-" or "") + sort
            list = list.order_by(order_by)

        return list
Ejemplo n.º 2
0
    def get_queryset(self):
        vars = self.request.GET
        sort = vars.get('sort', None)
        if sort:
            if sort not in self.model._meta.get_all_field_names():
                raise Http404("%s is not a valid sorting field" % sort)

        self.owner = get_object_or_404(User, username=self.kwargs.get("owner"))
        owner_lookup = dict([(self.owner_field, self.owner)])
        list = self.model.objects.filter(**owner_lookup)

        if self.permission:
            f = PermissionsRegistry.get_filter(self.permission,
                                               self.request.user)
            list = list.filter(f)

        if self.related:
            list = list.select_related(*self.related)

        if self.defer:
            list = list.defer(*self.defer)
        if sort:
            dir = vars.get('dir', "desc")
            order_by = (dir == "desc" and "-" or "") + sort
            list = list.order_by(order_by)

        return list
def user_counts(context, target_user):
    request = context["request"]
    exhibit_count = PublishedExhibit.objects.filter(owner=target_user).filter(PermissionsRegistry.get_filter("exhibit.can_view", request.user)).count()

    return {
        "target_user": target_user,
        "exhibit_count": exhibit_count,
    }
Ejemplo n.º 4
0
def user_counts(context, target_user):
    request = context["request"]
    exhibit_count = PublishedExhibit.objects.filter(owner=target_user).filter(
        PermissionsRegistry.get_filter("exhibit.can_view",
                                       request.user)).count()

    return {
        "target_user": target_user,
        "exhibit_count": exhibit_count,
    }
Ejemplo n.º 5
0
    def get_queryset(self):
        vars = self.request.GET
        sort = vars.get('sort', None)
        if sort:
            if sort not in models.PublishedExhibit._meta.get_all_field_names():
                raise Http404("%s is not a valid sorting field"%sort)

        perm_filter = PermissionsRegistry.get_filter("exhibit.can_view", self.request.user)
        list = models.PublishedExhibit.objects.all().filter(perm_filter)
        list = list.select_related("owner")
        if sort:
            dir = vars.get('dir', "desc")
            order_by = (dir=="desc" and "-" or "") + sort
            list = list.order_by(order_by)

        return list
Ejemplo n.º 6
0
    def get_queryset(self):
        vars = self.request.GET
        sort = vars.get('sort', None)
        if sort:
            if sort not in PublishedExhibit._meta.get_all_field_names():
                raise Http404("%s is not a valid sorting field"%sort)

        user = get_object_or_404(User, username=self.kwargs.get("username"))

        pre_friend_ids = [i['friend'].id for i in
                          Friendship.objects.friends_for_user(user)]
        perm_filter = PermissionsRegistry.get_filter("exhibit.can_view", self.request.user)
        list = PublishedExhibit.objects.filter(owner__pk__in=pre_friend_ids).filter(perm_filter)
        list = list.select_related("owner")
        if sort:
            dir = vars.get('dir', "desc")
            order_by = (dir=="desc" and "-" or "") + sort
            list = list.order_by(order_by)

        return list
Ejemplo n.º 7
0
    def get_queryset(self):
        vars = self.request.GET
        sort = vars.get('sort', None)
        if sort:
            if sort not in PublishedExhibit._meta.get_all_field_names():
                raise Http404("%s is not a valid sorting field" % sort)

        user = get_object_or_404(User, username=self.kwargs.get("username"))

        pre_friend_ids = [
            i['friend'].id for i in Friendship.objects.friends_for_user(user)
        ]
        perm_filter = PermissionsRegistry.get_filter("exhibit.can_view",
                                                     self.request.user)
        list = PublishedExhibit.objects.filter(
            owner__pk__in=pre_friend_ids).filter(perm_filter)
        list = list.select_related("owner")
        if sort:
            dir = vars.get('dir', "desc")
            order_by = (dir == "desc" and "-" or "") + sort
            list = list.order_by(order_by)

        return list
Ejemplo n.º 8
0
    def items(self, obj):
        u = AnonymousUser()
        filter = PermissionsRegistry.get_filter('exhibit.can_view', u)

        return PublishedExhibit.objects.filter(owner=obj).filter(filter).order_by('-created')[:10]
Ejemplo n.º 9
0
    def items(self, obj):
        u = AnonymousUser()
        filter = PermissionsRegistry.get_filter('exhibit.can_view', u)

        return PublishedExhibit.objects.filter(
            owner=obj).filter(filter).order_by('-created')[:10]
Ejemplo n.º 10
0
def profile(request, username, template_name="profiles/profile.html", extra_context=None):

    if extra_context is None:
        extra_context = {}

    other_user = get_object_or_404(User, username=username, is_active=True)

    if request.user.is_authenticated():
        is_friend = Friendship.objects.are_friends(request.user, other_user)
        other_friends = Friendship.objects.friends_for_user(other_user)
        if request.user == other_user:
            is_me = True
        else:
            is_me = False
        objects = FriendshipInvitation.objects
        previous_invitations_to = objects.invitations(to_user=other_user, from_user=request.user)
        previous_invitations_from = objects.invitations(to_user=request.user, from_user=other_user)
    else:
        other_friends = []
        is_friend = False
        is_me = False

        previous_invitations_to = None
        previous_invitations_from = None

    if is_friend:
        invite_form = None
        if request.method == "POST":
            if request.POST.get("action") == "remove":
                Friendship.objects.remove(request.user, other_user)
                send_message(
                    request, _("You have removed %(from_user)s" " from your connections"), {"from_user": other_user}
                )

                is_friend = False
                invite_form = InviteFriendForm(
                    request.user, {"to_user": username, "message": ugettext("Let's Connect!")}
                )

    else:
        if request.user.is_authenticated() and request.method == "POST":
            if request.POST.get("action") == "invite":
                invite_form = InviteFriendForm(request.user, request.POST)
                if invite_form.is_valid():
                    invite_form.save()
                    send_message(request, _("Connection requested with %s"), invite_form.cleaned_data["to_user"])
            else:
                invite_form = InviteFriendForm(
                    request.user, {"to_user": username, "message": ugettext("Let's be Connect!")}
                )
                invitation_id = request.POST.get("invitation", None)

                friends_for_user = Friendship.objects.friends_for_user
                invitations = FriendshipInvitation.objects
                if request.POST.get("action") == "accept":
                    try:
                        invitation = invitations.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.accept()
                            message = _("You have accepted the connection " "request from %(from_user)s")
                            send_message(request, message, {"from_user": invitation.from_user})

                            is_friend = True
                            other_friends = friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
                elif request.POST.get("action") == "decline":
                    try:
                        invitation = invitations.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.decline()
                            send_message(
                                request,
                                _("You have declined the connection " "request from %(from_user)s"),
                                {"from_user": invitation.from_user},
                            )
                            other_friends = friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
        else:
            invite_form = InviteFriendForm(request.user, {"to_user": username, "message": ugettext("Let's Connect!")})
    drafts = None
    if is_me:
        drafts = (
            DraftExhibit.objects.filter(owner=other_user)
            .filter(parent__isnull=True)
            .select_related("source")
            .order_by("-modified")
        )

    exhibit_filter = PermissionsRegistry.get_filter("exhibit.can_view", request.user)
    exhibits = PublishedExhibit.objects.filter(owner=other_user).filter(exhibit_filter)

    return render_to_response(
        template_name,
        dict(
            {
                "is_me": is_me,
                "is_friend": is_friend,
                "other_user": other_user,
                "exhibits": exhibits,
                "drafts": drafts,
                "other_friends": other_friends,
                "invite_form": invite_form,
                "previous_invitations_to": previous_invitations_to,
                "previous_invitations_from": previous_invitations_from,
            },
            **extra_context
        ),
        context_instance=RequestContext(request),
    )
Ejemplo n.º 11
0
def profile(request, username, template_name="profiles/profile.html",
            extra_context=None):

    if extra_context is None:
        extra_context = {}

    other_user = get_object_or_404(User, username=username, is_active=True)

    if request.user.is_authenticated():
        is_friend = Friendship.objects.are_friends(request.user, other_user)
        other_friends = Friendship.objects.friends_for_user(other_user)
        if request.user == other_user:
            is_me = True
        else:
            is_me = False
        objects = FriendshipInvitation.objects
        previous_invitations_to = objects.invitations(to_user=other_user,
                                                      from_user=request.user)
        previous_invitations_from = objects.invitations(to_user=request.user,
                                                        from_user=other_user)
    else:
        other_friends = []
        is_friend = False
        is_me = False

        previous_invitations_to = None
        previous_invitations_from = None

    if is_friend:
        invite_form = None
        if request.method == "POST":
            if request.POST.get("action") == "remove":
                Friendship.objects.remove(request.user, other_user)
                send_message(request,
                             _("You have removed %(from_user)s"
                               " from your connections"),
                             {'from_user': other_user})

                is_friend = False
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's Connect!"),
                })

    else:
        if request.user.is_authenticated() and request.method == "POST":
            if request.POST.get("action") == "invite":
                invite_form = InviteFriendForm(request.user, request.POST)
                if invite_form.is_valid():
                    invite_form.save()
                    send_message(request,
                                 _("Connection requested with %s"),
                                 invite_form.cleaned_data["to_user"])
            else:
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's be Connect!"),
                })
                invitation_id = request.POST.get("invitation", None)

                friends_for_user = Friendship.objects.friends_for_user
                invitations = FriendshipInvitation.objects
                if request.POST.get("action") == "accept":
                    try:
                        invitation = invitations.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.accept()
                            message = _("You have accepted the connection "
                                        "request from %(from_user)s")
                            send_message(request,
                                         message,
                                         {'from_user': invitation.from_user})

                            is_friend = True
                            other_friends = friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
                elif request.POST.get("action") == "decline":
                    try:
                        invitation = invitations.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.decline()
                            send_message(request,
                                         _("You have declined the connection "
                                           "request from %(from_user)s"),
                                         {'from_user': invitation.from_user})
                            other_friends = friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
        else:
            invite_form = InviteFriendForm(request.user, {
                'to_user': username,
                'message': ugettext("Let's Connect!"),
            })
    drafts = None
    if is_me:
        drafts = (DraftExhibit.objects.filter(owner=other_user)
                                      .filter(parent__isnull=True)
                                      .select_related("source")
                                      .order_by("-modified"))

    exhibit_filter = PermissionsRegistry.get_filter("exhibit.can_view",
                                                    request.user)
    exhibits = (PublishedExhibit.objects
                                .filter(owner=other_user)
                                .filter(exhibit_filter))

    return render_to_response(template_name, dict({
        "is_me": is_me,
        "is_friend": is_friend,
        "other_user": other_user,
        "exhibits": exhibits,
        "drafts": drafts,
        "other_friends": other_friends,
        "invite_form": invite_form,
        "previous_invitations_to": previous_invitations_to,
        "previous_invitations_from": previous_invitations_from,
    }, **extra_context), context_instance=RequestContext(request))