Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
luxcem
memopol
Commits
a231f56e
Commit
a231f56e
authored
Mar 23, 2015
by
luxcem
Browse files
updates legislatures models : no colisions with representatives
parent
305aaeee
Changes
14
Hide whitespace changes
Inline
Side-by-side
core/templates/core/blocks/navigation.haml
View file @
a231f56e
...
...
@@ -2,3 +2,16 @@
%li
%a
{
href:
"{% url 'legislature:representatives_index' %}"
}
Representatives
%li
%a
{
href:
"{% url 'legislature:groups_by_kind' 'country' %}"
}
Countries
%li
%a
{
href:
"{% url 'legislature:groups_by_kind' 'group' %}"
}
Parties
%li
%a
{
href:
"{% url 'legislature:groups_by_kind' 'delegation' %}"
}
Delegations
%li
%a
{
href:
"{% url 'legislature:groups_by_kind' 'committee' %}"
}
Committees
legislature/admin.py
View file @
a231f56e
from
django.contrib
import
admin
from
representatives.models
import
Email
,
WebSite
,
Address
,
Phone
,
Country
,
Constituency
from
legislature.models
import
MMandate
,
MRepresentative
,
MGroup
class
EmailInline
(
admin
.
TabularInline
):
model
=
Email
extra
=
0
class
WebsiteInline
(
admin
.
TabularInline
):
model
=
WebSite
extra
=
0
class
AdressInline
(
admin
.
StackedInline
):
model
=
Address
extra
=
0
class
PhoneInline
(
admin
.
TabularInline
):
model
=
Phone
extra
=
0
class
RepresentativeAdmin
(
admin
.
ModelAdmin
):
list_display
=
(
'full_name'
,
'gender'
,
'birth_place'
)
search_fields
=
(
'first_name'
,
'last_name'
,
'birth_place'
)
list_filter
=
(
'gender'
,
)
inlines
=
[
PhoneInline
,
EmailInline
,
WebsiteInline
,
AdressInline
,
]
class
MandateAdmin
(
admin
.
ModelAdmin
):
list_display
=
(
'representative'
,
'group'
,
'role'
,
'constituency'
,
'begin_date'
,
'end_date'
,
'active'
)
search_fields
=
(
'representative'
,
'group'
,
'constituency'
)
# list_filter = ('role',)
admin
.
site
.
register
(
MRepresentative
,
RepresentativeAdmin
)
# admin.site.register(Country)
admin
.
site
.
register
(
MMandate
,
MandateAdmin
)
admin
.
site
.
register
(
MGroup
)
# admin.site.register(Constituency)
legislature/management/commands/legislature_updates_models.py
View file @
a231f56e
# import datetime
from
django.core.management.base
import
BaseCommand
from
representatives
import
models
from
legislature.models
import
Representative
,
Mandate
from
legislature.models
import
M
Representative
,
M
Mandate
,
MGroup
from
django.db
import
transaction
...
...
@@ -14,21 +14,37 @@ class Command(BaseCommand):
print
(
'Representatives'
)
n
=
models
.
Representative
.
objects
.
all
().
count
()
for
i
,
representative
in
enumerate
(
models
.
Representative
.
objects
.
all
()):
legislature_representative
=
Representative
(
representative_ptr
=
representative
)
legislature_representative
.
__dict__
.
update
(
representative
.
__dict__
)
legislature_representative
.
update_country
()
legislature_representative
.
save
()
mrepresentative
=
MRepresentative
(
representative_ptr
=
representative
)
mrepresentative
.
__dict__
.
update
(
representative
.
__dict__
)
mrepresentative
.
save
()
print
(
"%s/%s
\r
"
%
(
i
,
n
)),
print
(
'Mandates'
)
for
i
,
representative
in
enumerate
(
Representative
.
objects
.
all
()):
legislature_mandates
=
[]
for
i
,
m
representative
in
enumerate
(
M
Representative
.
objects
.
all
()):
representative
=
mrepresentative
.
representative_ptr
for
mandate
in
representative
.
mandate_set
.
all
():
legislature_mandate
=
Mandate
(
mandate_ptr
=
mandate
)
legislature_mandate
.
__dict__
.
update
(
mandate
.
__dict__
)
legislature_mandate
.
update_active
()
legislature_mandate
.
save
()
legislature_mandates
.
append
(
legislature_mandate
)
representative
.
update_active
()
representative
.
save
()
mmandate
=
MMandate
(
mandate_ptr
=
mandate
)
mmandate
.
__dict__
.
update
(
mandate
.
__dict__
)
mmandate
.
mrepresentative
=
mrepresentative
# Group creation
try
:
mgroup
=
MGroup
(
group_ptr
=
mandate
.
group
)
except
MGroup
.
DoesNotExist
:
mgroup
=
MGroup
(
group_ptr
=
mandate
.
group
)
mgroup
.
__dict__
.
update
(
mandate
.
group
.
__dict__
)
mgroup
.
save
()
mmandate
.
mgroup
=
mgroup
mmandate
.
save
()
mmandate
.
update_active
()
mrepresentative
.
update_country
()
mrepresentative
.
update_active
()
mrepresentative
.
save
()
print
(
"%s/%s
\r
"
%
(
i
,
n
)),
print
(
'Groups'
)
for
i
,
mgroup
in
enumerate
(
MGroup
.
objects
.
all
()):
mgroup
.
update_active
()
legislature/migrations/0001_initial.py
View file @
a231f56e
...
...
@@ -7,44 +7,46 @@ from django.db import models, migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'representatives'
,
'000
4_representative_country
'
),
(
'representatives'
,
'000
7_auto_20150323_1017
'
),
]
operations
=
[
migrations
.
CreateModel
(
name
=
'Constituency'
,
fields
=
[
(
'constituency_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Constituency'
)),
],
options
=
{
},
bases
=
(
'representatives.constituency'
,),
),
migrations
.
CreateModel
(
name
=
'Group'
,
name
=
'MGroup'
,
fields
=
[
(
'group_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Group'
)),
(
'active'
,
models
.
BooleanField
(
default
=
False
)),
],
options
=
{
},
bases
=
(
'representatives.group'
,),
),
migrations
.
CreateModel
(
name
=
'Mandate'
,
name
=
'
M
Mandate'
,
fields
=
[
(
'mandate_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Mandate'
)),
(
'active'
,
models
.
BooleanField
(
default
=
False
)),
(
'mgroup'
,
models
.
ForeignKey
(
to
=
'legislature.MGroup'
)),
],
options
=
{
},
bases
=
(
'representatives.mandate'
,),
),
migrations
.
CreateModel
(
name
=
'Representative'
,
name
=
'
M
Representative'
,
fields
=
[
(
'representative_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Representative'
)),
(
'active'
,
models
.
BooleanField
(
default
=
False
)),
(
'country'
,
models
.
ForeignKey
(
to
=
'representatives.Country'
,
null
=
True
)),
],
options
=
{
},
bases
=
(
'representatives.representative'
,),
),
migrations
.
AddField
(
model_name
=
'mmandate'
,
name
=
'mrepresentative'
,
field
=
models
.
ForeignKey
(
to
=
'legislature.MRepresentative'
),
preserve_default
=
True
,
),
]
legislature/migrations/0002_auto_20150319_1620.py
deleted
100644 → 0
View file @
305aaeee
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
models
,
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'representatives'
,
'0005_auto_20150319_1620'
),
(
'legislature'
,
'0001_initial'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'representative'
,
name
=
'active'
,
field
=
models
.
BooleanField
(
default
=
False
),
preserve_default
=
True
,
),
migrations
.
AddField
(
model_name
=
'representative'
,
name
=
'country'
,
field
=
models
.
ForeignKey
(
to
=
'representatives.Country'
,
null
=
True
),
preserve_default
=
True
,
),
]
legislature/models.py
View file @
a231f56e
...
...
@@ -3,18 +3,18 @@ import representatives
import
datetime
class
Representative
(
representatives
.
models
.
Representative
):
class
M
Representative
(
representatives
.
models
.
Representative
):
active
=
models
.
BooleanField
(
default
=
False
)
country
=
models
.
ForeignKey
(
representatives
.
models
.
Country
,
null
=
True
)
def
active_mandates
(
self
):
return
self
.
mandate_set
.
filter
(
active
=
True
)
return
self
.
m
mandate_set
.
filter
(
active
=
True
)
def
former_mandates
(
self
):
return
self
.
mandate_set
.
filter
(
active
=
False
)
return
self
.
m
mandate_set
.
filter
(
active
=
False
)
def
current_group
(
self
):
return
self
.
mandate_set
.
get
(
def
current_group
_mandate
(
self
):
return
self
.
m
mandate_set
.
get
(
active
=
True
,
group__kind
=
'group'
)
...
...
@@ -22,10 +22,10 @@ class Representative(representatives.models.Representative):
def
update_active
(
self
):
# If a representative has at least one active manadate
self
.
active
=
False
for
mandate
in
self
.
mandate_set
.
all
():
for
mandate
in
self
.
m
mandate_set
.
all
():
if
mandate
.
active
:
self
.
active
=
True
continue
break
self
.
save
()
...
...
@@ -33,7 +33,7 @@ class Representative(representatives.models.Representative):
# Create a country if it does not exist
# The representative's country is the one associated
# with the last 'country' mandate
country_mandate
=
self
.
mandate_set
.
filter
(
country_mandate
=
self
.
m
mandate_set
.
filter
(
group__kind
=
'country'
).
order_by
(
'-begin_date'
)[
0
:
1
].
get
()
...
...
@@ -45,7 +45,22 @@ class Representative(representatives.models.Representative):
self
.
save
()
class
Mandate
(
representatives
.
models
.
Mandate
):
class
MGroup
(
representatives
.
models
.
Group
):
active
=
models
.
BooleanField
(
default
=
False
)
def
update_active
(
self
):
self
.
active
=
False
for
mandate
in
self
.
mmandate_set
.
all
():
if
mandate
.
active
:
self
.
active
=
True
break
self
.
save
()
class
MMandate
(
representatives
.
models
.
Mandate
):
active
=
models
.
BooleanField
(
default
=
False
)
mgroup
=
models
.
ForeignKey
(
MGroup
)
mrepresentative
=
models
.
ForeignKey
(
MRepresentative
)
def
update_active
(
self
):
date
=
datetime
.
datetime
.
now
().
date
()
...
...
legislature/templates/legislature/group_list.haml
deleted
100644 → 0
View file @
305aaeee
-
extends
'base.html'
-
block
content
-
for
group
in
groups
{{ group.name }}
%br
legislature/templates/legislature/groups_list.haml
0 → 100644
View file @
a231f56e
-
extends
'base.html'
-
load
by_mandate_url
-
block
content
%ul
-
for
group
in
groups
%li
%a
{
'href'
:
'
{{
group
|
by_group_url
}}
'
}
-
if
group
.
abbreviation
{{ group.abbreviation }} .
{{ group.name }}
legislature/templates/legislature/representative_block.haml
View file @
a231f56e
...
...
@@ -9,9 +9,9 @@
={
representative
.
full_name
}
%td
%a
{
'href'
:
"{% url 'legislature:representatives_by_
mandate' mandate
_kind='country' search=representative.country.code %}"
}
%a
{
'href'
:
"{% url 'legislature:representatives_by_
group' group
_kind='country' search=representative.country.code %}"
}
={
representative
.
country
.
name
}
%td
%a
{
'href'
:
"{{ representative.current_group|by_mandate_url }}"
}
={
representative
.
current_group
.
group
.
abbreviation
}
%a
{
'href'
:
"{{ representative.current_group
_mandate
|by_mandate_url }}"
}
={
representative
.
current_group
_mandate
.
m
group
.
abbreviation
}
legislature/templates/legislature/representative_view.html
View file @
a231f56e
{% extends 'base.html' %}
{% load by_
mandate
_url %}
{% load by_
group
_url %}
{% block content %}
...
...
@@ -10,8 +10,8 @@
<h1>
{{ representative.full_name }}
</h1>
<p>
<strong>
<a
href=
"{{ representative.current_group|by_mandate_url }}"
>
{{ representative.current_group.role }} of {{ representative.current_group
.
group.name }}
<a
href=
"{{ representative.current_group
_mandate
|by_mandate_url }}"
>
{{ representative.current_group
_mandate
.role }} of {{ representative.current_group
_mandate.m
group.name }}
</a>
</strong>
</p>
...
...
legislature/templatetags/by_group_url.py
0 → 100644
View file @
a231f56e
from
django
import
template
from
django.core.urlresolvers
import
reverse
register
=
template
.
Library
()
@
register
.
filter
def
by_group_url
(
group
):
kwargs
=
{
'group_kind'
:
group
.
kind
}
if
group
.
abbreviation
:
kwargs
[
'search'
]
=
group
.
abbreviation
else
:
kwargs
[
'search'
]
=
group
.
name
return
reverse
(
'legislature:representatives_by_group'
,
kwargs
=
kwargs
)
@
register
.
filter
def
by_mandate_url
(
mandate
):
return
by_group_url
(
mandate
.
group
)
legislature/templatetags/by_mandate_url.py
View file @
a231f56e
...
...
@@ -5,15 +5,20 @@ register = template.Library()
@
register
.
filter
def
by_
mandate_url
(
mandate
):
kwargs
=
{
'
mandate
_kind'
:
mandate
.
group
.
kind
}
def
by_
group_url
(
group
):
kwargs
=
{
'
group
_kind'
:
group
.
kind
}
if
mandate
.
group
.
abbreviation
:
kwargs
[
'search'
]
=
mandate
.
group
.
abbreviation
if
group
.
abbreviation
:
kwargs
[
'search'
]
=
group
.
abbreviation
else
:
kwargs
[
'search'
]
=
mandate
.
group
.
name
kwargs
[
'search'
]
=
group
.
name
return
reverse
(
'legislature:representatives_by_
mandate
'
,
'legislature:representatives_by_
group
'
,
kwargs
=
kwargs
)
@
register
.
filter
def
by_mandate_url
(
mandate
):
return
by_group_url
(
mandate
.
group
)
legislature/urls.py
View file @
a231f56e
...
...
@@ -15,9 +15,9 @@ urlpatterns = patterns(
name
=
'representative_view'
),
url
(
r
'^representatives/(?P<
mandate
_kind>\w+)/(?P<search>.+)$'
,
views
.
representatives_by_
mandate
,
name
=
'representatives_by_
mandate
'
r
'^representatives/(?P<
group
_kind>\w+)/(?P<search>.+)$'
,
views
.
representatives_by_
group
,
name
=
'representatives_by_
group
'
),
url
(
r
'^representatives/(?P<name>.+)$'
,
...
...
@@ -25,8 +25,8 @@ urlpatterns = patterns(
name
=
'representative_view_by_name'
),
url
(
r
'^group/(?P<kind>\w+)$'
,
views
.
group_by_kind
,
name
=
'group_by_kind'
r
'^group
s
/(?P<kind>\w+)$'
,
views
.
group
s
_by_kind
,
name
=
'group
s
_by_kind'
)
)
legislature/views.py
View file @
a231f56e
...
...
@@ -2,14 +2,13 @@ from django.shortcuts import render, get_object_or_404
from
django.core.paginator
import
Paginator
,
EmptyPage
,
PageNotAnInteger
from
django.db.models
import
Q
from
legislature.models
import
Representative
from
representatives.models
import
Group
from
legislature.models
import
MRepresentative
,
MGroup
def
representatives_index
(
request
):
representative_list
=
_filter_by_search
(
request
,
Representative
.
objects
.
all
()
M
Representative
.
objects
.
all
()
)
return
_render_list
(
request
,
representative_list
)
...
...
@@ -17,7 +16,7 @@ def representatives_index(request):
def
representative_by_name
(
request
,
name
):
representative
=
get_object_or_404
(
Representative
,
full_name
=
name
)
M
Representative
,
full_name
=
name
)
return
render
(
request
,
'legislature/representative_view.html'
,
...
...
@@ -26,7 +25,7 @@ def representative_by_name(request, name):
def
representative_view
(
request
,
num
):
representative
=
get_object_or_404
(
Representative
,
pk
=
num
)
representative
=
get_object_or_404
(
M
Representative
,
pk
=
num
)
return
render
(
request
,
...
...
@@ -35,36 +34,36 @@ def representative_view(request, num):
)
def
representatives_by_
mandate
(
request
,
mandate_kind
,
mandate
_abbr
=
None
,
mandate
_name
=
None
,
search
=
None
):
if
mandate
_abbr
:
representative_list
=
Representative
.
objects
.
filter
(
mandate__group__abbreviation
=
mandate
_abbr
,
mandate__group__kind
=
mandate
_kind
,
mandate__active
=
True
def
representatives_by_
group
(
request
,
group_kind
,
group
_abbr
=
None
,
group
_name
=
None
,
search
=
None
):
if
group
_abbr
:
representative_list
=
M
Representative
.
objects
.
filter
(
m
mandate__
m
group__abbreviation
=
group
_abbr
,
m
mandate__
m
group__kind
=
group
_kind
,
m
mandate__active
=
True
)
elif
mandate
_name
:
representative_list
=
Representative
.
objects
.
filter
(
Q
(
mandate__group__name__icontains
=
mandate
_name
),
mandate__group__kind
=
mandate
_kind
,
mandate__active
=
True
elif
group
_name
:
representative_list
=
M
Representative
.
objects
.
filter
(
Q
(
m
mandate__
m
group__name__icontains
=
group
_name
),
m
mandate__
m
group__kind
=
group
_kind
,
m
mandate__active
=
True
)
elif
search
:
try
:
Group
.
objects
.
get
(
abbreviation
=
search
,
kind
=
mandate
_kind
)
representative_list
=
Representative
.
objects
.
filter
(
mandate__group__abbreviation
=
search
,
mandate__group__kind
=
mandate
_kind
,
mandate__active
=
True
M
Group
.
objects
.
get
(
abbreviation
=
search
,
kind
=
group
_kind
)
representative_list
=
M
Representative
.
objects
.
filter
(
m
mandate__
m
group__abbreviation
=
search
,
m
mandate__
m
group__kind
=
group
_kind
,
m
mandate__active
=
True
)
except
Group
.
DoesNotExist
:
representative_list
=
Representative
.
objects
.
filter
(
Q
(
mandate__group__abbreviation__icontains
=
search
)
|
Q
(
mandate__group__name__icontains
=
search
),
mandate__group__kind
=
mandate
_kind
,
mandate__active
=
True
except
M
Group
.
DoesNotExist
:
representative_list
=
M
Representative
.
objects
.
filter
(
Q
(
m
mandate__
m
group__abbreviation__icontains
=
search
)
|
Q
(
m
mandate__
m
group__name__icontains
=
search
),
m
mandate__
m
group__kind
=
group
_kind
,
m
mandate__active
=
True
)
# Select distinct representatives and filter by search
...
...
@@ -89,7 +88,7 @@ def _filter_by_search(request, representative_list):
return
representative_list
def
_render_list
(
request
,
representative_list
,
num_by_page
=
5
0
):
def
_render_list
(
request
,
representative_list
,
num_by_page
=
3
0
):
"""
Render a paginated list of representatives
"""
...
...
@@ -118,13 +117,14 @@ def _render_list(request, representative_list, num_by_page=50):
)
def
group_by_kind
(
request
,
kind
):
groups
=
Group
.
objects
.
filter
(
kind
=
kind
def
groups_by_kind
(
request
,
kind
):
groups
=
MGroup
.
objects
.
filter
(
kind
=
kind
,
active
=
True
)
return
render
(
request
,
'legislature/group_list.html'
,
'legislature/group
s
_list.html'
,
{
'groups'
:
groups
}
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment