Beispiel #1
0
def edit_debate(request, space_url, pk):


    place = get_object_or_404(Space, url=space_url)

    if has_space_permission(request.user, place, allow=['admins']) \
    or has_all_permissions(request.user):

        RowFormSet = inlineformset_factory(Debate, Row, extra=1)
        ColumnFormSet = inlineformset_factory(Debate, Column, extra=1)

        instance = Debate.objects.get(pk=pk)
        debate_form = DebateForm(request.POST or None, instance=instance)
        row_formset = RowFormSet(request.POST or None, instance=instance, prefix="rowform")
        column_formset = ColumnFormSet(request.POST or None, instance=instance, prefix="colform")


        if request.user.has_perm('debate.debate_edit') \
        or has_all_permissions(request.user):
            if request.method == 'POST':
                if debate_form.is_valid() and row_formset.is_valid() \
                and column_formset.is_valid():
                    debate_form_uncommited = debate_form.save(commit=False)
                    debate_form_uncommited.space = place
                    debate_form_uncommited.author = request.user

                    saved_debate = debate_form_uncommited.save()
                    debate_instance = get_object_or_404(Debate,
                        pk=pk)
                        
                    row = row_formset.save(commit=False)
                    for form in row:
                        form.debate = instance
                        form.save()

                    column = column_formset.save(commit=False)
                    for form in column:
                        form.debate = instance
                        form.save()

                    return HttpResponseRedirect(reverse(urln.DEBATE_VIEW,
                        kwargs={'space_url': space_url,
                                'debate_id': str(debate_form_uncommited.id)}))

            return render_to_response('debate/debate_add.html',
                                  {'form': debate_form,
                                   'rowform': row_formset,
                                   'colform': column_formset,
                                   'get_place': place,
                                   'debateid': pk},
                                  context_instance=RequestContext(request))
    return render_to_response('not_allowed.html',
                              context_instance=RequestContext(request))
Beispiel #2
0
    def get_object(self):
        # Makes sure the space ins't already in the cache before hitting 
        # the database
        space_url = self.kwargs['space_url']
        space_object = get_or_insert_object_in_cache(Space, space_url,
            url=space_url)
        
        if space_object.public or has_all_permissions(self.request.user):
            if self.request.user.is_anonymous():
                messages.info(self.request, _("Hello anonymous user. Remember \
                    that this space is public to view, but you must \
                    <a href=\"/accounts/register\">register</a> or \
                    <a href=\"/accounts/login\">login</a> to participate."))
            return space_object

        # Check if the user is in the admitted user groups of the space
        if has_space_permission(self.request.user, space_object,
                                allow=['admins', 'mods', 'users']):
            return space_object

        # If the user does not meet any of the conditions, it's not allowed to
        # enter the space
        if self.request.user.is_anonymous():
            messages.info(self.request, _("You're an anonymous user. You must \
                <a href=\"/accounts/register\">register</a> or \
                <a href=\"/accounts/login\">login</a> to access here."))
        else:
            messages.warning(self.request, _("You're not registered to this \
            space."))
        
        self.template_name = 'not_allowed.html'
        return space_object
Beispiel #3
0
 def get_context_data(self, **kwargs):
     context = super(EditDocument, self).get_context_data(**kwargs)
     space = get_object_or_404(Space, url=self.kwargs['space_url'])
     context['get_place'] = space
     context['user_is_admin'] = (has_space_permission(self.request.user,
         space, allow=['admins', 'mods']) or has_all_permissions(
             self.request.user))
     return context
Beispiel #4
0
 def get_context_data(self, **kwargs):
     context = super(EditDocument, self).get_context_data(**kwargs)
     space = get_object_or_404(Space, url=self.kwargs['space_url'])
     context['get_place'] = space
     context['user_is_admin'] = (has_space_permission(
         self.request.user, space, allow=['admins', 'mods'])
                                 or has_all_permissions(self.request.user))
     return context
Beispiel #5
0
 def get_object(self):
     prop_id = self.kwargs['prop_id']
     space_url = self.kwargs['space_url']
     proposal = get_object_or_404(Proposal, pk = prop_id)
     place = get_object_or_404(Space, url = space_url)
     
     if place.public:
         return proposal
     elif has_space_permission(self.request.user, place,
         allow=['admins', 'mods', 'users']) \
         or has_all_permissions(request.user):
         return proposal
     else:
         self.template_name = 'not_allowed.html'
         return Proposal.objects.none()
Beispiel #6
0
def delete_note(request, space_url):

    """
    Deletes a note object.
    """
    note = get_object_or_404(Note, pk=request.POST['noteid'])

    if note.author == request.user or has_all_permissions(request.user):
        ctype = ContentType.objects.get_for_model(Note)
        all_comments = Comment.objects.filter(is_public=True,
                is_removed=False, content_type=ctype,
                object_pk=note.id).all()
        for i in range(note.comment_count):
            all_comments[i].delete()
        note.delete()
        return HttpResponse("The note has been deleted.")

    else:
        return HttpResponse("You're not the author of the note. Can't delete.")
Beispiel #7
0
    def get_object(self):
        # Makes sure the space ins't already in the cache before hitting the
        # databass
        space_url = self.kwargs['space_url']
        space_object = get_or_insert_object_in_cache(Space, space_url, 
            url=space_url)

        if has_space_permission(self.request.user, space_object,
            allow=['admins','mods']) \
        or has_all_permissions(self.request.user):
            try:
                intent = Intent.objects.get(token=self.kwargs['token'])
                intent.space.users.add(intent.user)
                self.status = _("The user has been authorized to participate \
                in space \"%s\"." % space_object.name)
                messages.success(self.request, _("Authorization successful"))

            except Intent.DoesNotExist:
                self.status  = _("The requested intent does not exist!")

            return space_object
Beispiel #8
0
    def get_object(self):
        # Makes sure the space ins't already in the cache before hitting
        # the database
        space_url = self.kwargs['space_url']
        space_object = get_or_insert_object_in_cache(Space,
                                                     space_url,
                                                     url=space_url)

        if space_object.public or has_all_permissions(self.request.user):
            if self.request.user.is_anonymous():
                messages.info(
                    self.request,
                    _("Hello anonymous user. Remember \
                    that this space is public to view, but you must \
                    <a href=\"/accounts/register\">register</a> or \
                    <a href=\"/accounts/login\">login</a> to participate."))
            return space_object

        # Check if the user is in the admitted user groups of the space
        if has_space_permission(self.request.user,
                                space_object,
                                allow=['admins', 'mods', 'users']):
            return space_object

        # If the user does not meet any of the conditions, it's not allowed to
        # enter the space
        if self.request.user.is_anonymous():
            messages.info(
                self.request,
                _("You're an anonymous user. You must \
                <a href=\"/accounts/register\">register</a> or \
                <a href=\"/accounts/login\">login</a> to access here."))
        else:
            messages.warning(
                self.request,
                _("You're not registered to this \
            space."))

        self.template_name = 'not_allowed.html'
        return space_object
Beispiel #9
0
    def get_object(self):
        # Makes sure the space ins't already in the cache before hitting the
        # databass
        space_url = self.kwargs['space_url']
        space_object = get_or_insert_object_in_cache(Space,
                                                     space_url,
                                                     url=space_url)

        if has_space_permission(self.request.user, space_object,
                                 allow=['admins','mods']) \
        or has_all_permissions(self.request.user):
            try:
                intent = Intent.objects.get(token=self.kwargs['token'])
                intent.space.users.add(intent.user)
                self.status = _("The user has been authorized to participate \
                in space \"%s\"." % space_object.name)
                messages.success(self.request, _("Authorization successful"))

            except Intent.DoesNotExist:
                self.status = _("The requested intent does not exist!")

            return space_object
