def setUp(self): self.config = testing.setUp() self.config.include('pyramid_mailer.testing') #DBSession.remove() #DBSession = _initTestingDB() my_settings = { 'sqlalchemy.url': 'sqlite:///test_webtest.db', # the database 'available_languages': 'de en', 'speedfunding.dashboard_number': 20, } from speedfunding import main #try: app = main({}, **my_settings) from sqlalchemy import create_engine engine = create_engine('sqlite:///test_webtest.db') DBSession.configure(bind=engine) from speedfunding.models import Base Base.metadata.create_all(engine) _populate_testDB() #import pdb; pdb.set_trace() #except: # app = main({}, **my_other_settings) # pass from webtest import TestApp self.testapp = TestApp(app)
def tearDown(self): DBSession.remove() testing.tearDown() try: os.remove('test_utils.db') except: print("file not found: test_utils.db") pass
def tearDown(self): # maybe I need to check and remove globals here, # so the other tests are not compromised #del engine # from c3smembership.models import DBSession # DBSession.close() # DBSession.remove() DBSession.remove() testing.tearDown()
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from speedfunding.models import ( Base, #Speedfundings, #Group, #C3sStaff, #TheTotal ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) self.initialize_db()
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite:///test_utils.db') from speedfunding.models import ( Base, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: model1 = Speedfundings( # english firstname=u'first', lastname=u'last', email=u'*****@*****.**', address1=u'some', address2=u'where', postcode=u'98765', city=u'over', country=u'some country', locale=u'en', donation=u'3', shirt_size=u'3', comment=u'some comment.', ) DBSession.add(model1) DBSession.flush() with transaction.manager: model2 = Speedfundings( # german firstname=u'first', lastname=u'last', email=u'*****@*****.**', address1=u'some', address2=u'where', postcode=u'98765', city=u'over', country=u'some country', locale=u'de', donation=u'3', shirt_size=u'3', comment=u'some comment.', ) DBSession.add(model2) DBSession.flush()
def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite:///tests.db') from speedfunding.models import ( Base, Speedfundings, TheTotal, ) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: model = Speedfundings( firstname=u'first', lastname=u'last', email=u'*****@*****.**', address1=u'some', address2=u'where', postcode=u'98765', city=u'over', country=u'CC', locale=u'AT', donation=u'', shirt_size=u'', comment=u'some comment.', ) DBSession.add(model) # a total with transaction.manager: a_total = TheTotal( amount_actual=u'4200', amount_promised=u'5000', #time='2013-11-20', num_shirts=u'0' ) #try: DBSession.add(a_total) DBSession.flush()
def _populate_testDB(): """ set up a database to run tests against """ from speedfunding.models import ( Speedfundings, Group, C3sStaff, TheTotal ) # a speedfunding item with transaction.manager: speedfunding_item = Speedfundings( firstname=u"karl", lastname=u"ranseier", email=u"*****@*****.**", address1=u"lange straße 123", address2=u"hinterhof", city=u"hauptort", postcode="12345", country="CC", locale="DE", donation=u"4", shirt_size=u"2", comment=u"no comment ;-)" ) #try: DBSession.add(speedfunding_item) #DBSession.flush() #print("adding speedfunding_item") #except: # print("could not add speedfunding_item.") # pass # a total with transaction.manager: a_total = TheTotal( amount_actual=u'4200', amount_promised=u'5000', #time='2013-11-20', num_shirts='0' ) try: DBSession.add(a_total) DBSession.flush() print("adding a total") except: print("could not add the total.") # pass # a group for authorization with transaction.manager: accountants_group = Group(name=u"staff") try: DBSession.add(accountants_group) DBSession.flush() print("adding group staff") except: print("could not add group staff.") # pass # staff personnel with transaction.manager: staffer1 = C3sStaff( login=u"rut", password=u"berries", email=u"*****@*****.**", ) staffer1.groups = [accountants_group] try: DBSession.add(staffer1) print("adding staff rut") DBSession.flush() except: print("it borked! (rut)") # pass return DBSession
def tearDown(self): DBSession.remove() testing.tearDown()
def new_total(request): """ This view lets accountants set the amount collected to be displayed on the front page """ def sort_totals_reversed(objects): """ show the objects in reverse order (highest id first) """ return sorted(objects, key=lambda obj: obj.id, reverse=True) try: last_total = TheTotal.get_total() last_sum = last_total.amount_actual last_promised = last_total.amount_promised last_shirts = last_total.num_shirts except: last_sum = 0 last_promised = 0 last_shirts = 0 class NewTotal(colander.MappingSchema): """ colander schema for setting the collected sum """ amount_collected = colander.SchemaNode( colander.Int(), title=_( u"Sum collected (Euro, Integer) " "(This amount will be deducted from 70000 and " "the result be displayed on the landing page)"), validator=colander.Range(0, 200000), default=last_sum, oid="sum", ) amount_promised = colander.SchemaNode( colander.Int(), title=_(u"Sum promised (Euro, Integer)"), default=last_promised, oid="promised", ) num_shirts = colander.SchemaNode( colander.Int(), title=_(u"Number of shirts (Integer)"), default=last_shirts, oid="shirts", ) schema = NewTotal() form = deform.Form( schema, buttons=[ deform.Button('submit', _(u'Submit')), deform.Button('reset', _(u'Reset')) ], #use_ajax=True, #renderer=zpt_renderer ) # get and show the former totals _totals = TheTotal.get_listing( TheTotal.id.desc()) # if the form has been used and SUBMITTED, check contents if 'submit' in request.POST: print("the form was submitted") controls = request.POST.items() try: appstruct = form.validate(controls) print("the appstruct: %s" % appstruct) # time to save the data to the DB #{ # 'num_shirts': 123, # 'amount_collected': 12345, # 'amount_promised': 23456 #} _new_total = TheTotal( amount_actual=appstruct['amount_collected'], amount_promised=appstruct['amount_promised'], num_shirts=appstruct['num_shirts'], ) try: DBSession.add(_new_total) DBSession.flush() _totals = TheTotal.get_listing( TheTotal.id.desc()) except: print("could not write to DB. Error: ") except ValidationFailure, e: print(e) request.session.flash( _(u"Please note: There were errors, " "please check the form below."), 'message_above_form', allow_duplicate=False) return{'form': e.render(), 'totals': {}}
def tearDown(self): DBSession.remove() testing.tearDown() os.remove('tests.db')
def shirt_view(request): """ this view is the default view: the speedfunding form """ DEBUG = True if 'paypal' in request.params: #print("paypal option") request.session.pop_flash('message_above_form') return { 'form': '', # if paypal was chosen, don't show the form 'paypal': True} # but the paypal button (see templates/shirt.pt) else: paypal = False #print("DEBUG: paypal in shirt_view is %s" % paypal) if hasattr(request, '_REDIRECT_'): _query = request._REDIRECT_ request.response.set_cookie('_LOCALE_', _query) request._LOCALE_ = locale_name = _query return HTTPFound(location=request.route_url('speedfund'), headers=request.response.headers) else: locale_name = get_locale_name(request) # set default of Country select widget according to locale LOCALE_COUNTRY_MAPPING = { 'de': 'DE', #'da': 'DK', 'en': 'GB', #'es': 'ES', #'fr': 'FR', } country_default = LOCALE_COUNTRY_MAPPING.get(locale_name) if DEBUG: # pragma: no cover print("== locale is :" + str(locale_name)) print("== choosing :" + str(country_default)) # declare a form class TShirt(colander.MappingSchema): """ what size and fit for the shirt? """ shirt_option = colander.SchemaNode( colander.String(), title=_(u"I want to support the C3S by wearing my own t-shirt! " "Here's my choice:"), #widget=deform.widget.RadioChoiceWidget( # widget=deform.widget.SelectSliderWidget( widget=deform.widget.Select2Widget( values=( (u'S', _(u'S €35,00 EUR')), (u'M', _(u'M €35,00 EUR')), (u'L', _(u'L €35,00 EUR')), (u'XL', _(u'XL €35,00 EUR')), (u'XXL', _(u'XXL €35,00 EUR')), (u'S (Ladyfit)', _(u'S (Ladyfit) €35,00 EUR')), (u'M (Ladyfit)', _(u'M (Ladyfit) €35,00 EUR')), (u'L (Ladyfit)', _(u'L (Ladyfit) €35,00 EUR')), (u'XL (Ladyfit)', _(u'XL (Ladyfit) €35,00 EUR')), (u'XXL (Ladyfit)', _(u'XXL (Ladyfit) €35,00 EUR')), ) ) ) class PersonalData(colander.MappingSchema): """ people who want a shirt need to give us some address information """ firstname = colander.SchemaNode( colander.String(), widget=deform.widget.TextInputWidget( css_class='deformWidgetWithStyle'), title=_(u'First Name') ) lastname = colander.SchemaNode( colander.String(), title=_(u"Last Name") ) email = colander.SchemaNode( colander.String(), validator=colander.Email(), title=_(u"Email (just in case we need to check back with you)") ) address1 = colander.SchemaNode( colander.String(), title=_(u'Address Line 1') ) address2 = colander.SchemaNode( colander.String(), missing=unicode(''), title=_(u"Address Line 2") ) postcode = colander.SchemaNode( colander.String(), title=_(u'Zip Code'), oid="postcode" ) city = colander.SchemaNode( colander.String(), title=_(u'City'), oid="city", ) country = colander.SchemaNode( colander.String(), title=_(u'Country'), default=country_default, widget=deform.widget.SelectWidget( values=country_codes), oid="country", ) class TShirtForm(colander.Schema): """ the shirt form comprises shirt option and personal data """ shirt_data = TShirt( title=_(u'Choose a Shirt') ) personalData = PersonalData( title=_('Personal Data') ) schema = TShirtForm() form = deform.Form( schema, buttons=[ deform.Button('order_shirt', _(u'Yes, I want this T-Shirt!')), deform.Button('go_back', _(u'Go back, let me start over again.')), ], renderer=zpt_renderer) # if the form has been used and SUBMITTED, check contents submitted = (('order_shirt' in request.POST) or ('go_back' in request.POST)) if not submitted: request.session.pop_flash('message_above_form') if submitted: if ('go_back' in request.POST): return HTTPFound( location=request.route_url('speedfund'), ) controls = request.POST.items() try: appstruct = form.validate(controls) #print("the form validated!") # XXX TODO: persist _shirt = Speedfundings( firstname=appstruct['personalData']['firstname'], lastname=appstruct['personalData']['lastname'], email=appstruct['personalData']['email'], address1=appstruct['personalData']['address1'], address2=appstruct['personalData']['address2'], postcode=appstruct['personalData']['postcode'], city=appstruct['personalData']['city'], country=appstruct['personalData']['country'], locale=locale_name, donation='', shirt_size=appstruct['shirt_data']['shirt_option'], comment='', ) # persist try: DBSession.add(_shirt) DBSession.flush() except: print("failed to persist") # mail out message = Message( subject=_("[fund C3S!] thank you for choosing a shirt!"), sender="*****@*****.**", recipients=[_shirt.email], body=make_shirt_confirmation_emailbody(_shirt) ) mailer = get_mailer(request) mailer.send(message) print("message sent!") except deform.ValidationFailure, e: print(e) request.session.flash( _(u"Please note: There were errors, " "please check the form below."), 'message_above_form', allow_duplicate=False) # if there were errors, present the form with error messages #print("DEBUG: paypal in shirt_view is %s" % paypal) return{'form': e.render(), 'paypal': paypal} # if the form validated correctly, use the data given #print("the appstruct: %s" % appstruct) request.session.pop_flash() # delete old error messages return HTTPFound( location=request.route_url('success'), # XXX transport info there )
def donate_view(request): """ this view handles donations """ DEBUG = False if hasattr(request, '_REDIRECT_'): _query = request._REDIRECT_ request.response.set_cookie('_LOCALE_', _query) request._LOCALE_ = locale_name = _query return HTTPFound(location=request.route_url('speedfund'), headers=request.response.headers) else: locale_name = get_locale_name(request) #if hasattr(request, '_paypal'): # print("hasattr(request, '_paypal') !!!") #import pdb;pdb.set_trace() # set default of Country select widget according to locale LOCALE_COUNTRY_MAPPING = { 'de': 'DE', #'da': 'DK', 'en': 'GB', #'es': 'ES', #'fr': 'FR', } if 'paypal' in request.params: request.session.pop_flash('message_above_form') return { 'form': '', # if paypal was chosen, don't show the form 'paypal': True} # but the paypal button (see templates/donate.pt) else: paypal = False #print("DEBUG: paypal in donate_view is %s" % paypal) country_default = LOCALE_COUNTRY_MAPPING.get(locale_name) if DEBUG: # pragma: no cover print("== locale is :" + str(locale_name)) print("== choosing :" + str(country_default)) donation_amount_choice = ( ('10', u'5000,00 €'), ('9', u'2500,00 €'), ('8', u'1000,00 €'), ('7', u'500,00 €'), ('6', u'250,00 €'), ('5', u'100,00 €'), ('4', u'50,00 €'), ('3', u'25,00 €'), ('2', u'10,00 €'), ('1', u'5,00 €'), ) # declare a data set class DonationOption(colander.MappingSchema): """ a class for the donation choices in our speedfunding """ the_amount = colander.SchemaNode( colander.String(), title=_(u'I want to support C3S with my donation:'), default='1', # default: '1' ==> '5€' widget=deform.widget.SelectSliderWidget( #widget=deform.widget.SelectWidget( values=donation_amount_choice), oid="donation_choice", ) class PersonalData(colander.MappingSchema): email = colander.SchemaNode( colander.String(), title=_(u'Email'), validator=colander.Email(), oid="email", ) class DonationForm(colander.Schema): """ a donation """ donation = DonationOption( title=_('The Donation') ) #if True: personalData = PersonalData( title=_(u'Please tell us your email address in case we need ' 'to contact you:') ) # now construct the form schema from the parts above schema = DonationForm() form = deform.Form( schema, buttons=[ deform.Button('donate', _(u'Yes, I want to donate!')), deform.Button('go_back', _(u'Go back, let me start over again.')), ], renderer=zpt_renderer) # if the form has been used and SUBMITTED, check contents submitted = (('donate' in request.POST) or ('go_back' in request.POST)) if not submitted: request.session.pop_flash('message_above_form') if submitted: if ('go_back' in request.POST): return HTTPFound( location=request.route_url('speedfund'), ) controls = request.POST.items() try: appstruct = form.validate(controls) #print("the form did validate!") #print("the appstruct: %s" % appstruct) # if the form validated correctly, use the data given _donation = Speedfundings( firstname='', lastname='', email=appstruct['personalData']['email'], address1='', address2='', postcode='', city='', country='', locale=locale_name, donation=appstruct['donation']['the_amount'], shirt_size='', comment='', ) try: DBSession.add(_donation) DBSession.flush() #print("speedfunding entry was persisted.") except: print("failed to persist") #try: message = Message( subject=_("[fund C3S!] thanks for your donation!"), sender="*****@*****.**", recipients=[_donation.email, ], body=make_donation_confirmation_emailbody(_donation) ) mailer = get_mailer(request) mailer.send(message) #except: # print("failed to send the mail") except deform.ValidationFailure, e: print(e) request.session.flash( _(u"Please note: There were errors, " "please check the form below."), 'message_above_form', allow_duplicate=False) # if there were errors, present the form with error messages return{ 'form': e.render(), 'paypal': paypal, } #print("the appstruct: %s" % appstruct) request.session.pop_flash() # delete old error messages return HTTPFound( location=request.route_url('success'), )
def new_total(request): """ This view lets accountants set the amount collected to be displayed on the front page """ class NewTotal(colander.MappingSchema): """ colander schema for setting the collected sum """ amount_collected = colander.SchemaNode( colander.Int(), title=_(u"sum collected"), validator=colander.Range(0, 200000), oid="sum", ) amount_promised = colander.SchemaNode( colander.Int(), title=_(u"promised"), oid="promised", ) num_shirts = colander.SchemaNode( colander.Int(), title=_(u"shirts"), oid="shirts", ) schema = NewTotal() form = deform.Form( schema, buttons=[ deform.Button('submit', _(u'Submit')), deform.Button('reset', _(u'Reset')) ], #use_ajax=True, #renderer=zpt_renderer ) # get and show the former totals _totals = TheTotal.get_listing( TheTotal.id.asc()) # if the form has been used and SUBMITTED, check contents if 'submit' in request.POST: print("the form was submitted") controls = request.POST.items() try: appstruct = form.validate(controls) print("the appstruct: %s" % appstruct) # time to save the data to the DB #{ # 'num_shirts': 123, # 'amount_collected': 12345, # 'amount_promised': 23456 #} _new_total = TheTotal( amount_actual=appstruct['amount_collected'], amount_promised=appstruct['amount_promised'], num_shirts=appstruct['num_shirts'], ) try: DBSession.add(_new_total) DBSession.flush() except: print("could not write to DB. Error: ") except ValidationFailure, e: print(e) request.session.flash( _(u"Please note: There were errors, " "please check the form below."), 'message_above_form', allow_duplicate=False) return{'form': e.render(), 'totals': {}}