Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        
        if request.POST.get('user'):
            voter_id = request.POST['user']
        else:
            voter_id = request.user.id
        proposal = self.get_object()
        pid = proposal.id

        val = request.POST['val']

        value = ''
        if val == 'pro':
            value = ProposalVoteValue.PRO
        elif val == 'con':
            value = ProposalVoteValue.CON
        elif val == 'reset':
            vote = get_object_or_404(ProposalVote,
                                     proposal_id=pid, user_id=voter_id)

            vote.delete()
            return json_response({
                'result': 'ok',
                'html': render_to_string('issues/_vote_panel.html',
                                         {
                                             'proposal': proposal,
                                             'community': self.community,
                                         }),
                'sum': render_to_string('issues/_member_vote_sum.html',
                                         {
                                             'proposal': proposal,
                                             'community': self.community,
                                         })
            })

        else:
            return HttpResponseBadRequest('vote value not valid')

        if ProposalVote.objects.filter(proposal_id=pid, user_id=voter_id).exists():
            ProposalVote.objects.filter(proposal_id=pid, user_id=voter_id).update(value=value)
        else:
            ProposalVote.objects.create(
                proposal_id=pid,
                user_id=voter_id,
                value=value)

        return json_response({
            'result': 'ok',
            'html': render_to_string('issues/_vote_reset_panel.html',
                                        {
                                             'proposal': proposal,
                                             'community': self.community,
                                         }),
            'sum': render_to_string('issues/_member_vote_sum.html',
                                     {
                                         'proposal': proposal,
                                         'community': self.community,
                                     })
        })
Esempio n. 2
0
 def post(self, request, *args, **kwargs):
     p = self.get_object()
     if request.POST.get('val', None):
         p.register_board_votes = request.POST.get('val') == '1'
         p.save()
         return json_response({'result': 'ok'})
     else:
         return json_response({'result': 'err'})
Esempio n. 3
0
 def post(self, request, *args, **kwargs):
     p = self.get_object()
     if request.POST.get('val', None):
         p.register_board_votes = request.POST.get('val') == '1'
         p.save()
         return json_response({'result': 'ok'})
     else:
         return json_response({'result': 'err'})
Esempio n. 4
0
    def post(self, request, *args, **kwargs):

        if request.POST.get("user"):
            voter_id = request.POST["user"]
            vote_class = ProposalVoteBoard
        else:
            voter_id = request.user.id
            vote_class = ProposalVote

        proposal = self.get_object()
        pid = proposal.id

        val = request.POST["val"]

        value = ""
        if val == "pro":
            value = ProposalVoteValue.PRO
        elif val == "con":
            value = ProposalVoteValue.CON
        elif val == "reset":
            vote = get_object_or_404(vote_class, proposal_id=pid, user_id=voter_id)
            vote.delete()
            return json_response(
                {
                    "result": "ok",
                    "html": render_to_string(
                        "issues/_vote_panel.html", {"proposal": proposal, "community": self.community}
                    ),
                    "sum": render_to_string(
                        "issues/_member_vote_sum.html", {"proposal": proposal, "community": self.community}
                    ),
                }
            )

        else:
            return HttpResponseBadRequest("vote value not valid")

        vote, created = vote_class.objects.get_or_create(proposal_id=pid, user_id=voter_id)
        vote.value = value
        vote.save()
        return json_response(
            {
                "result": "ok",
                "html": render_to_string(
                    "issues/_vote_reset_panel.html", {"proposal": proposal, "community": self.community}
                ),
                "sum": render_to_string(
                    "issues/_member_vote_sum.html", {"proposal": proposal, "community": self.community}
                ),
            }
        )
Esempio n. 5
0
    def post(self, request, *args, **kwargs):
        voter_id = request.user.id
        """
        voter_id = int(request.POST.get('uid', '0'))
        if voter_id != request.user.id:
            return HttpResponseBadRequest('no permission for action')
        pid = int(request.POST['pid'])
        """
        pid = int(kwargs['pk'])
        proposal = get_object_or_404(Proposal, id=pid)
        val = request.POST['val']
        
   
        value = ''
        if val == 'pro':
            value = ProposalVoteValue.PRO
        elif val == 'con':
            value = ProposalVoteValue.CON
        elif val == 'reset':
            vote = get_object_or_404(ProposalVote,
                                     proposal_id=pid, user_id=voter_id)
            
            vote.delete()
            return json_response({
                'result': 'ok',
                'html': render_to_string('issues/_vote_panel.html',
                                         {
                                             'proposal': proposal,
                                             'community': self.community,   
                                         })
            })
            
        else:
            return HttpResponseBadRequest('vote value not valid')

        ProposalVote.objects.create(
            proposal_id=pid,
            user_id=voter_id,
            value=value)
            
        return json_response({
            'result': 'ok',
            'html': render_to_string('issues/_vote_reset_panel.html',
                                        {
                                             'proposal': proposal,
                                             'community': self.community,
                                         })
        })
Esempio n. 6
0
 def post(self, request, *args, **kwargs):
     # TODO: check post permission for user and for each issue
     # Shultze
     # send_issue_ranking(request)
     return json_response({
         'res': 'ok',
     })
Esempio n. 7
0
def send_issue_ranking(request):
    if request.POST:
        current_vote = json.loads(request.POST.get('new_order'))
        prev_vote = IssueRankingVote.objects.filter(
                            voted_by=request.user,
                            issue__community_id=request.POST['community_id']) \
                            .order_by('rank')

        if current_vote:
            prev_param = {
                'ballot': [],
                'count': 1,
            }
            current_param = {
                'ballot': [],
                'count': 1,
            }
            prev_vote_as_list = list(
                prev_vote.values_list('issue_id', flat=True))
            for v in prev_vote:
                v.delete()

            for v in prev_vote_as_list:
                prev_param['ballot'].append([v])

            for i, v in enumerate(current_vote):
                IssueRankingVote.objects.create(voted_by=request.user,
                                                issue_id=v,
                                                rank=i)
                current_param['ballot'].append([v])
            print current_param, prev_param
            # process_vote_stub(current_vote, prev_vote)
            return HttpResponse(json_response('ok'))
Esempio n. 8
0
def send_issue_ranking(request):
    if request.POST:
        current_vote = json.loads(request.POST.get('new_order'))
        prev_vote = IssueRankingVote.objects.filter(
                            voted_by=request.user,
                            issue__community_id=request.POST['community_id']) \
                            .order_by('rank')
        
        if current_vote:
            prev_param = {'ballot': [], 'count': 1, }
            current_param = {'ballot': [], 'count': 1, }
            prev_vote_as_list = list(prev_vote.values_list('issue_id', flat=True))
            for v in prev_vote:
                v.delete()

            for v in prev_vote_as_list:
                prev_param['ballot'].append([v])

            for i, v in enumerate(current_vote):
                IssueRankingVote.objects.create(
                    voted_by=request.user,
                    issue_id=v,
                    rank=i
                )
                current_param['ballot'].append([v])
            print current_param, prev_param
            # process_vote_stub(current_vote, prev_vote)
            return HttpResponse(json_response('ok'))
Esempio n. 9
0
def send_issue_ranking(request):
    if request.POST:
        cid = request.POST['committee_id']
        current_vote = json.loads(request.POST.get('new_order'))
        prev_vote = IssueRankingVote.objects.filter(voted_by=request.user, issue__committee_id=cid).order_by('rank')

        if current_vote:
            prev_param = {'ballot': [], 'count': 1, }
            current_param = {'ballot': [], 'count': 1, }
            if prev_vote:
                prev_vote_as_list = list(prev_vote.values_list('issue_id', flat=True))
                for v in prev_vote:
                    v.delete()

                for v in prev_vote_as_list:
                    prev_param['ballot'].append([v])
                prev_param = [prev_param, ]
            else:
                prev_param = None

            for i, v in enumerate(current_vote):
                IssueRankingVote.objects.create(
                    voted_by=request.user,
                    issue_id=v,
                    rank=i
                )
                current_param['ballot'].append([v])
            remaining_issues = Issue.objects.filter(committee_id=cid, active=True).exclude(id__in=current_vote)
            # current_param['ballot'].append([issue.id for issue in remaining_issues])
            current_param = [current_param, ]
            # print current_param, prev_param
            user_vote(cid, current_param, prev_param)
            set_issues_order_by_votes(cid)
            return HttpResponse(json_response('ok'))
