def user_counts(context, target_user):
    request = context["request"]
    exhibit_count = target_user.exhibits.filter(PermissionsRegistry.get_filter("exhibit.can_view", request.user)).count()
    dataset_count = target_user.datasets.filter(PermissionsRegistry.get_filter("dataset.can_view", request.user)).count()

    return {
        "target_user": target_user,
        "exhibit_count": exhibit_count,
        "dataset_count": dataset_count
    }
Пример #2
0
    def get(self, request, *args, **kwargs):
        dataset = self.get_object()
        ds_kwargs = {"owner": dataset.owner.username, "slug": dataset.slug}
        context = {
            "dataset": dataset,
            "dataurl": reverse("dataset_data_json", kwargs=ds_kwargs),
            "profileurl": reverse("dataset_profile_json", kwargs=ds_kwargs),
            "cancel_url": reverse("dataset_summary", kwargs=ds_kwargs),
            "save_url": reverse("dataset_edit", kwargs=ds_kwargs),
        }

        user = self.request.user
        filter = PermissionsRegistry.get_filter("dataset.can_view", user)

        context["can_view"] = (user.has_perm("dataset.can_view", dataset),)
        context["can_inspect"] = (user.has_perm("dataset.can_inspect", dataset),)

        context["can_build"] = user.has_perm("dataset.can_build", dataset)
        context["can_edit"] = user.has_perm("dataset.can_edit", dataset)
        context["can_delete"] = user.has_perm("dataset.can_delete", dataset)
        context["exhibits"] = dataset.exhibits.filter(filter)

        response = render(request, self.template_name, context)

        return response
Пример #3
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:
            list = list.filter(PermissionsRegistry.get_filter(self.permission, self.request.user))

        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
Пример #4
0
    def get(self, request, *args, **kwargs):
        dataset = self.get_object()
        ds_kwargs = {'owner': dataset.owner.username, 'slug': dataset.slug}
        context = {
            "dataset": dataset,
            "dataurl": reverse('dataset_data_json', kwargs=ds_kwargs),
            "profileurl": reverse('dataset_profile_json', kwargs=ds_kwargs),
            "cancel_url": reverse('dataset_summary', kwargs=ds_kwargs),
            "save_url": reverse('dataset_edit', kwargs=ds_kwargs),
            "dataset_transaction_status_url": reverse(
                "datasource_transaction_status",
                kwargs={"uuid": dataset.source.uuid})
        }

        user = self.request.user
        filter = PermissionsRegistry.get_filter("dataset.can_view", user)

        context["can_view"] = user.has_perm("dataset.can_view", dataset),
        context["can_inspect"] = user.has_perm("dataset.can_inspect", dataset),

        context["can_build"] = user.has_perm("dataset.can_build", dataset)
        context["can_edit"] = user.has_perm("dataset.can_edit", dataset)
        context["can_delete"] = user.has_perm("dataset.can_delete", dataset)
        context["exhibits"] = dataset.exhibits.filter(filter)

        response = render(request, self.template_name, context)

        return response
Пример #5
0
    def get(self, request, *args, **kwargs):
        dataset = self.get_object()
        ds_kwargs = {'owner': dataset.owner.username, 'slug': dataset.slug}
        context = {
            "dataset": dataset,
            "dataurl": reverse('dataset_data_json', kwargs=ds_kwargs),
            "profileurl": reverse('dataset_profile_json', kwargs=ds_kwargs),
            "cancel_url": reverse('dataset_summary', kwargs=ds_kwargs),
            "save_url": reverse('dataset_edit', kwargs=ds_kwargs),
        }

        user = self.request.user
        filter = PermissionsRegistry.get_filter("dataset.can_view", user)

        context["can_view"] = user.has_perm("dataset.can_view", dataset),
        context["can_inspect"] = user.has_perm("dataset.can_inspect", dataset),

        context["can_build"] = user.has_perm("dataset.can_build", dataset)
        context["can_edit"] = user.has_perm("dataset.can_edit", dataset)
        context["can_delete"] = user.has_perm("dataset.can_delete", dataset)
        context["exhibits"] = dataset.exhibits.filter(filter)

        response = render(request, self.template_name, context)

        return response
Пример #6
0
    def get_queryset(self):
        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)
        return Exhibit.objects.filter(owner__pk__in=pre_friend_ids).filter(perm_filter)
