<commit_before># -*- coding: utf-8 -*-
#
# Copyright (c) 2010-2012 <NAME>. Galega
#
# This file is part of e-cidadania.
#
# e-cidadania is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# e-cidadania is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with e-cidadania. If not, see <http://www.gnu.org/licenses/>.

"""
Proposal module views.
"""

from django.core.urlresolvers import reverse
from django.views.generic.list import ListView
from django.views.generic.edit import UpdateView, DeleteView
from django.views.generic import FormView
from django.views.generic.create_update import update_object
from django.views.decorators.http import require_POST
from django.db.models import Count
from django.db.models import F
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib import messages
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.utils.decorators import method_decorator
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404, redirect

from apps.ecidadania.proposals import url_names as urln_prop
from core.spaces import url_names as urln_space
from core.spaces.models import Space
from core.permissions import has_operation_permission, has_space_permission
from apps.ecidadania.proposals.models import Proposal, ProposalSet, \
        ProposalField
from apps.ecidadania.proposals.forms import ProposalForm, VoteProposal, \
        ProposalSetForm, ProposalFieldForm, ProposalSetSelectForm, \
        ProposalMergeForm, ProposalFieldDeleteForm

class AddProposal(FormView):

    """
    Create a new single (not tied to a set) proposal. The permission checks are
    done in the form_valid method.
    
    :parameters: space_url
    :rtype: HTML Form
    :context: form, get_place
    """
    form_class = ProposalForm
    template_name = 'proposals/proposal_form.html'
    
    def get_success_url(self):
        space = self.kwargs['space_url']
        return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space})
        
    def form_valid(self, form):
        space = get_object_or_404(Space, url=self.kwargs['space_url'])

        if has_operation_permission(self.request.user, space,
        'proposals.add_proposal', allow=['admins', 'mods','users']):
            form_uncommited = form.save(commit=False)
            form_uncommited.space = space
            form_uncommited.author = self.request.user
            form_uncommited.save()

            return super(AddProposal, self).form_valid(form)
        else:
            return render_to_response('not_allowed.html',
                context_instance=RequestContext(self.request))
    
    def get_context_data(self, **kwargs):
        context = super(AddProposal, self).get_context_data(**kwargs)
        self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
        #self.field = ProposalField.objects.filter(proposalset=self.kwargs['p_set'])
        context['get_place'] = self.space
        #context['form_field'] = [f_name.field_name for f_name in self.field]
        return context
    
    @method_decorator(permission_required('proposals.add_proposal'))
    def dispatch(self, *args, **kwargs):
        return super(AddProposal, self).dispatch(*args, **kwargs)


class EditProposal(UpdateView):

    """
    The proposal can be edited by space and global admins, but also by their
    creator.

    :rtype: HTML Form
    :context: get_place
    :parameters: space_url, prop_id
    """
    model = Proposal
    template_name = 'proposals/proposal_form.html'
    
    def get_success_url(self):
        space = self.kwargs['space_url']
        proposal = self.kwargs['prop_id']
        return reverse(urln_prop.PROPOSAL_VIEW, kwargs={'space_url':space,
                                                        'prop_id':proposal})
        
    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)
        space = get_object_or_404(Space, url = space_url)

        if has_operation_permission(self.request.user, space,
        'proposals.change_proposal', allow=['admins', 'mods']) \
        or proposal.author.id == self.request.user.id:
            return get_object_or_404(Proposal, pk = prop_id)
        else:
            return render_to_response('not_allowed.html',
                context_instance=RequestContext(self.request))
        
    def get_context_data(self, **kwargs):
        context = super(EditProposal, self).get_context_data(**kwargs)<commit_msg>Slightly changed a comment<commit_after># -*- coding: utf-8 -*-
#
# Copyright (c) 2010-2012 <NAME>
#
# This file is part of e-cidadania.
#
# e-cidadania is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# e-cidadania is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with e-cidadania. If not, see <http://www.gnu.org/licenses/>.

"""
Proposal module views.
"""

from django.core.urlresolvers import reverse
from django.views.generic.list import ListView
from django.views.generic.edit import UpdateView, DeleteView
from django.views.generic import FormView
from django.views.generic.create_update import update_object
from django.views.decorators.http import require_POST
from django.db.models import Count
from django.db.models import F
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib import messages
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.utils.decorators import method_decorator
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404, redirect

from apps.ecidadania.proposals import url_names as urln_prop
from core.spaces import url_names as urln_space
from core.spaces.models import Space
from core.permissions import has_operation_permission, has_space_permission
from apps.ecidadania.proposals.models import Proposal, ProposalSet, \
        ProposalField
from apps.ecidadania.proposals.forms import ProposalForm, VoteProposal, \
        ProposalSetForm, ProposalFieldForm, ProposalSetSelectForm, \
        ProposalMergeForm, ProposalFieldDeleteForm

class AddProposal(FormView):

    """
    Create a new single (not tied to a set) proposal. The permission checks are
    done in the form_valid method.
    
    :parameters: space_url
    :rtype: HTML Form
    :context: form, get_place
    """
    form_class = ProposalForm
    template_name = 'proposals/proposal_form.html'
    
    def get_success_url(self):
        space = self.kwargs['space_url']
        return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space})
        
    def form_valid(self, form):
        space = get_object_or_404(Space, url=self.kwargs['space_url'])

        if has_operation_permission(self.request.user, space,
        'proposals.add_proposal', allow=['admins', 'mods','users']):
            form_uncommited = form.save(commit=False)
            form_uncommited.space = space
            form_uncommited.author = self.request.user
            form_uncommited.save()

            return super(AddProposal, self).form_valid(form)
        else:
            return render_to_response('not_allowed.html',
                context_instance=RequestContext(self.request))
    
    def get_context_data(self, **kwargs):
        context = super(AddProposal, self).get_context_data(**kwargs)
        self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
        #self.field = ProposalField.objects.filter(proposalset=self.kwargs['p_set'])
        context['get_place'] = self.space
        #context['form_field'] = [f_name.field_name for f_name in self.field]
        return context
    
    @method_decorator(permission_required('proposals.add_proposal'))
    def dispatch(self, *args, **kwargs):
        return super(AddProposal, self).dispatch(*args, **kwargs)


class EditProposal(UpdateView):

    """
    The proposal can not only be edited by space and global admins, but also by their
    creator.

    :rtype: HTML Form
    :context: get_place
    :parameters: space_url, prop_id
    """
    model = Proposal
    template_name = 'proposals/proposal_form.html'
    
    def get_success_url(self):
        space = self.kwargs['space_url']
        proposal = self.kwargs['prop_id']
        return reverse(urln_prop.PROPOSAL_VIEW, kwargs={'space_url':space,
                                                        'prop_id':proposal})
        
    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)
        space = get_object_or_404(Space, url = space_url)

        if has_operation_permission(self.request.user, space,
        'proposals.change_proposal', allow=['admins', 'mods']) \
        or proposal.author.id == self.request.user.id:
            return get_object_or_404(Proposal, pk = prop_id)
        else:
            return render_to_response('not_allowed.html',
                context_instance=RequestContext(self.request))
        
    def get_context_data(self, **kwargs):
        context = super(EditProposal, self).get_context_data(**kwargs)
{'fraction_non_alphanumeric': 0.08977384464110127, 'fraction_numerical': 0.005506391347099311, 'mean_word_length': 3.4531523642732047, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1453785', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2919, 'n_words': 777}