Beispiel #10
0
def add_new_debate(request, space_url):

    """
    Create a new debate. This function returns two forms to create
    a complete debate, debate form and phases formset.
    
    .. versionadded:: 0.1.5

    :attributes: debate_form, row_formset, column_formset
    :context: form, rowform, colform, get_place, debateid
    """
    place = get_object_or_404(Space, url=space_url)

    if has_space_permission(request.user, place, allow=['admins']) \
    or has_all_permissions(request.user):

        RowFormSet = inlineformset_factory(Debate, Row, extra=1)
        ColumnFormSet = inlineformset_factory(Debate, Column, extra=1)

        debate_form = DebateForm(request.POST or None)
        row_formset = RowFormSet(request.POST or None, prefix="rowform")
        column_formset = ColumnFormSet(request.POST or None, prefix="colform")


        # Get the last PK and add 1 to get the current PK
        try:
            last_debate_id = Debate.objects.latest('id')
            current_debate_id = last_debate_id.pk + 1
        except ObjectDoesNotExist:
            current_debate_id = 1

        if request.user.has_perm('debate.debate_add') \
        or has_all_permissions(request.user):
            if request.method == 'POST':
                if debate_form.is_valid() and row_formset.is_valid() \
                and column_formset.is_valid():
                    debate_form_uncommited = debate_form.save(commit=False)
                    debate_form_uncommited.space = place
                    debate_form_uncommited.author = request.user

                    saved_debate = debate_form_uncommited.save()
                    debate_instance = get_object_or_404(Debate,
                        pk=current_debate_id)
                    
                    row = row_formset.save(commit=False)
                    for form in row:
                        form.debate = debate_instance
                        form.save()
                    
                    column = column_formset.save(commit=False)
                    for form in column:
                        form.debate = debate_instance
                        form.save()

                    return HttpResponseRedirect(reverse(urln.DEBATE_VIEW,
                        kwargs={'space_url': space_url,
                                'debate_id': str(debate_form_uncommited.id)}))

            return render_to_response('debate/debate_add.html',
                                  {'form': debate_form,
                                   'rowform': row_formset,
                                   'colform': column_formset,
                                   'get_place': place,
                                   'debateid': current_debate_id},
                                  context_instance=RequestContext(request))
    return render_to_response('not_allowed.html',
                              context_instance=RequestContext(request))
Beispiel #11
0
def add_new_debate(request, space_url):

    """
    Create a new debate. This function returns two forms to create
    a complete debate, debate form and phases formset.
    
    .. versionadded:: 0.1.5

    :attributes: debate_form, row_formset, column_formset
    :context: form, rowform, colform, get_place, debateid
    """
    place = get_object_or_404(Space, url=space_url)

    if has_space_permission(request.user, place, allow=['admins']) \
    or has_all_permissions(request.user):

        # Define FormSets
        # This class is used to make empty formset forms required
        # See http://stackoverflow.com/questions/2406537/django-formsets-make
        # -first-required/4951032#4951032
        class RequiredFormSet(BaseFormSet):
            """
            """
            def __init__(self, *args, **kwargs):
                super(RequiredFormSet, self).__init__(*args, **kwargs)
                for form in self.forms:
                    form.empty_permitted = False

        RowFormSet = formset_factory(RowForm, max_num=10,
            formset=RequiredFormSet, can_delete=True)
        ColumnFormSet = formset_factory(ColumnForm, max_num=10,
            formset=RequiredFormSet, can_delete=True)

        debate_form = DebateForm(request.POST or None)
        row_formset = RowFormSet(request.POST or None, prefix="rowform")
        column_formset = ColumnFormSet(request.POST or None, prefix="colform")

        # Get the last PK and add 1 to get the current PK
        try:
            last_debate_id = Debate.objects.latest('id')
            current_debate_id = last_debate_id.pk + 1
        except ObjectDoesNotExist:
            current_debate_id = 1

        if request.user.has_perm('debate.debate_add') or has_all_permissions():
            if request.method == 'POST':
                if debate_form.is_valid() and row_formset.is_valid() \
                and column_formset.is_valid():
                    debate_form_uncommited = debate_form.save(commit=False)
                    debate_form_uncommited.space = place
                    debate_form_uncommited.author = request.user

                    saved_debate = debate_form_uncommited.save()
                    debate_instance = get_object_or_404(Debate,
                        pk=current_debate_id)

                    for form in row_formset.forms:
                        row = form.save(commit=False)
                        row.debate = debate_instance
                        row.save()

                    for form in column_formset.forms:
                        column = form.save(commit=False)
                        column.debate = debate_instance
                        column.save()

                    return HttpResponseRedirect(reverse(urln.DEBATE_VIEW,
                        kwargs={'space_url': space_url,
                                'debate_id': str(debate_form_uncommited.id)}))

            return render_to_response('debate/debate_add.html',
                                  {'form': debate_form,
                                   'rowform': row_formset,
                                   'colform': column_formset,
                                   'get_place': place,
                                   'debateid': current_debate_id},
                                  context_instance=RequestContext(request))
    return render_to_response('not_allowed.html',
                              context_instance=RequestContext(request))