def password(request, id, name):

    presentation = get_object_or_404(
        filter_by_access(request.user, Presentation).filter(
            Presentation.published_q(request.user), id=id))

    class PasswordForm(forms.Form):
        password = forms.CharField(widget=forms.PasswordInput)

        def clean_password(self):
            p = self.cleaned_data.get('password')
            if p != presentation.password:
                raise forms.ValidationError("Password is not correct.")
            return p

    if request.method == 'POST':
        form = PasswordForm(request.POST)
        if form.is_valid():
            request.session.setdefault(
                'passwords',
                dict())[presentation.id] = form.cleaned_data.get('password')
            request.session.modified = True
            return HttpResponseRedirect(
                request.GET.get('next', reverse('presentation-browse')))
    else:
        form = PasswordForm()

    return render_to_response(
        'presentation_password.html', {
            'form': form,
            'presentation': presentation,
            'next': request.GET.get('next', reverse('presentation-browse')),
        },
        context_instance=RequestContext(request))
Beispiel #2
0
def password(request, id, name):

    presentation = get_object_or_404(
        filter_by_access(request.user, Presentation)
        .filter(Presentation.published_q(request.user), id=id)
    )

    class PasswordForm(forms.Form):
        password = forms.CharField(widget=forms.PasswordInput)

        def clean_password(self):
            p = self.cleaned_data.get('password')
            if p != presentation.password:
                raise forms.ValidationError("Password is not correct.")
            return p

    if request.method == 'POST':
        form = PasswordForm(request.POST)
        if form.is_valid():
            request.session.setdefault(
                'passwords', dict()
            )[presentation.id] = form.cleaned_data.get('password')
            request.session.modified = True
            return HttpResponseRedirect(
                request.GET.get('next', reverse('presentation-browse')))
    else:
        form = PasswordForm()

    return render_to_response(
        'presentation_password.html',
        {
            'form': form,
            'presentation': presentation,
            'next': request.GET.get('next', reverse('presentation-browse')),
        },
        context_instance=RequestContext(request)
    )
