elections2/controllers/root.py | 5
elections2/lib/admin.py | 21 ++-
elections2/model/__init__.py | 222 +++++++++++++++++++++++++++++------------
3 files changed, 173 insertions(+), 75 deletions(-)
New commits:
commit 9cee6a800b052fa48ce3ad20b573b9fcc6040e50
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Sun Dec 4 21:52:38 2011 +0100
Start porting the model to SA
diff --git a/elections2/model/__init__.py b/elections2/model/__init__.py
index c6df12a..c9dce69 100644
--- a/elections2/model/__init__.py
+++ b/elections2/model/__init__.py
@@ -8,8 +8,6 @@ from sqlalchemy.types import Unicode, Integer, DateTime, String
#from sqlalchemy import MetaData
from sqlalchemy.ext.declarative import declarative_base
-from fedora.tg.json import SABase
-
# Global session manager: DBSession() returns the Thread-local
# session object appropriate for the current web request.
maker = sessionmaker(autoflush=True, autocommit=False,
@@ -53,48 +51,8 @@ def init_model(engine):
# use the model outside tg2, you need to make sure this is called before
# you use the model.
- #
- # See the following example:
-
- #global t_reflected
-
- #t_reflected = Table("Reflected", metadata,
- # autoload=True, autoload_with=engine)
- ElectionsTable = Table('elections', metadata, autoload=True)
- VotesTable = Table('votes', metadata, autoload=True)
- CandidatesTable = Table('candidates', metadata, autoload=True)
- LegalVotersTable = Table('legalvoters', metadata, autoload=True)
- ElectionAdminsTable = Table('electionadmins', metadata, autoload=True)
-
# View in the DB. Needs to have the column keys defined
- VoteTallyTable = Table('fvotecount', metadata,
- Column('id', Integer,
- ForeignKey('candidates.id'), primary_key=True),
- Column('election_id', Integer),
- Column('name', String, nullable=False),
- Column('novotes', Integer, nullable=False)
- )
- UserVoteCountTable = Table('uservotes', metadata,
- Column('election_id', Integer, ForeignKey('elections.id'), primary_key=True),
- Column('voter', String, nullable=False, primary_key=True),
- Column('novotes', Integer, nullable=False)
- )
-
- #mapper(Reflected, t_reflected)
- mapper(Elections, ElectionsTable, properties = {
- 'legalVoters': relation(LegalVoters, backref='election'),
- 'candidates': relation(Candidates, backref='election'),
- 'uservotes': relation(UserVoteCount, backref='election')
- })
- mapper(Votes, VotesTable)
- mapper(Candidates, CandidatesTable, properties = {
- 'votes': relation(Votes, backref='candidate'),
- 'tally': relation(VoteTally, backref='candidate')
- })
- mapper(LegalVoters, LegalVotersTable)
- mapper(ElectionAdmins, ElectionAdminsTable)
- mapper(VoteTally, VoteTallyTable)
- mapper(UserVoteCount, UserVoteCountTable)
+
# Import your model modules here.
from elections2.model.auth import User, Group, Permission
@@ -103,23 +61,163 @@ from elections2.model.auth import User, Group, Permission
# Classes to map to
#
-class Elections(SABase):
- pass
-
-class Votes(SABase):
- pass
-
-class Candidates(SABase):
- pass
-
-class LegalVoters(SABase):
- pass
-
-class ElectionAdmins(SABase):
- pass
-
-class VoteTally(SABase):
- pass
-class UserVoteCount(SABase):
- pass
+class LegalVoters(DeclarativeBase):
+ """
+ Define the voters.
+ """
+ __tablename__ = 'legalvoters'
+
+ #{ Columns
+ id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
+ election_id = Column(Integer, ForeignKey('elections.id'), nullable=False)
+ group_name = Column(Unicode(150), nullable=False)
+
+ #{ Special methods
+ def __repr__(self):
+ return '<LegalVoters: group=%r>' % self.group_name
+
+ def __unicode__(self):
+ return self.group_name
+ #}
+
+
+class ElectionAdmins(DeclarativeBase):
+ """
+ Define the Admins.
+ """
+ __tablename__ = 'electionadmins'
+
+ #{ Columns
+ id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
+ election_id = Column(Integer, ForeignKey('elections.id'), nullable=False)
+ group_name = Column(Unicode(150), nullable=False)
+
+ #{ Special methods
+ def __repr__(self):
+ return '<ElectionAdmins: group=%r>' % self.group_name
+
+ def __unicode__(self):
+ return self.group_name
+ #}
+
+class VoteTally(DeclarativeBase):
+ __tablename__ = 'fvotecount'
+
+ #{ Columns
+ id = Column(Integer, ForeignKey('candidates.id'), primary_key=True)
+ name = Column(String, nullable=False)
+ novotes = Column(Integer, nullable=False)
+
+ #{ Special methods
+ def __repr__(self):
+ return '<VoteTally: id=%r election=%r group=%r>' % ( self.id,
+ self.election_id, self.group_name)
+
+ def __unicode__(self):
+ return self.group_name
+ #}
+
+class UserVoteCount(DeclarativeBase):
+
+ __tablename__ = 'uservotes'
+
+ #{ Columns
+ election_id = Column(Integer, ForeignKey('elections.id'), primary_key=True)
+ voter = Column(String, nullable=False, primary_key=True)
+ novotes = Column(Integer, nullable=False)
+
+ #{ Special methods
+ def __repr__(self):
+ return '<UserVoteCount: id=%r voter=%r novotes=%r>' % (
+ self.election_id, self.voter, self.novotes)
+
+ def __unicode__(self):
+ return self.group_name
+ #}
+
+class Elections(DeclarativeBase):
+ """
+ Define the different elections.
+ """
+ __tablename__ = 'elections'
+
+ #{ Columns
+ id = Column(Integer, autoincrement=True, primary_key=True)
+ shortdesc = Column(Unicode(150), unique=True, nullable=False)
+ alias = Column(Unicode(150), unique=True, nullable=False)
+ description = Column(Unicode(150), unique=True, nullable=False)
+ url = Column(Unicode(150), unique=True, nullable=False)
+ start_date = Column(DateTime, nullable=False)
+ end_date = Column(DateTime, nullable=False)
+ seats_elected = Column(Integer, nullable=False)
+ votes_per_user = Column(Integer, nullable=False)
+ embargoed = Column(Integer, nullable=False, default=0)
+ usefas = Column(Integer, nullable=False, default=0)
+ allow_nominations = Column(Integer, nullable=False, default=0)
+ nominations_until = Column(DateTime)
+
+ #{ Relations
+ legalVoters = relation('LegalVoters')
+ candidates = relation('Candidates')
+ uservotes = relation('UserVoteCount')
+
+ #{ Special methods
+ def __repr__(self):
+ return '<Elections: election=%r>' % self.shortdesc
+
+ def __unicode__(self):
+ return self.shortdesc
+ #}
+
+
+class Votes(DeclarativeBase):
+ """
+ Define the different votes.
+ """
+ __tablename__ = 'votes'
+
+ #{ Columns
+ id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
+ election_id = Column(Integer, ForeignKey('elections.id'), nullable=False)
+ voter = Column(Unicode(150), nullable=False)
+ timestamp = Column(DateTime, nullable=False)
+ candidate_id = Column(Integer, ForeignKey('candidates.id'), nullable=False)
+ weight = Column(Integer, nullable=False)
+
+ #{ Special methods
+ def __repr__(self):
+ return '<Votes: voter=%r>' % self.voter
+
+ def __unicode__(self):
+ return self.voter
+ #}
+
+
+class Candidates(DeclarativeBase):
+ """
+ Define the candidates.
+ """
+ __tablename__ = 'candidates'
+
+ #{ Columns
+ id = Column(Integer, autoincrement=True, primary_key=True)
+ election_id = Column(Integer, ForeignKey('elections.id'),
+ primary_key=True, nullable=False)
+ name = Column(Unicode(150), nullable=False)
+ url = Column(Unicode(150))
+ formalname = Column(Unicode(150), nullable=True)
+ human = Column(Integer)
+ status = Column(Integer)
+
+ #{ Relations
+ #votes = relation('Votes', backref='candidate'),
+ #tally = relation('VoteTally', backref='candidate')
+
+ #{ Special methods
+ def __repr__(self):
+ return '<Candidates: candidate=%r>' % self.name
+
+ def __unicode__(self):
+ return self.name
+ #}
commit 4377aeeb9fe9f4577bbff24849c126e0251b1b7d
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Sun Dec 4 21:50:00 2011 +0100
Remove prints and rework the entry into the database of the dates
diff --git a/elections2/lib/admin.py b/elections2/lib/admin.py
index 9a44b61..126c7ae 100644
--- a/elections2/lib/admin.py
+++ b/elections2/lib/admin.py
@@ -42,13 +42,12 @@ import sqlalchemy, tg
#from turbogears.database import session
-identity = request.environ.get('repoze.who.identity')
+request.identity = request.environ.get('repoze.who.identity')
class Admin(BaseController):
allow_only = predicates.in_group(config.get('admingroup', 'elections'))
def __init__(self, fas, appTitle):
- #print dir(fas), fas.username
self.fas = fas
self.appTitle = appTitle
@@ -106,8 +105,10 @@ class Admin(BaseController):
election.shortdesc=kw['shortdesc']
election.description=kw['info']
election.url=kw['url']
- election.start_date=kw['startdate']
- election.end_date=kw['enddate']
+ election.start_date= datetime.strptime(kw['startdate'],
+ "%Y-%m-%d %H:%M:%S")
+ election.end_date=datetime.strptime(kw['enddate'],
+ "%Y-%m-%d %H:%M:%S")
election.embargoed=setembargo
election.seats_elected=kw['seats']
election.usefas=usefas
@@ -174,7 +175,7 @@ class Admin(BaseController):
candidate = entry.split("!")
#Python doesn't have a good way of doing case/switch statements
if len(candidate) == 1:
- if len(candidate[0]) :
+ if len(candidate[0]):
c=Candidates()
c.election_id=kw['id']
c.name=candidate[0]
@@ -182,10 +183,10 @@ class Admin(BaseController):
c.human=1
model.DBSession.add(c)
elif len(candidate) == 2:
- if len(candidate[0]) :
+ if len(candidate[0]):
c = Candidates()
c.election_id=kw['id']
- c.name=candidate[0],
+ c.name=candidate[0]
c.url=candidate[1]
c.status=0
c.human=1
@@ -214,8 +215,10 @@ class Admin(BaseController):
election.shortdesc=kw['shortdesc']
election.description=kw['info']
election.url=kw['url']
- election.start_date=kw['startdate']
- election.end_date=kw['enddate']
+ election.start_date=election.end_date=datetime.strptime(kw['startdate'],
+ "%Y-%m-%d %H:%M:%S")
+ election.end_date=election.end_date=datetime.strptime(kw['enddate'],
+ "%Y-%m-%d %H:%M:%S")
election.embargoed=setembargo
election.seats_elected=kw['seats']
election.usefas=usefas
commit 9590908bdea3b3a004fbb511ca0620d6c0a01f21
Author: Pierre-Yves Chibon <pingou(a)pingoured.fr>
Date: Sun Dec 4 21:49:05 2011 +0100
Remove old prints and comment out admin not used
diff --git a/elections2/controllers/root.py b/elections2/controllers/root.py
index 83d4b60..430d6b7 100644
--- a/elections2/controllers/root.py
+++ b/elections2/controllers/root.py
@@ -43,7 +43,7 @@ class RootController(BaseController):
"""
secc = SecureController()
- admin = AdminController(model, DBSession, config_type=TGAdminConfig)
+ #admin = AdminController(model, DBSession, config_type=TGAdminConfig)
error = ErrorController()
@@ -176,7 +176,6 @@ class RootController(BaseController):
login_dict['login_counter'] = str(login_counter)
login_dict['page'] = 'login'
login_dict['came_from'] = came_from
- print csrf_login
return login_dict
@@ -252,12 +251,10 @@ class RootController(BaseController):
"""
## default code from TG2:
- print request.identity
if not request.identity:
login_counter = request.environ['repoze.who.logins'] + 1
redirect('/login', came_from=came_from, __logins=login_counter)
userid = request.identity['repoze.who.userid']
- print request.identity.keys()
flash(_('Welcome back, %s!') % userid)
redirect(came_from)