Пример #7
0
def dataset_list_item(context, dataset):
    request = context["request"]
    user = request.user

    can_edit = user.has_perm("dataset.can_edit", dataset)
    can_delete = user.has_perm("dataset.can_delete", dataset)
    can_view = user.has_perm("dataset.can_view", dataset)
    can_inspect = user.has_perm("dataset.can_inspect", dataset)

    can_build = user.has_perm("dataset.can_build", dataset)

    return {
        "dataset":
        dataset,
        "dataset_url":
        dataset.get_absolute_url(),
        "exhibits":
        dataset.exhibits.filter(
            PermissionsRegistry.get_filter("exhibit.can_view", user)),
        "request":
        request,
        "can_view":
        can_view,
        "can_edit":
        can_edit,
        "can_delete":
        can_delete,
        "can_build":
        can_build,
        "can_inspect":
        can_inspect
    }
Пример #8
0
    def get_context_data(self, **kwargs):
        context = dict(super(DetailView, self).get_context_data(**kwargs))
        source = self.get_object()
        user = self.request.user
        filter = PermissionsRegistry.get_filter("datasource.can_view", user)

        context["can_view"] = user.has_perm("datasource.can_view", source),
        context["can_inspect"] = user.has_perm("datasource.can_inspect", source),

        context["can_build"] = user.has_perm("datasource.can_build", source)
        context["can_edit"] = user.has_perm("datasource.can_edit", source)
        context["can_delete"] = user.has_perm("datasource.can_delete", source)

        try:
            context["can_refresh"] = user.has_perm("datasource.can_edit", source)
        except ObjectDoesNotExist, ex:
            pass
Пример #9
0
    def get_context_data(self, **kwargs):
        context = dict(super(DatasetView, self).get_context_data(**kwargs))
        dataset = self.get_object(models.Dataset.objects.select_related("source"))
        user = self.request.user
        filter = PermissionsRegistry.get_filter("dataset.can_view", user)

        context["can_view"] = user.has_perm("dataset.can_view", dataset),
        context["can_inspect"] = user.has_perm("dataset.can_inspect", dataset),

        context["can_build"] = user.has_perm("dataset.can_build", dataset)
        context["can_edit"] = user.has_perm("dataset.can_edit", dataset)
        context["can_delete"] = user.has_perm("dataset.can_delete", dataset)

        try:
            source = dataset.source
            context["can_refresh"] = user.has_perm("datasource.can_edit", source)
        except ObjectDoesNotExist, ex:
            pass
Пример #10
0
    def get_context_data(self, **kwargs):
        context = dict(super(DatasetView, self).get_context_data(**kwargs))
        dataset = self.get_object()
        user = self.request.user
        filter = PermissionsRegistry.get_filter("dataset.can_view", user)

        context["can_view"] = user.has_perm("dataset.can_view", dataset),
        context["can_inspect"] = user.has_perm("dataset.can_inspect", dataset),

        context["can_build"] = user.has_perm("dataset.can_build", dataset)
        context["can_edit"] = user.has_perm("dataset.can_edit", dataset)
        context["can_delete"] = user.has_perm("dataset.can_delete", dataset)

        try:
            source = dataset.source
            context["can_refresh"] = user.has_perm("datasource.can_edit",
                                                   source)
        except ObjectDoesNotExist, ex:
            pass
Пример #11
0
    def get_queryset(self):
        vars = self.request.GET
        sort = vars.get('sort', None)
        if sort:
            if sort not in Exhibit._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 = Exhibit.objects.filter(owner__pk__in=pre_friend_ids).filter(perm_filter)
        list = list.select_related("owner", "dataset", "dataset__owner")
        if sort:
            dir = vars.get('dir', "desc")
            order_by = (dir=="desc" and "-" or "") + sort
            list = list.order_by(order_by)

        return list
Пример #12
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)


        owner = get_object_or_404(User, username=self.kwargs.get("owner"))
        list = self.model.objects.filter(owner=owner)


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

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

        return list
Пример #13
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)

        owner = get_object_or_404(User, username=self.kwargs.get("owner"))
        list = self.model.objects.filter(owner=owner)

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

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

        return list
Пример #14
0
def dataset_list_item(context, dataset):
    request = context["request"]
    user = request.user

    can_edit = user.has_perm("dataset.can_edit", dataset)
    can_delete = user.has_perm("dataset.can_delete", dataset)
    can_view = user.has_perm("dataset.can_view", dataset)
    can_inspect = user.has_perm("dataset.can_inspect", dataset)

    can_build = user.has_perm("dataset.can_build", dataset)

    return {"dataset": dataset,
            "dataset_url": dataset.get_absolute_url(),
            "exhibits": dataset.exhibits.filter(PermissionsRegistry.get_filter("exhibit.can_view", user)),
            "request": request,
            "can_view": can_view,
            "can_edit": can_edit,
            "can_delete": can_delete,
            "can_build": can_build,
            "can_inspect": can_inspect
            }
Пример #15
0
    def get_queryset(self):
        perm_filter = PermissionsRegistry.get_filter("exhibit.can_view",
                                                     self.request.user)

        return models.Exhibit.objects.filter(
            dataset=self.get_dataset()).filter(perm_filter)