Beispiel #3
0
def browse(request, manage=False):

    if manage and not request.user.is_authenticated():
        raise Http404()

    if request.user.is_authenticated() and not request.GET.items():
        # retrieve past settings
        qs = load_settings(
            request.user, filter='presentation_browse_querystring')
        if 'presentation_browse_querystring' in qs:

            # Don't restore the "untagged only" setting, as it confuses
            # a lot of users
            args = qs['presentation_browse_querystring'][0]
            args = '&'.join(
                p for p in args.split('&')
                if not p.startswith('ut=')
            )

            return HttpResponseRedirect(
                '%s?%s' % (
                    reverse('presentation-manage'
                            if manage else 'presentation-browse'),
                    args,
                )
            )

    presenter = request.GET.get('presenter')
    tags = filter(None, request.GET.getlist('t'))
    sortby = request.GET.get('sortby')
    if sortby not in ('title', 'created', 'modified'):
        sortby = 'title'
    untagged = 1 if request.GET.get('ut') else 0
    if untagged:
        tags = []
    remove_tag = request.GET.get('rt')
    if remove_tag and remove_tag in tags:
        tags.remove(remove_tag)
    keywords = request.GET.get('kw', '')
    get = request.GET.copy()
    get.setlist('t', tags)
    if 'rt' in get:
        del get['rt']
    if untagged:
        get['ut'] = '1'
    elif 'ut' in get:
        del get['ut']

    if untagged and request.user.is_authenticated():
        qs = TaggedItem.objects.filter(
            content_type=OwnedWrapper.t(OwnedWrapper)
        ).values('object_id').distinct()
        qs = OwnedWrapper.objects.filter(
            user=request.user,
            content_type=OwnedWrapper.t(Presentation),
            id__in=qs
        ).values('object_id')
        q = ~Q(id__in=qs)
    elif tags:
        qs = OwnedWrapper.objects.filter(
            content_type=OwnedWrapper.t(Presentation))
        # get list of matching IDs for each individual tag, since tags
        # may be attached by different owners
        ids = [
            list(
                TaggedItem.objects
                .get_by_model(qs, '"%s"' % tag)
                .values_list('object_id', flat=True)
            ) for tag in tags
        ]
        q = Q(*(Q(id__in=x) for x in ids))
    else:
        q = Q()

    if presenter:
        presenter = User.objects.get(username=presenter)
        qp = Q(owner=presenter)
    else:
        qp = Q()

    if keywords:
        qk = Q(*(Q(title__icontains=kw) | Q(description__icontains=kw) |
                 Q(owner__last_name__icontains=kw) |
                 Q(owner__first_name__icontains=kw) |
                 Q(owner__username__icontains=kw) for kw in keywords.split()))
    else:
        qk = Q()

    if manage:
        qv = Q()
        presentations = filter_by_access(
            request.user, Presentation, write=True, manage=True)
    else:
        qv = Presentation.published_q()
        presentations = filter_by_access(request.user, Presentation)

    presentations = presentations.select_related('owner').filter(q, qp, qk, qv)
    presentations = presentations.order_by(
        '-' + sortby if sortby != 'title' else sortby)

    if request.method == "POST":

        if manage and (
            request.POST.get('hide') or request.POST.get('unhide')
        ) and request.user.has_perm('presentation.publish_presentations'):
            hide = request.POST.get('hide') or False
            ids = map(int, request.POST.getlist('h'))
            for presentation in Presentation.objects.filter(
                    owner=request.user, id__in=ids):
                presentation.hidden = hide
                presentation.save()

        if manage and request.POST.get('delete'):
            ids = map(int, request.POST.getlist('h'))
            Presentation.objects.filter(
                owner=request.user, id__in=ids).delete()

        get['kw'] = request.POST.get('kw')
        if get['kw'] != request.POST.get('okw') and 'page' in get:
            # user entered keywords, reset page counter
            del get['page']

        if request.POST.get('update_tags'):
            ids = map(int, request.POST.getlist('h'))
            update_actionbar_tags(request, *presentations.filter(id__in=ids))

        # check for clicks on "add selected items" buttons
        for button in filter(
                lambda k: k.startswith('add-selected-items-'),
                request.POST.keys()):
            id = int(button[len('add-selected-items-'):])
            presentation = get_object_or_404(
                filter_by_access(
                    request.user, Presentation, write=True, manage=True)
                .filter(id=id)
            )
            add_selected_items(request, presentation)
            return HttpResponseRedirect(
                reverse(
                    'presentation-edit',
                    args=(presentation.id, presentation.name)
                )
            )

        return HttpResponseRedirect(request.path + '?' + get.urlencode())

    active_tags = tags

    def col(model, field):
        qn = connection.ops.quote_name
        return '%s.%s' % (
            qn(model._meta.db_table),
            qn(model._meta.get_field(field).column)
        )

    if presentations:
        q = OwnedWrapper.objects.extra(
            tables=(Presentation._meta.db_table,),
            where=(
                '%s=%s' % (
                    col(OwnedWrapper, 'object_id'),
                    col(Presentation, 'id')
                ),
                '%s=%s' % (
                    col(OwnedWrapper, 'user'),
                    col(Presentation, 'owner')
                )
            )
        ).filter(
            object_id__in=presentations.values('id'),
            content_type=OwnedWrapper.t(Presentation)
        )
        tags = Tag.objects.usage_for_queryset(q, counts=True)

        if not manage:
            for p in presentations:
                p.verify_password(request)
    else:
        tags = ()

    if presentations and request.user.is_authenticated():
        usertags = Tag.objects.usage_for_queryset(
            OwnedWrapper.objects.filter(
                user=request.user,
                object_id__in=presentations.values('id'),
                content_type=OwnedWrapper.t(Presentation)
            ),
            counts=True
        )
    else:
        usertags = ()

    presenters = User.objects.filter(presentation__in=presentations) \
        .annotate(presentations=Count('presentation')) \
        .order_by('last_name', 'first_name')

    if request.user.is_authenticated() and presentations:
        # save current settings
        querystring = request.GET.urlencode()
        store_settings(
            request.user, 'presentation_browse_querystring', querystring)

    return render_to_response(
        'presentation_browse.html',
        {
            'manage': manage,
            'tags': tags if len(tags) > 0 else None,
            'untagged': untagged,
            'usertags': usertags if len(usertags) > 0 else None,
            'active_tags': active_tags,
            'active_presenter': presenter,
            'presentations': presentations,
            'presenters': presenters if len(presenters) > 1 else None,
            'keywords': keywords,
            'sortby': sortby,
        },
        context_instance=RequestContext(request)
    )
