Skip to content
GitLab
Menu
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
305aaeee
Commit
305aaeee
authored
Mar 20, 2015
by
luxcem
Browse files
adds methods to legislatures models
parent
d2564c1e
Changes
8
Show whitespace changes
Inline
Side-by-side
legislature/management/commands/legislature_updates_models.py
0 → 100644
View file @
305aaeee
# import datetime
from
django.core.management.base
import
BaseCommand
from
representatives
import
models
from
legislature.models
import
Representative
,
Mandate
from
django.db
import
transaction
class
Command
(
BaseCommand
):
@
transaction
.
atomic
def
handle
(
self
,
*
args
,
**
options
):
# Representatives
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
()
print
(
"%s/%s
\r
"
%
(
i
,
n
)),
print
(
'Mandates'
)
for
i
,
representative
in
enumerate
(
Representative
.
objects
.
all
()):
legislature_mandates
=
[]
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
()
print
(
"%s/%s
\r
"
%
(
i
,
n
)),
legislature/migrations/0001_initial.py
0 → 100644
View file @
305aaeee
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
models
,
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'representatives'
,
'0004_representative_country'
),
]
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'
,
fields
=
[
(
'group_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Group'
)),
],
options
=
{
},
bases
=
(
'representatives.group'
,),
),
migrations
.
CreateModel
(
name
=
'Mandate'
,
fields
=
[
(
'mandate_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Mandate'
)),
],
options
=
{
},
bases
=
(
'representatives.mandate'
,),
),
migrations
.
CreateModel
(
name
=
'Representative'
,
fields
=
[
(
'representative_ptr'
,
models
.
OneToOneField
(
parent_link
=
True
,
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
to
=
'representatives.Representative'
)),
],
options
=
{
},
bases
=
(
'representatives.representative'
,),
),
]
legislature/migrations/0002_auto_20150319_1620.py
0 → 100644
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/migrations/__init__.py
0 → 100644
View file @
305aaeee
legislature/models.py
View file @
305aaeee
from
django.db
import
models
import
representatives
import
datetime
class
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
)
def
former_mandates
(
self
):
return
self
.
mandate_set
.
filter
(
active
=
False
)
def
current_group
(
self
):
return
self
.
mandate_set
.
get
(
active
=
True
,
group__kind
=
'group'
)
def
update_active
(
self
):
# If a representative has at least one active manadate
self
.
active
=
False
for
mandate
in
self
.
mandate_set
.
all
():
if
mandate
.
active
:
self
.
active
=
True
continue
self
.
save
()
def
update_country
(
self
):
# 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
(
group__kind
=
'country'
).
order_by
(
'-begin_date'
)[
0
:
1
].
get
()
country
,
created
=
representatives
.
models
.
Country
.
objects
.
get_or_create
(
name
=
country_mandate
.
group
.
name
,
code
=
country_mandate
.
group
.
abbreviation
)
self
.
country
=
country
self
.
save
()
class
Mandate
(
representatives
.
models
.
Mandate
):
def
update_active
(
self
):
date
=
datetime
.
datetime
.
now
().
date
()
self
.
active
=
self
.
end_date
>
date
self
.
save
()
legislature/templates/legislature/representative_block.haml
View file @
305aaeee
%td<
-
load
by_mandate_url
%td
%a
{
'href'
:
"{% url 'legislature:representative_view_by_name' representative.full_name %}"
}
%img
{
'src'
:
'
=
{
representative
.
photo
}
'
}
/
%img
{
'src'
:
'
=
{
representative
.
photo
}
'
, '
width
': '
80
'
}
/
%td
<
%td
%a
{
'href'
:
"{% url 'legislature:representative_view_by_name' representative.full_name %}"
}
={
representative
.
full_name
}
[
=
{
representative
.
country
.
code
}]
={
representative
.
full_name
}
%td
%a
{
'href'
:
"{% url 'legislature:representatives_by_mandate' mandate_kind='country' search=representative.country.code %}"
}
={
representative
.
country
.
name
}
%td
%a
{
'href'
:
"{{ representative.current_group|by_mandate_url }}"
}
={
representative
.
current_group
.
group
.
abbreviation
}
legislature/templates/legislature/representatives_list.haml
View file @
305aaeee
-
extends
"base.html"
-
block
content
-
include
'legislature/search.html'
%p
...
...
legislature/views.py
View file @
305aaeee
...
...
@@ -2,7 +2,8 @@ from django.shortcuts import render, get_object_or_404
from
django.core.paginator
import
Paginator
,
EmptyPage
,
PageNotAnInteger
from
django.db.models
import
Q
from
representatives.models
import
Representative
,
Group
from
legislature.models
import
Representative
from
representatives.models
import
Group
def
representatives_index
(
request
):
...
...
@@ -51,6 +52,14 @@ def representatives_by_mandate(request, mandate_kind, mandate_abbr=None,
)
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
)
except
Group
.
DoesNotExist
:
representative_list
=
Representative
.
objects
.
filter
(
Q
(
mandate__group__abbreviation__icontains
=
search
)
|
Q
(
mandate__group__name__icontains
=
search
),
...
...
@@ -101,6 +110,7 @@ def _render_list(request, representative_list, num_by_page=50):
context
[
'representatives'
]
=
representatives
context
[
'representative_num'
]
=
paginator
.
count
return
render
(
request
,
'legislature/representatives_list.html'
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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