Пример #16
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)

    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
        previous_invitations_to = FriendshipInvitation.objects.invitations(to_user=other_user, from_user=request.user)
        previous_invitations_from = FriendshipInvitation.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": # @@@ perhaps the form should just post to friends and be redirected here
                Friendship.objects.remove(request.user, other_user)
                request.user.message_set.create(message=_("You have removed %(from_user)s from friends") % {'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": # @@@ perhaps the form should just post to friends and be redirected here
                invite_form = InviteFriendForm(request.user, request.POST)
                if invite_form.is_valid():
                    invite_form.save()
            else:
                invite_form = InviteFriendForm(request.user, {
                    'to_user': username,
                    'message': ugettext("Let's be Connect!"),
                })
                invitation_id = request.POST.get("invitation", None)
                if request.POST.get("action") == "accept": # @@@ perhaps the form should just post to friends and be redirected here
                    try:
                        invitation = FriendshipInvitation.objects.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.accept()
                            request.user.message_set.create(message=_("You have accepted the connection request from %(from_user)s") % {'from_user': invitation.from_user})
                            is_friend = True
                            other_friends = Friendship.objects.friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
                elif request.POST.get("action") == "decline": # @@@ perhaps the form should just post to friends and be redirected here
                    try:
                        invitation = FriendshipInvitation.objects.get(id=invitation_id)
                        if invitation.to_user == request.user:
                            invitation.decline()
                            request.user.message_set.create(message=_("You have declined the connection request from %(from_user)s") % {'from_user': invitation.from_user})
                            other_friends = Friendship.objects.friends_for_user(other_user)
                    except FriendshipInvitation.DoesNotExist:
                        pass
        else:
            invite_form = InviteFriendForm(request.user, {
                'to_user': username,
                'message': ugettext("Let's Connect!"),
            })

    return render_to_response(template_name, dict({
        "is_me": is_me,
        "is_friend": is_friend,
        "other_user": other_user,
        "datasets": other_user.datasets.filter(PermissionsRegistry.get_filter("dataset.can_view", request.user)),
        "exhibits": other_user.exhibits.filter(PermissionsRegistry.get_filter("exhibit.can_view", request.user)),
        "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))
Пример #17
0
    def get_queryset(self):
        perm_filter = PermissionsRegistry.get_filter("exhibit.can_view", self.request.user)

        return models.Exhibit.objects.filter(dataset=self.get_dataset()).filter(perm_filter)
Пример #18
0
def permission_filter(queryset, permission, user):
    return queryset.filter(PermissionsRegistry.get_filter(permission, user))
Пример #19
0
 def items(self, obj):
     filter = PermissionsRegistry.get_filter('dataset.can_view', AnonymousUser())
     return Dataset.objects.filter(owner=obj).filter(filter).order_by('-created')[:10]
Пример #20
0
    def items(self, obj):
        u = AnonymousUser()
        filter = PermissionsRegistry.get_filter('exhibit.can_view', u)

        return Exhibit.objects.filter(owner=obj).filter(filter).order_by('-created')[:10]
Пример #21
0
    def items(self):
        u = AnonymousUser()
        filter = PermissionsRegistry.get_filter('dataset.can_view', u)

        return Dataset.objects.filter(filter).order_by('-created')[:10]
Пример #22
0
def permission_filter(queryset, permission, user):
    return queryset.filter(PermissionsRegistry.get_filter(permission, user))
Пример #23
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!"),
            })
    pending_datasets = None
    if is_me:
        pending_datasets = DataSource.objects\
                                     .filter(owner=other_user)\
                                     .filter(dataset__isnull=True)\
                                     .order_by("-modified")

    dataset_filter = PermissionsRegistry.get_filter("dataset.can_view",
                                                    request.user)
    datasets = other_user.datasets.filter(dataset_filter)
    datasets = datasets.select_related("owner")

    exhibit_filter = PermissionsRegistry.get_filter("exhibit.can_view",
                                                    request.user)
    exhibits = other_user.exhibits.filter(exhibit_filter)
    exhibits = exhibits.select_related("owner", "dataset__owner", "dataset")

    return render_to_response(template_name, dict({
        "is_me": is_me,
        "is_friend": is_friend,
        "other_user": other_user,
        "datasets": datasets,
        "exhibits": exhibits,
        "pending_datasets": pending_datasets,
        "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))
Пример #24
0
 def get_queryset(self):
     owner = get_object_or_404(User, username=self.kwargs.get("owner"))
     list = self.model.objects.filter(owner=owner)
     if self.permission:
         list = list.filter(PermissionsRegistry.get_filter(self.permission, self.request.user))
     return list