def browse(request, manage=False):

    if manage and not request.user.is_authenticated():
        raise Http404()

    if request.user.is_authenticated() and not request.GET.items():
        # retrieve past settings
        qs = load_settings(request.user,
                           filter='presentation_browse_querystring')
        if 'presentation_browse_querystring' in qs:

            # Don't restore the "untagged only" setting, as it confuses
            # a lot of users
            args = qs['presentation_browse_querystring'][0]
            args = '&'.join(p for p in args.split('&')
                            if not p.startswith('ut='))

            return HttpResponseRedirect('%s?%s' % (
                reverse('presentation-manage'
                        if manage else 'presentation-browse'),
                args,
            ))

    presenter = request.GET.get('presenter')
    tags = filter(None, request.GET.getlist('t'))
    sortby = request.GET.get('sortby')
    if sortby not in ('title', 'created', 'modified'):
        sortby = 'title'
    untagged = 1 if request.GET.get('ut') else 0
    if untagged:
        tags = []
    remove_tag = request.GET.get('rt')
    if remove_tag and remove_tag in tags:
        tags.remove(remove_tag)
    keywords = request.GET.get('kw', '')
    get = request.GET.copy()
    get.setlist('t', tags)
    if 'rt' in get:
        del get['rt']
    if untagged:
        get['ut'] = '1'
    elif 'ut' in get:
        del get['ut']

    if untagged and request.user.is_authenticated():
        qs = TaggedItem.objects.filter(content_type=OwnedWrapper.t(
            OwnedWrapper)).values('object_id').distinct()
        qs = OwnedWrapper.objects.filter(
            user=request.user,
            content_type=OwnedWrapper.t(Presentation),
            id__in=qs).values('object_id')
        q = ~Q(id__in=qs)
    elif tags:
        qs = OwnedWrapper.objects.filter(
            content_type=OwnedWrapper.t(Presentation))
        # get list of matching IDs for each individual tag, since tags
        # may be attached by different owners
        ids = [
            list(
                TaggedItem.objects.get_by_model(qs, '"%s"' % tag).values_list(
                    'object_id', flat=True)) for tag in tags
        ]
        q = Q(*(Q(id__in=x) for x in ids))
    else:
        q = Q()

    if presenter:
        presenter = User.objects.get(username=presenter)
        qp = Q(owner=presenter)
    else:
        qp = Q()

    if keywords:
        qk = Q(*(Q(title__icontains=kw) | Q(description__icontains=kw)
                 | Q(owner__last_name__icontains=kw)
                 | Q(owner__first_name__icontains=kw)
                 | Q(owner__username__icontains=kw)
                 for kw in keywords.split()))
    else:
        qk = Q()

    if manage:
        qv = Q()
        presentations = filter_by_access(request.user,
                                         Presentation,
                                         write=True,
                                         manage=True)
    else:
        qv = Presentation.published_q()
        presentations = filter_by_access(request.user, Presentation)

    presentations = presentations.select_related('owner').filter(q, qp, qk, qv)
    presentations = presentations.order_by(
        '-' + sortby if sortby != 'title' else sortby)

    if request.method == "POST":

        if manage and (
                request.POST.get('hide') or request.POST.get('unhide')
        ) and request.user.has_perm('presentation.publish_presentations'):
            hide = request.POST.get('hide') or False
            ids = map(int, request.POST.getlist('h'))
            for presentation in Presentation.objects.filter(owner=request.user,
                                                            id__in=ids):
                presentation.hidden = hide
                presentation.save()

        if manage and request.POST.get('delete'):
            ids = map(int, request.POST.getlist('h'))
            Presentation.objects.filter(owner=request.user,
                                        id__in=ids).delete()

        get['kw'] = request.POST.get('kw')
        if get['kw'] != request.POST.get('okw') and 'page' in get:
            # user entered keywords, reset page counter
            del get['page']

        if request.POST.get('update_tags'):
            ids = map(int, request.POST.getlist('h'))
            update_actionbar_tags(request, *presentations.filter(id__in=ids))

        # check for clicks on "add selected items" buttons
        for button in filter(lambda k: k.startswith('add-selected-items-'),
                             request.POST.keys()):
            id = int(button[len('add-selected-items-'):])
            presentation = get_object_or_404(
                filter_by_access(request.user,
                                 Presentation,
                                 write=True,
                                 manage=True).filter(id=id))
            add_selected_items(request, presentation)
            return HttpResponseRedirect(
                reverse('presentation-edit',
                        args=(presentation.id, presentation.name)))

        return HttpResponseRedirect(request.path + '?' + get.urlencode())

    active_tags = tags

    def col(model, field):
        qn = connection.ops.quote_name
        return '%s.%s' % (qn(
            model._meta.db_table), qn(model._meta.get_field(field).column))

    if presentations:
        q = OwnedWrapper.objects.extra(
            tables=(Presentation._meta.db_table, ),
            where=('%s=%s' %
                   (col(OwnedWrapper, 'object_id'), col(Presentation, 'id')),
                   '%s=%s' % (col(OwnedWrapper, 'user'),
                              col(Presentation, 'owner')))).filter(
                                  object_id__in=presentations.values('id'),
                                  content_type=OwnedWrapper.t(Presentation))
        tags = Tag.objects.usage_for_queryset(q, counts=True)

        if not manage:
            for p in presentations:
                p.verify_password(request)
    else:
        tags = ()

    if presentations and request.user.is_authenticated():
        usertags = Tag.objects.usage_for_queryset(OwnedWrapper.objects.filter(
            user=request.user,
            object_id__in=presentations.values('id'),
            content_type=OwnedWrapper.t(Presentation)),
                                                  counts=True)
    else:
        usertags = ()

    presenters = User.objects.filter(presentation__in=presentations) \
        .annotate(presentations=Count('presentation')) \
        .order_by('last_name', 'first_name')

    if request.user.is_authenticated() and presentations:
        # save current settings
        querystring = request.GET.urlencode()
        store_settings(request.user, 'presentation_browse_querystring',
                       querystring)

    return render_to_response(
        'presentation_browse.html', {
            'manage': manage,
            'tags': tags if len(tags) > 0 else None,
            'untagged': untagged,
            'usertags': usertags if len(usertags) > 0 else None,
            'active_tags': active_tags,
            'active_presenter': presenter,
            'presentations': presentations,
            'presenters': presenters if len(presenters) > 1 else None,
            'keywords': keywords,
            'sortby': sortby,
        },
        context_instance=RequestContext(request))