Esempio n. 10
0
    def post(self, request, *args, **kwargs):

        form = forms.CreateIssueCommentForm(request.POST)
        if not form.is_valid():
            return HttpResponseBadRequest()

        i = self.get_object()
        comment_id = request.POST.get('comment_id', None)
        try:
            c = i.comments.get(pk=int(comment_id))
            c.content=enhance_html(form.cleaned_data['content'])
            c.save()
            return json_response({'comment_id': c.id})
        except:
            c = i.comments.create(content=enhance_html(form.cleaned_data['content']),
                                  created_by=request.user)
            return json_response({'comment_id': c.id})
Esempio n. 11
0
    def post(self, request, *args, **kwargs):

        form = forms.CreateIssueCommentForm(request.POST)
        if not form.is_valid():
            return HttpResponseBadRequest()

        i = self.get_object()
        comment_id = request.POST.get('comment_id', None)
        try:
            c = i.comments.get(pk=int(comment_id))
            c.content = enhance_html(form.cleaned_data['content'])
            c.save()
            return json_response({'comment_id': c.id})
        except:
            c = i.comments.create(content=enhance_html(
                form.cleaned_data['content']),
                                  created_by=request.user)
            return json_response({'comment_id': c.id})
Esempio n. 12
0
    def post(self, request, *args, **kwargs):
        voter_ids = json.loads(request.POST["users"])
        proposal = self.get_object()
        pid = proposal.id

        val = request.POST["val"]

        value = ""
        if val == "pro":
            value = ProposalVoteValue.PRO
        elif val == "con":
            value = ProposalVoteValue.CON
        elif val == "reset":
            ProposalVoteBoard.objects.filter(proposal_id=pid, user_id__in=voter_ids).delete()
            return json_response(
                {
                    "result": "ok",
                    "html": render_to_string(
                        "issues/_vote_panel.html", {"proposal": proposal, "community": self.community}
                    ),
                    "sum": render_to_string(
                        "issues/_member_vote_sum.html", {"proposal": proposal, "community": self.community}
                    ),
                }
            )

        else:
            return HttpResponseBadRequest("vote value not valid")

        for user_id in voter_ids:
            vote, created = ProposalVoteBoard.objects.get_or_create(proposal_id=pid, user_id=user_id)
            vote.value = value
            vote.save()
        return json_response(
            {
                "result": "ok",
                "html": render_to_string(
                    "issues/_vote_reset_panel.html", {"proposal": proposal, "community": self.community}
                ),
                "sum": render_to_string(
                    "issues/_member_vote_sum.html", {"proposal": proposal, "community": self.community}
                ),
            }
        )
Esempio n. 13
0
def send_issue_ranking(request):
    if request.POST:
        cid = request.POST['community_id']
        current_vote = json.loads(request.POST.get('new_order'))
        prev_vote = IssueRankingVote.objects.filter(
                            voted_by=request.user,
                            issue__community_id=cid) \
                            .order_by('rank')

        if current_vote:
            prev_param = {
                'ballot': [],
                'count': 1,
            }
            current_param = {
                'ballot': [],
                'count': 1,
            }
            if prev_vote:
                prev_vote_as_list = list(
                    prev_vote.values_list('issue_id', flat=True))
                for v in prev_vote:
                    v.delete()

                for v in prev_vote_as_list:
                    prev_param['ballot'].append([v])
                prev_param = [
                    prev_param,
                ]
            else:
                prev_param = None

            for i, v in enumerate(current_vote):
                IssueRankingVote.objects.create(voted_by=request.user,
                                                issue_id=v,
                                                rank=i)
                current_param['ballot'].append([v])
            remaining_issues = Issue.objects.filter(community_id=cid, active=True) \
                                            .exclude(id__in=current_vote)
            # current_param['ballot'].append([issue.id for issue in remaining_issues])
            current_param = [
                current_param,
            ]
            # print current_param, prev_param
            user_vote(cid, current_param, prev_param)
            set_issues_order_by_votes(request.POST['community_id'])
            return HttpResponse(json_response('ok'))
Esempio n. 14
0
    def post(self, request, *args, **kwargs):
        voted_ids = json.loads(request.POST['users'])
        proposal = self.get_object()
        pid = proposal.id
        voter_group = request.user.get_default_group(self.community) \
            if request.user.is_authenticated() \
            else ''

        val = request.POST['val']

        value = self._vote_values_map(val)
        if value == None:
            return HttpResponseBadRequest('vote value not valid')

        vote_failed = []
        for user_id in voted_ids:
            vote, valid = self._do_vote(ProposalVoteBoard, proposal, user_id,
                                        value, True, voter_group)
            if valid == ProposalVoteMixin.VOTE_OVERRIDE_ERR:
                vote_failed.append({
                    'uid': user_id,
                    'val': self._vote_values_map(vote.value),
                })

        return json_response({
            'result':
            'ok',
            'html':
            render_to_string('issues/_vote_reset_panel.html', {
                'proposal': proposal,
                'community': self.community,
            }),
            'override_fail':
            vote_failed,
            'sum':
            render_to_string(
                'issues/_member_vote_sum.html', {
                    'proposal': proposal,
                    'community': self.community,
                    'board_attending': board_voters_on_proposal(proposal),
                })
        })
Esempio n. 15
0
    def post(self, request, *args, **kwargs):
        voted_ids = json.loads(request.POST['users'])
        proposal = self.get_object()
        pid = proposal.id
        voter_group = request.user.get_default_group(self.community) \
            if request.user.is_authenticated() \
            else ''

        val = request.POST['val']

        value = self._vote_values_map(val)
        if value == None:
            return HttpResponseBadRequest('vote value not valid')

        vote_failed = []
        for user_id in voted_ids:
            vote, valid = self._do_vote(ProposalVoteBoard, proposal,
                                        user_id, value, True, voter_group)
            if valid == ProposalVoteMixin.VOTE_OVERRIDE_ERR:
                vote_failed.append({'uid': user_id, 'val': self._vote_values_map(vote.value), })

        return json_response({
            'result': 'ok',
            'html': render_to_string('issues/_vote_reset_panel.html',
                                     {
                                         'proposal': proposal,
                                         'community': self.community,
                                     }),
            'override_fail': vote_failed,
            'sum': render_to_string('issues/_member_vote_sum.html',
                                    {
                                        'proposal': proposal,
                                        'community': self.community,
                                        'board_attending': board_voters_on_proposal(proposal),
                                    })
        })
Esempio n. 16
0
 def post(self, request, *args, **kwargs):
     # TODO: check post permission for user and for each issue
     send_issue_ranking(request)
     return json_response({'res': 'ok', })
Esempio n. 17
0
 def post(self, request, *args, **kwargs):
     send_issue_ranking(request)
     return json_response({"res": "ok"})
Esempio n. 18
0
    def post(self, request, *args, **kwargs):

        is_board = request.POST.get('board', False)
        user_id = request.POST.get('user', request.user.id)
        voter_id = request.user.id
        voter_group = request.user.get_default_group(self.community) \
            if request.user.is_authenticated() \
            else ''
        val = request.POST['val']
        if is_board:
            # vote for board member by chairman or board member
            vote_class = ProposalVoteBoard
        else:
            # straw vote by member
            vote_class = ProposalVote

        proposal = self.get_object()
        pid = proposal.id
        vote_panel_tpl = 'issues/_vote_panel.html' if val == 'reset' \
            else 'issues/_vote_reset_panel.html'

        res_panel_tpl = 'issues/_board_vote_res.html' if is_board \
            else 'issues/_vote_reset_panel.html'
        vote_response = {
            'result': 'ok',
            'html': render_to_string(res_panel_tpl,
                                     {
                                         'proposal': proposal,
                                         'community': self.community,
                                         'vote_status': val,
                                     }),
        }

        value = ''
        if val == 'reset':
            vote = get_object_or_404(vote_class,
                                     proposal_id=pid, user_id=user_id)
            vote.delete()
            related_arguments = ProposalVoteArgumentRanking.objects.filter(user=request.user, argument__proposal_vote__proposal=proposal)
            if related_arguments.count():
                related_arguments.delete()
            vote_response['html'] = render_to_string(vote_panel_tpl,
                                                     {
                                                         'proposal': proposal,
                                                         'community': self.community
                                                     })

            return json_response(vote_response)
        else:
            value = self._vote_values_map(val)
        if value == None:
            return HttpResponseBadRequest('vote value not valid')

        vote, valid = self._do_vote(vote_class, proposal, user_id, value,
                                    is_board, voter_group)
        if valid == ProposalVoteMixin.VOTE_OK:
            vote_response['html'] = render_to_string(res_panel_tpl,
                                                     {
                                                         'proposal': proposal,
                                                         'community': self.community,
                                                         'vote_status': val,
                                                         'user': self.request.user
                                                     })
            if is_board and voter_group == DefaultGroups.CHAIRMAN:
                vote_response['sum'] = render_to_string('issues/_member_vote_sum.html',
                                                        {
                                                            'proposal': proposal,
                                                            'community': self.community,
                                                            'board_attending': board_voters_on_proposal(proposal),
                                                        })
        else:
            vote_response['result'] = 'err'
            if valid == ProposalVoteMixin.VOTE_OVERRIDE_ERR:
                vote_response['override_fail'] = [{'uid': user_id,
                                                   'val': self._vote_values_map(vote.value),
                                                  }]

        return json_response(vote_response)
Esempio n. 19
0
 def post(self, request, *args, **kwargs):
     send_issue_ranking(request)
     return json_response({'res': 'ok', })
Esempio n. 20
0
    def post(self, request, *args, **kwargs):

        is_board = request.POST.get('board', False)
        user_id = request.POST.get('user', request.user.id)
        voter_id = request.user.id
        voter_group = request.user.get_default_group(
            self.community) if request.user.is_authenticated else ''
        val = request.POST['val']
        if is_board:
            # vote for board member by chairman or board member
            vote_class = ProposalVoteBoard
        else:
            # straw vote by member
            vote_class = ProposalVote

        proposal = self.get_object()
        pid = proposal.id
        vote_panel_tpl = 'issues/_vote_panel.html' if val == 'reset' \
            else 'issues/_vote_reset_panel.html'

        res_panel_tpl = 'issues/_board_vote_res.html' if is_board \
            else 'issues/_vote_reset_panel.html'
        vote_response = {
            'result':
            'ok',
            'html':
            render_to_string(
                res_panel_tpl, {
                    'proposal': proposal,
                    'community': self.community,
                    'vote_status': val,
                }),
        }

        value = ''
        if val == 'reset':
            vote = get_object_or_404(vote_class,
                                     proposal_id=pid,
                                     user_id=user_id)
            vote.delete()
            related_arguments = ProposalVoteArgumentRanking.objects.filter(
                user=request.user, argument__proposal_vote__proposal=proposal)
            if related_arguments.count():
                related_arguments.delete()
            vote_response['html'] = render_to_string(
                vote_panel_tpl, {
                    'proposal': proposal,
                    'community': self.community
                })

            return json_response(vote_response)
        else:
            value = self._vote_values_map(val)
        if value == None:
            return HttpResponseBadRequest('vote value not valid')

        vote, valid = self._do_vote(vote_class, proposal, user_id, value,
                                    is_board, voter_group)
        if valid == ProposalVoteMixin.VOTE_OK:
            vote_response['html'] = render_to_string(
                res_panel_tpl, {
                    'proposal': proposal,
                    'community': self.community,
                    'vote_status': val,
                    'user': self.request.user
                })
            if is_board and voter_group == DefaultGroups.CHAIRMAN:
                vote_response['sum'] = render_to_string(
                    'issues/_member_vote_sum.html', {
                        'proposal': proposal,
                        'community': self.community,
                        'board_attending': board_voters_on_proposal(proposal),
                    })
        else:
            vote_response['result'] = 'err'
            if valid == ProposalVoteMixin.VOTE_OVERRIDE_ERR:
                vote_response['override_fail'] = [{
                    'uid':
                    user_id,
                    'val':
                    self._vote_values_map(vote.value),
                }]

        return json_response(vote_response)