Skip to content
Extraits de code Groupes Projets
Valider 0eb70312 rédigé par Arnaud Fabre's avatar Arnaud Fabre
Parcourir les fichiers

adds votes

parent 80479bde
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de avec 429 ajouts et 0 suppression
# coding: utf-8
# This file is part of memopol.
#
# memopol is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# memopol 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
from __future__ import absolute_import
from django.contrib import admin
from .admin_import_vote import import_vote_with_recommendation, import_vote
from .models import Recommendation
admin.site.register_view('import_vote', view=import_vote)
admin.site.register_view('import_vote_with_recommendation', view=import_vote_with_recommendation)
class RecommendationsAdmin(admin.ModelAdmin):
list_display = ('title', 'recommendation', 'proposal', 'weight')
search_fields = ('title', 'recommendation', 'proposal')
admin.site.register(Recommendation, RecommendationsAdmin)
# coding: utf-8
# This file is part of memopol.
#
# memopol is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# memopol 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
from __future__ import absolute_import
from django.conf import settings
from django.shortcuts import render, redirect
from django import forms
from django.core.management import call_command
import requests
from representatives_votes.models import Proposal
from .forms import RecommendationForm
class SearchForm(forms.Form):
query = forms.CharField(label='Search', max_length=100)
def import_vote_with_recommendation(request):
context = {}
toutatis_server = getattr(settings,
'TOUTATIS_SERVER',
'http://toutatis.mm.staz.be')
if request.method == 'POST' and 'search' in request.POST:
form = SearchForm(request.POST)
if form.is_valid():
query = form.cleaned_data['query']
context['api_url'] = '{}/api/proposals/?search={}&limit=1000'.format(
toutatis_server,
query
)
r = requests.get(context['api_url'])
context['results'] = r.json()
elif request.method == 'POST' and 'create_recommendation' in request.POST:
form = RecommendationForm(data=request.POST)
if form.is_valid():
# First import proposal
proposal_id = int(request.POST['proposal_id'])
api_url = '{}/api/proposals/{}'.format(toutatis_server, proposal_id)
proposal = requests.get(api_url).json()
call_command('import_proposal_from_toutatis', proposal_id, interactive=False)
# call_command('update_memopol_votes', proposal['dossier_reference'], interactive=False)
memopol_proposal = Proposal.objects.get(
title = proposal['title'],
datetime = proposal['datetime'],
kind = proposal['kind'],
)
recommendation = form.save(commit=False)
recommendation.proposal = memopol_proposal
recommendation.save()
return redirect('/admin/votes/recommendation/')
else:
proposal_id = request.GET.get('import', None)
if proposal_id:
api_url = '{}/api/proposals/{}'.format(toutatis_server, proposal_id)
proposal = requests.get(api_url).json()
context['recommendation_proposal_title'] = proposal['title']
context['recommendation_proposal_dossier_title'] = proposal['dossier_title']
context['recommendation_proposal_id'] = proposal_id
context['recommendation_form'] = RecommendationForm()
form = SearchForm()
context['form'] = form
return render(request, 'votes/admin/import.html', context)
def import_vote(request):
context = {}
toutatis_server = getattr(settings,
'TOUTATIS_SERVER',
'http://toutatis.mm.staz.be')
if request.method == 'POST' and 'search' in request.POST:
print(request.POST)
form = SearchForm(request.POST)
if form.is_valid():
query = form.cleaned_data['query']
context['api_url'] = '{}/api/proposals/?search={}&limit=1000'.format(
toutatis_server,
query
)
r = requests.get(context['api_url'])
context['results'] = r.json()
else:
proposal_id = request.GET.get('import', None)
if proposal_id:
api_url = '{}/api/proposals/{}'.format(toutatis_server, proposal_id)
proposal = requests.get(api_url).json()
call_command('import_proposal_from_toutatis', proposal_id, interactive=False)
call_command('update_memopol_votes', proposal['dossier_reference'], interactive=False)
return redirect('/admin/')
form = SearchForm()
context['form'] = form
return render(request, 'votes/admin/import.html', context)
# coding: utf-8
# This file is part of memopol.
#
# memopol is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# memopol 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
from __future__ import absolute_import
from django.forms import ModelForm
from .models import Recommendation
class RecommendationForm(ModelForm):
class Meta:
model = Recommendation
fields = ['recommendation', 'title', 'description', 'weight']
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('representatives_votes', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='MemopolDossier',
fields=[
('dossier_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='representatives_votes.Dossier')),
('name', models.CharField(max_length=1000)),
('description', models.TextField(blank=True)),
],
options={
},
bases=('representatives_votes.dossier',),
),
migrations.CreateModel(
name='Recommendation',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('recommendation', models.CharField(max_length=10, choices=[(b'abstain', b'abstain'), (b'for', b'for'), (b'against', b'against')])),
('title', models.CharField(max_length=1000, blank=True)),
('description', models.TextField(blank=True)),
('weight', models.IntegerField(default=0)),
('proposal', models.OneToOneField(related_name='recommendation', to='representatives_votes.Proposal')),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='MemopolVote',
fields=[
],
options={
'proxy': True,
},
bases=('representatives_votes.vote',),
),
]
# coding: utf-8
# This file is part of memopol.
#
# memopol is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# memopol 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
from django.db import models
from representatives_votes.models import Vote, Proposal, Dossier
from legislature.models import MemopolRepresentative
class Recommendation(models.Model):
SCORE_TABLE = {
('abstain', 'abstain'): 1,
('abstain', 'for'): -0.5,
('abstain', 'against'): -0.5,
}
VOTECHOICES = (
('abstain', 'abstain'),
('for', 'for'),
('against', 'against')
)
proposal = models.OneToOneField(
Proposal,
related_name='recommendation'
)
recommendation = models.CharField(max_length=10, choices=VOTECHOICES)
title = models.CharField(max_length=1000, blank=True)
description = models.TextField(blank=True)
weight = models.IntegerField(default=0)
class MemopolDossier(Dossier):
name = models.CharField(max_length=1000)
description = models.TextField(blank=True)
class MemopolVote(Vote):
class Meta:
proxy = True
@property
def representative(self):
return MemopolRepresentative.objects.get(
remote_id = self.representative_remote_id
)
- extends 'adminplus/base.html'
- block title
Import Toutatis vote
- block content
%h1 Import Toutatis vote
%form{:action => '', :method => 'post'}
- csrf_token
{{ form }}.
%input{:type => 'submit', :value => 'Search', :name => 'search'}
- if results
-# %a{:href => '={api_url}'}= api_url
%table
%tr
%th Dossier reference
%th Dossier title
%th Vote reference
%th Vote kind
%th Vote title
%th Import
- for result in results.results
%tr
%td= result.dossier_reference
%td= result.dossier_title
%td= result.reference
%td= result.kind
%td= result.title
%td
%strong
<a href="?import={{ result.id }}">
Import this vote
</a>
- if recommendation_form
%form{:action => '', :method => 'post'}
- csrf_token
%input{:type => 'hidden', :value => '={recommendation_proposal_id}', :name => 'proposal_id'}
%table
%tr
%th Dossier
%td= recommendation_proposal_dossier_title
%tr
%th Vote
%td= recommendation_proposal_title
{{ recommendation_form }}
%tr
%td
%td
%input{:type => 'submit', :value => 'Go', :name => 'create_recommendation'}
- extends "base.html"
- block content
-# - include 'legislature/search.html'
%p
Number of votes: {{ votes_num }}
%table
- for vote in votes
%tr
%td
{{ vote.title }}
-# %tr
-# - include '/representative_block.html'
.pagination
%span.step-links
- if votes.has_previous
%a{'href': '?={queries.urlencode}&page=={votes.previous_page_number}'} previous
%span.current
Page ={votes.number} of ={votes.paginator.num_pages}
- if votes.has_next
%a{'href': '?={queries.urlencode}&page=={votes.next_page_number}'} next
from django.test import TestCase
# Create your tests here.
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns(
'',
url(
r'^votes/?$',
views.votes_index,
name='votes_index'
),
)
from django.shortcuts import render
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from representatives_votes.models import Dossier
def votes_index(request):
votes_list = Dossier.objects.all()
return _render_list(request, votes_list)
def _render_list(request, votes_list, num_by_page=30):
"""
Render a paginated list of votes
"""
paginator = Paginator(votes_list, num_by_page)
page = request.GET.get('page')
try:
votes = paginator.page(page)
except PageNotAnInteger:
votes = paginator.page(1)
except EmptyPage:
votes = paginator.page(paginator.num_pages)
context = {}
queries_without_page = request.GET.copy()
if 'page' in queries_without_page:
del queries_without_page['page']
context['queries'] = queries_without_page
context['votes'] = votes
context['votes_num'] = paginator.count
return render(
request,
'votes/votes_list.html',
context
)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter