本文整理汇总了Python中nuorisovaalit.models.DBSession类的典型用法代码示例。如果您正苦于以下问题:Python DBSession类的具体用法?Python DBSession怎么用?Python DBSession使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DBSession类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_vote__returned_options
def test_vote__returned_options(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.views.voting import vote
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.url import route_url
session = DBSession()
populate_testing_db()
voter = session.query(Voter).first()
self.config.add_route('select', '/valitse')
self.config.testing_securitypolicy(userid=voter.openid)
self.config.set_session_factory(UnencryptedCookieSessionFactoryConfig)
request = DummyRequest()
csrf_token = request.session.new_csrf_token()
request.matchdict['number'] = '1'
options = vote(request)
self.assertEquals({
'action_url': request.path_url,
'select_url': route_url('select', request),
'candidate': {
'number': 1,
'name': u'Turhapuro, Uuno',
},
'profile': {
'fullname': u'Matti Meikäläinen',
'district': u'Ahvenanmaan maakunnan vaalipiiri',
},
'error': False,
'csrf_token': csrf_token
}, options)
示例2: test_valid_voting_process__accept_openid
def test_valid_voting_process__accept_openid(self):
from nuorisovaalit.models import Voter
# Fake the OpenID authentication.
self.authenticate(u"http://example.com/id/matti.meikalainen")
# Select a candidate.
response = self.testapp.get("/valitse")
response = response.click(href="/aanesta/1$")
# Vote for the candidate.
response.form[u"vote"] = u"1"
response = response.form.submit()
# Follow the redirect to the OpenID preference page.
response = response.follow()
# Fill in valid information and choose to keep the OpenID.
response.form[u"gsm"] = u"0401234567"
response.form[u"email"] = u"[email protected]"
response.form[u"street"] = u"Söme street 16"
response.form[u"zipcode"] = u"01234"
response.form[u"city"] = u"Söme city, Länd"
response.form[u"use_open_identity"] = u"yes"
response = response.form.submit()
self.assertEquals("302 Found", response.status)
response = response.follow()
self.assertEquals("200 OK", response.status)
# Assert that the voter information is correct.
session = DBSession()
voter = session.query(Voter).filter_by(openid=u"http://example.com/id/matti.meikalainen").one()
self.assertTrue(voter.accept_openid)
self.assertEquals(u"0401234567", voter.gsm)
self.assertEquals(u"[email protected]", voter.email)
self.assertEquals(u"Söme street 16, 01234, Söme city, Länd", voter.address)
示例3: test_vote_finish__declined
def test_vote_finish__declined(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.views.voting import vote_finish
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from webob.exc import HTTPFound
session = DBSession()
populate_testing_db()
voter = session.query(Voter).first()
self.assertEquals(False, voter.has_preference())
self.config.set_session_factory(UnencryptedCookieSessionFactoryConfig)
self.config.testing_securitypolicy(userid=voter.openid)
self.config.add_route('close-window', '/close-window')
self.config.add_route('vote-finish', '/valmis')
request = DummyRequest()
csrf_token = request.session.new_csrf_token()
request.POST = {
'form.submitted': u'1',
'csrf_token': csrf_token,
'use_open_identity': u'no',
}
response = vote_finish(request)
self.assertTrue(isinstance(response, HTTPFound))
self.assertEquals('http://example.com/close-window', response.location)
self.assertEquals(False, voter.accept_openid)
self.assertEquals(True, voter.has_preference())
示例4: test_valid_voting_process__decline_openid
def test_valid_voting_process__decline_openid(self):
from nuorisovaalit.models import Vote
from nuorisovaalit.models import Voter
from nuorisovaalit.models import VotingLog
# Fake the OpenID authentication.
self.authenticate(u"http://example.com/id/matti.meikalainen")
# Select a candidate
response = self.testapp.get("/valitse")
self.assertEquals("200 OK", response.status)
# Select a candidate
response = response.click(href="/aanesta/1$")
self.assertEquals("200 OK", response.status)
# Assert we are showing the correct voter information
self.assertEquals(
[tag.text for tag in response.html.find("table", id="voter").findAll("td")],
[u"Matti Meikäläinen", u"Ahvenanmaan maakunnan vaalipiiri"],
)
# Assert we got the candidate we selected
self.assertEquals(response.html.find("p", "candidate-name").text, u"1 Turhapuro, Uuno")
# Vote for the candidate
response.form[u"vote"] = u"1"
response = response.form.submit()
# The form submission uses a redirect to avoid accidental resubmissions.
self.assertEquals("302 Found", response.status)
response = response.follow()
self.assertEquals("200 OK", response.status)
# Assert the vote was registered
self.assertEquals(response.html.find("h1").text, u"Äänesi on tallennettu")
session = DBSession()
self.assertEquals(
1, session.query(VotingLog).join(Voter).filter_by(openid=u"http://example.com/id/matti.meikalainen").count()
)
self.assertEquals(1, session.query(Vote).count())
# Submit the OpenID preference form with default values which will
# decline the offer to use OpenID in the future.
response = response.form.submit()
self.assertEquals("302 Found", response.status)
response = response.follow()
self.assertEquals("200 OK", response.status)
# Assert that the accept_openid is False for the voter.
session = DBSession()
voter = session.query(Voter).filter_by(openid=u"http://example.com/id/matti.meikalainen").one()
self.assertFalse(voter.accept_openid)
# Assert we got logged out.
self.assertEquals(self.testapp.cookies["auth_tkt"], "")
# Assert we are suggested to close the browser window.
self.assertEquals(response.html.h1.text, u"Sulje ikkuna")
示例5: openid_response
def openid_response(request):
"""Processes the response of the OpenID provider and upon successful
response authenticates the user.
In order for a user to be authenticated the following criteria must be
met:
* the OpenID provider has responded with a positive assertation.
* the verified OpenID identity must match an existing
:py:attr:`nuorisovaalit.models.Voter.openid` record.
In case the above criteria are not met the user is presented with an error
page.
:param request: The currently active request.
:type request: :py:class:`pyramid.request.Request`
"""
log = logging.getLogger('nuorisovaalit')
mode = request.params.get('openid.mode', None)
if mode == 'id_res':
# We received a positive authentication response.
consumer = make_consumer(request)
response = consumer.complete(request.params, request.url)
if response.status == SUCCESS:
identity = response.identity_url.rstrip('/')
session = DBSession()
user = session.query(Voter).filter(Voter.openid == identity).first()
if user is None:
log.warn('Failed to authenticate "{0}": unknown user.'.format(identity))
return openid_failure(request)
log.info('Authenticated "{0}".'.format(identity))
if user.has_voted():
url = route_url('vote-finish', request)
request.session['vote_registered'] = 'yes'
log.info('User "{0}" has already voted.'.format(identity))
else:
url = route_url('select', request)
headers = remember(request, identity)
return HTTPFound(location=url, headers=headers)
else:
log.warn('Failed to authenticate "{0}".'.format(response.identity_url))
return openid_failure(request)
elif mode == 'cancel':
# We received an explicit negative authentication response.
log.info('OpenID authentication canceled.')
return openid_canceled(request)
else:
log.warn('Unknown OpenID response mode: {0}.'.format(mode))
return openid_failure(request)
示例6: _add_coalition
def _add_coalition(self, name, district, *parties):
"""Creates a new coalition containing the given parties."""
from nuorisovaalit.models import Coalition
session = DBSession()
coalition = Coalition(name, district)
for party in parties:
coalition.parties.append(party)
session.add(coalition)
session.flush()
示例7: test_coalition__multiple_coalitions_in_different_districts
def test_coalition__multiple_coalitions_in_different_districts(self):
from nuorisovaalit.models import Coalition
from nuorisovaalit.models import District
from nuorisovaalit.models import Party
district_x = District(u'District X', 1)
district_y = District(u'District Y', 2)
party = Party(u'Foobar')
session = DBSession()
session.add_all([district_x, district_y, party])
session.flush()
coalition_x = Coalition(u'Reds', district_x)
coalition_y = Coalition(u'Blues', district_y)
session.add_all([coalition_x, coalition_y])
coalition_x.parties.append(party)
coalition_y.parties.append(party)
session.flush()
self.assertEquals(coalition_x, party.coalition(district_x))
self.assertEquals(coalition_y, party.coalition(district_y))
示例8: test_coalition__no_coalition
def test_coalition__no_coalition(self):
from nuorisovaalit.models import District
from nuorisovaalit.models import Party
district = District(u'District X', 1)
party = Party(u'Foobar')
session = DBSession()
session.add_all([district, party])
session.flush()
self.assertEquals(None, party.coalition(district))
示例9: test_has_voted__negative
def test_has_voted__negative(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.models import VotingLog
session = DBSession()
populate_testing_db()
# Make sure no-one has voted
self.assertEquals(0, session.query(VotingLog).count())
# Make sure that a given user has not voted.
voter = session.query(Voter).first()
self.failIf(voter.has_voted())
示例10: create_districts
def create_districts():
"""Populates the database with information about the voting districts.
"""
session = DBSession()
districts = parse_districts(
seats=datafile("../data/paikkamaarat.txt"), municipalities=datafile("../data/luokitusavain_kunta_teksti.txt")
)
# Create voting districts and municipalities
for (distname, code, seats), municipalities in districts.iteritems():
session.add(District(u"{0} vaalipiiri".format(distname), code, seats))
session.flush()
示例11: test_vote_finish__accepted_empty_fields
def test_vote_finish__accepted_empty_fields(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.views.voting import vote_finish
from pyramid.session import UnencryptedCookieSessionFactoryConfig
session = DBSession()
populate_testing_db()
voter = session.query(Voter).first()
self.assertEquals(False, voter.has_preference())
self.config.set_session_factory(UnencryptedCookieSessionFactoryConfig)
self.config.testing_securitypolicy(userid=voter.openid)
self.config.add_route('close-window', '/close-window')
self.config.add_route('vote-finish', '/valmis')
request = DummyRequest()
csrf_token = request.session.new_csrf_token()
request.POST = {
'csrf_token': csrf_token,
'use_open_identity': 'yes',
'gsm': u'',
'email': u'',
'street': u'',
'zipcode': u'',
'city': u'',
'form.submitted': u'1',
}
self.assertEquals({
'action_url': 'http://example.com/valmis',
'csrf_token': csrf_token,
'accept_openid': True,
'message': u'Äänesi on tallennettu',
'has_preference': False,
'pref_selected': True,
'errors': [
u'GSM-numero on virheellinen, esimerkki oikeasta muodosta "0501234567".',
u'Sähköpostiosoite on virheellinen, esimerkki oikeasta muodosta "[email protected]".',
u'Katuosoite puuttuu.',
u'Postinumero on virheellinen, esimerkki oikeasta muodosta "12345".',
u'Postitoimipaikka puuttuu.',
],
'voter': voter,
'gsm': u'',
'email': u'',
'street': u'',
'zipcode': u'',
'city': u'',
}, vote_finish(request))
self.assertEquals(False, voter.has_preference())
示例12: test_vote__invalid_candidate_number
def test_vote__invalid_candidate_number(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.views.voting import vote
from pyramid.exceptions import NotFound
session = DBSession()
populate_testing_db()
voter = session.query(Voter).first()
self.config.testing_securitypolicy(userid=voter.openid)
request = DummyRequest()
request.matchdict['number'] = 666
self.assertRaises(NotFound, lambda: vote(request))
示例13: test_vote_finish__message_vote_saved
def test_vote_finish__message_vote_saved(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.views.voting import vote_finish
from pyramid.session import UnencryptedCookieSessionFactoryConfig
session = DBSession()
populate_testing_db()
voter = session.query(Voter).first()
self.config.set_session_factory(UnencryptedCookieSessionFactoryConfig)
self.config.testing_securitypolicy(userid=voter.openid)
self.config.add_route('vote-finish', '/valmis')
self.assertEquals(u'Äänesi on tallennettu', vote_finish(DummyRequest())['message'])
示例14: populate_demo
def populate_demo():
"""Populates a database with demo content."""
config = get_config()
engine = engine_from_config(config, "sqlalchemy.")
initialize_sql(engine)
engine.echo = False
session = DBSession()
districts = session.query(District).order_by(District.code)
provider = urlparse(config["nuorisovaalit.openid_provider"])
provider_tmpl = u"{0}://{{0}}.{1}".format(provider.scheme, provider.netloc)
# Create voters
for district in districts:
username = u"test.user{0:02}".format(district.code)
school = School(u"Koulu vaalipiirissä {0}".format(district.name), district)
session.add(school)
session.flush()
session.add(
Voter(
provider_tmpl.format(username),
u"Testi",
u"Käyttäjä #{0:02}".format(district.code),
date(1990, 4, 5),
u"040 123 4567",
u"[email protected]",
u"Nowhere street 5, 00000 Helsinki",
school,
)
)
transaction.commit()
print ("Created demo structure.")
示例15: test_vote_finish__message_already_voted
def test_vote_finish__message_already_voted(self):
from nuorisovaalit.models import Voter
from nuorisovaalit.views.voting import vote_finish
from pyramid.session import UnencryptedCookieSessionFactoryConfig
session = DBSession()
populate_testing_db()
voter = session.query(Voter).first()
self.config.set_session_factory(UnencryptedCookieSessionFactoryConfig)
self.config.testing_securitypolicy(userid=voter.openid)
self.config.add_route('vote-finish', '/valmis')
request = DummyRequest()
request.session['vote_registered'] = 'yes'
self.assertEquals(u'Olet jo äänestänyt', vote_finish(request)['message'])