def _initTestingDB(): # from sqlalchemy import create_engine # from c3smembership.models import initialize_sql # session = initialize_sql(create_engine('sqlite:///memory')) # session = DBSession my_settings = { 'sqlalchemy.url': 'sqlite:///:memory:', } engine = engine_from_config(my_settings) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) member2 = C3sMember( # german firstname=u'AAASomeFirstnäme', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAR', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) DBSession.add(member1) DBSession.add(member2) return DBSession
def make_member_ready_for_approval(self): with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) member1.signature_received = True member1.payment_received = True DBSession.add(member1)
def setUp(self): self.config = testing.setUp() self.config.include('pyramid_mailer.testing') try: DBSession.close() DBSession.remove() # print("removed old DBSession ==============================") except: # print("no DBSession to remove =============================") pass # try: # os.remove('test_webtest_functional.db') # #print "deleted old test database" # except: # pass # #print "never mind" my_settings = { # 'sqlalchemy.url': 'sqlite:///test_webtest_functional.db', 'sqlalchemy.url': 'sqlite:///:memory:', 'available_languages': 'da de en es fr', 'c3smembership.mailaddr': '*****@*****.**', 'testing.mail_to_console': 'false' } engine = engine_from_config(my_settings) DBSession.configure(bind=engine) self.session = DBSession # () Base.metadata.create_all(engine) # dummy database entries for testing with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) DBSession.add(member1) DBSession.flush() from c3smembership import main app = main({}, **my_settings) app.registry.get_mailer = get_mailer from webtest import TestApp self.testapp = TestApp(app)
def __create_membership_applicant(): """ Create and return a membership applicant """ member = None with transaction.manager: member = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'Membership Applicant', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date(1970, 1, 1), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date(2015, 1, 1), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) return member
def make_member_with_shares(self): with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) shares1 = Shares( number=2, date_of_acquisition=date.today(), reference_code=u'ABCDEFGH', signature_received=True, signature_received_date=date(2014, 6, 7), payment_received=True, payment_received_date=date(2014, 6, 8), signature_confirmed=True, signature_confirmed_date=date(2014, 6, 8), payment_confirmed=True, payment_confirmed_date=date(2014, 6, 9), accountant_comment=u'no comment', ) member1.shares = [shares1] shares2 = Shares( number=23, date_of_acquisition=date.today(), reference_code=u'IJKLMNO', signature_received=True, signature_received_date=date(2014, 1, 7), payment_received=True, payment_received_date=date(2014, 1, 8), signature_confirmed=True, signature_confirmed_date=date(2014, 1, 8), payment_confirmed=True, payment_confirmed_date=date(2014, 1, 9), accountant_comment=u'not connected', ) DBSession.add(member1) DBSession.add(shares1) DBSession.add(shares2)
def setUp(self): """ set up everything for a test case """ self.config = testing.setUp() self.config.include('pyramid_mailer.testing') try: DBSession.close() DBSession.remove() # print("removing old DBSession ==============================") except: # print("no DBSession to remove ==============================") pass from sqlalchemy import create_engine # engine = create_engine('sqlite:///test_utils.db') engine = create_engine('sqlite:///:memory:') DBSession.configure(bind=engine) self.session = DBSession # () Base.metadata.create_all(engine) with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAR', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) DBSession.add(member1) DBSession.flush()
def setUp(self): self.__member = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', )
def setUp(self): my_settings = { 'sqlalchemy.url': 'sqlite:///:memory:', 'api_auth_token': u"SECRETAUTHTOKEN", } self.config = testing.setUp() app = main({}, **my_settings) # set up the database engine = engine_from_config(my_settings) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) # pylint: disable=no-member DBSession.add(member1) member1.email_invite_token_bcgv18 = u'MEMBERS_TOKEN' # pylint: disable=no-member DBSession.flush() self.testapp = TestApp(app)
def new_member(request): ''' let staff create a new member entry, when receiving input via dead wood ''' # XXX check if submitted, etc... class PersonalData(colander.MappingSchema): """ colander schema for membership application form """ firstname = colander.SchemaNode( colander.String(), title=u'Vorname (b. Körpersch.: Ansprechpartner)', oid="firstname", ) lastname = colander.SchemaNode( colander.String(), title=u'Nachname (b. Körpersch.: Name der Körperschaft)', oid="lastname", ) email = colander.SchemaNode( colander.String(), title=_(u'E-Mail'), validator=colander.Email(), oid="email", ) passwort = colander.SchemaNode( colander.String(), widget=deform.widget.HiddenWidget(), default='NoneSet', missing='NoneSetPurposefully' ) address1 = colander.SchemaNode( colander.String(), title='Adresse Zeile 1' ) address2 = colander.SchemaNode( colander.String(), missing=unicode(''), title='Adresse Zeile 2' ) postcode = colander.SchemaNode( colander.String(), title='Postleitzahl', oid="postcode" ) city = colander.SchemaNode( colander.String(), title='Ort', oid="city", ) country = colander.SchemaNode( colander.String(), title='Land', default=country_default, widget=deform.widget.SelectWidget( values=country_codes), oid="country", ) date_of_birth = colander.SchemaNode( colander.Date(), title='Geburtsdatum', # widget=deform.widget.DatePartsWidget( # inline=True), default=date(1970, 1, 1), oid="date_of_birth", ) locale = colander.SchemaNode( colander.String(), widget=deform.widget.HiddenWidget(), default='de', missing='de', ) class MembershipInfo(colander.Schema): yes_no = ((u'yes', _(u'Yes')), (u'no', _(u'No')), (u'dontknow', _(u'Unknown')),) entity_type = colander.SchemaNode( colander.String(), title=(u'Person oder Körperschaft?'), description=u'Bitte die Kategorie des Mitglied auswählen.', widget=deform.widget.RadioChoiceWidget( values=( (u'person', (u'Person')), (u'legalentity', u'Körperschaft'), ), ), missing=unicode(''), oid='entity_type', ) membership_type = colander.SchemaNode( colander.String(), title=(u'Art der Mitgliedschaft (lt. Satzung, §4)'), description=u'Bitte die Art der Mitgliedschaft auswählen.', widget=deform.widget.RadioChoiceWidget( values=( (u'normal', (u'Normales Mitglied')), (u'investing', u'Investierendes Mitglied'), (u'unknown', u'Unbekannt.'), ), ), missing=unicode(''), oid='membership_type', ) member_of_colsoc = colander.SchemaNode( colander.String(), title='Mitglied einer Verwertungsgesellschaft?', validator=colander.OneOf([x[0] for x in yes_no]), widget=deform.widget.RadioChoiceWidget(values=yes_no), missing=unicode(''), oid="other_colsoc", # validator=colsoc_validator ) name_of_colsoc = colander.SchemaNode( colander.String(), title=(u'Falls ja, welche? (Kommasepariert)'), missing=unicode(''), oid="colsoc_name", # validator=colander.All( # colsoc_validator, # ) ) class Shares(colander.Schema): """ the number of shares a member wants to hold """ num_shares = colander.SchemaNode( colander.Integer(), title='Anzahl Anteile (1-60)', default="1", validator=colander.Range( min=1, max=60, min_err=u'mindestens 1', max_err=u'höchstens 60', ), oid="num_shares") class MembershipForm(colander.Schema): """ The Form consists of - Personal Data - Membership Information - Shares """ person = PersonalData( title=_(u"Personal Data"), # description=_(u"this is a test"), # css_class="thisisjustatest" ) membership_info = MembershipInfo( title=_(u"Membership Requirements") ) shares = Shares( title=_(u"Shares") ) schema = MembershipForm() form = deform.Form( schema, buttons=[ deform.Button('submit', _(u'Submit')), deform.Button('reset', _(u'Reset')) ], use_ajax=True, # renderer=zpt_renderer ) # if the form has NOT been used and submitted, remove error messages if any if 'submit' not in request.POST: request.session.pop_flash() # print('ping!') # if the form has been used and SUBMITTED, check contents if 'submit' in request.POST: controls = request.POST.items() try: appstruct = form.validate(controls) # print("the appstruct from the form: %s \n") % appstruct # for thing in appstruct: # print("the thing: %s") % thing # print("type: %s") % type(thing) # data sanity: if not in collecting society, don't save # collsoc name even if it was supplied through form # if 'no' in appstruct['membership_info']['member_of_colsoc']: # appstruct['membership_info']['name_of_colsoc'] = '' # print appstruct['membership_info']['name_of_colsoc'] # print '-'*80 except ValidationFailure as e: # print("Validation Failure!") # print("the request.POST: %s \n" % request.POST) # for thing in request.POST: # print("the thing: %s") % thing # print("type: %s") % type(thing) # print(e.args) # print(e.error) # print(e.message) request.session.flash( _(u"Please note: There were errors, " "please check the form below."), 'message_above_form', allow_duplicate=False) return{'form': e.render()} def make_random_string(): """ used as email confirmation code """ import random import string return u''.join( random.choice( string.ascii_uppercase + string.digits ) for x in range(10)) # make confirmation code and randomstring = make_random_string() # check if confirmation code is already used while (C3sMember.check_for_existing_confirm_code(randomstring)): # create a new one, if the new one already exists in the database randomstring = make_random_string() # pragma: no cover # to store the data in the DB, an objet is created member = C3sMember( firstname=appstruct['person']['firstname'], lastname=appstruct['person']['lastname'], email=appstruct['person']['email'], password='******', address1=appstruct['person']['address1'], address2=appstruct['person']['address2'], postcode=appstruct['person']['postcode'], city=appstruct['person']['city'], country=appstruct['person']['country'], locale=appstruct['person']['locale'], date_of_birth=appstruct['person']['date_of_birth'], email_is_confirmed=False, email_confirm_code=randomstring, # is_composer=('composer' in appstruct['activity']), # is_lyricist=('lyricist' in appstruct['activity']), # is_producer=('music producer' in appstruct['activity']), # is_remixer=('remixer' in appstruct['activity']), # is_dj=('dj' in appstruct['activity']), date_of_submission=datetime.now(), # invest_member=( # appstruct['membership_info']['invest_member'] == u'yes'), membership_type=appstruct['membership_info']['membership_type'], member_of_colsoc=( appstruct['membership_info']['member_of_colsoc'] == u'yes'), name_of_colsoc=appstruct['membership_info']['name_of_colsoc'], # opt_band=appstruct['opt_band'], # opt_URL=appstruct['opt_URL'], num_shares=appstruct['shares']['num_shares'], ) if 'legalentity' in appstruct['membership_info']['entity_type']: # print "this is a legal entity" member.membership_type = u'investing' member.is_legalentity = True dbsession = DBSession() try: _temp = request.url.split('?')[1].split('=') if 'id' in _temp[0]: _id = _temp[1] # print("the id we want to recreate: %s" % _id) # add a member with a DB id that had seen its entry deleted before _mem = C3sMember.get_by_id(_id) # load from id if isinstance(_mem, NoneType): # check deletion status member.id = _id # set id as specified except: # print "no splitable url params found, creating new entry" pass # add member at next free DB id (default if member.id not set) try: dbsession.add(member) dbsession.flush() # print(member.id) the_new_id = member.id # appstruct['email_confirm_code'] = randomstring # ??? except InvalidRequestError, e: # pragma: no cover print("InvalidRequestError! %s") % e except IntegrityError, ie: # pragma: no cover print("IntegrityError! %s") % ie
def main(argv=sys.argv): """ initialize the database """ if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.create_all(engine) # add some content with transaction.manager: # a group for accountants/staff 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 with transaction.manager: # staff personnel 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 # one more staffer with transaction.manager: staffer2 = C3sStaff( login=u"reel", password=u"boo", email=u"*****@*****.**", ) staffer2.groups = [accountants_group] try: DBSession.add(staffer2) print("adding staff reel") DBSession.flush() except: print("it borked! (reel)") # pass # a member, actually a membership form submission with transaction.manager: member1 = C3sMember( firstname=u"Firstnäme", # includes umlaut lastname=u"Lastname", email=u"*****@*****.**", password=u"berries", address1=u"address one", address2=u"address two", postcode=u"12345 foo", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u"ABCDEFGHIJ", num_shares=u'10', date_of_submission=datetime.now(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", ) try: DBSession.add(member1) print("adding Firstnäme") except: pass # even more members import random import string print("about to add %s members..." % how_many) with transaction.manager: for i in range(how_many): # create 50 members with semi-random dates #print i member = C3sMember( firstname=u"Firstnäme%s" % i, # includes umlaut lastname=u"Lastname", email=u"*****@*****.**", password=u"berries", address1=u"address one", address2=u"address two", postcode=u"12345 foo", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(8)), num_shares=random.randint(1, 60), date_of_submission=datetime.now(), membership_type=random.choice((u'normal', u'investing')), member_of_colsoc=random.choice((True, False)), name_of_colsoc=u"GEMA", ) try: DBSession.add(member) except IntegrityError: print("exception!!!!!!!!!!!!!!!!!!!!1")
def init_testing_db(): """ Initializes the memory database with test samples. """ my_settings = { 'sqlalchemy.url': 'sqlite:///:memory:', } engine = engine_from_config(my_settings) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: # There is a side effect of test_initialization.py after which there are # still records in the database although it is setup from scratch. # Therefore, remove all members to have an empty table. members = C3sMember.get_all() for member in members: DBSession.delete(member) DBSession.flush() member1 = C3sMember( # german person firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFG1', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'23', ) member2 = C3sMember( # english person firstname=u'AAASomeFirstnäme', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFG2', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'23', ) member3 = C3sMember( # german legalentity firstname=u'Cooles PlattenLabel', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFG3', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'42', ) member4 = C3sMember( # english legalentity firstname=u'Incredible Records', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFG4', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'2', ) member1.membership_accepted = True DBSession.add(member1) member2.membership_accepted = True DBSession.add(member2) member3.membership_accepted = True DBSession.add(member3) member4.membership_accepted = True DBSession.add(member4) DBSession.flush() return DBSession
def setUp(self): my_settings = { 'sqlalchemy.url': 'sqlite:///:memory:', } engine = engine_from_config(my_settings) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=35, ) member2 = C3sMember( # german firstname=u'AAASomeFirstnäme', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAR', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=45, ) member3 = C3sMember( firstname=u'Not Approved', lastname=u'Member', email=u'*****@*****.**', address1=u'Some Street 123', address2=u'', postcode=u"12345", city=u"Some City", country=u"Some Country", locale=u"DE", date_of_birth=date(1980, 1, 2), email_is_confirmed=False, email_confirm_code=u'NOT_APPROVED_MEMBER', password=u'not_approved_member', date_of_submission=date(1970, 1, 1), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u'', num_shares=7, ) # pylint: disable=no-member DBSession.add(member1) # pylint: disable=no-member DBSession.add(member2) # pylint: disable=no-member DBSession.add(member3) member1.membership_number = u'member1' member1.membership_date = date(2013, 1, 1) member1.membership_accepted = True member2.membership_number = u'member2' member2.membership_date = date(2013, 1, 1) member2.membership_accepted = True member3.payment_received_date = date(2016, 10, 11) share = Shares() share.reference_code = u'share1' share.date_of_acquisition = date(2013, 1, 2) share.payment_received_date = date(2012, 11, 10) share.number = 12 member1.shares.append(share) share = Shares() share.reference_code = u'share2' share.date_of_acquisition = date(2014, 2, 3) share.payment_received_date = date(2012, 12, 31) share.number = 23 member1.shares.append(share) share = Shares() share.reference_code = u'share3' share.date_of_acquisition = date(2014, 3, 4) share.payment_received_date = date(2014, 3, 3) share.number = 34 member2.shares.append(share) share = Shares() share.reference_code = u'share4' share.date_of_acquisition = date(2015, 4, 5) share.payment_received_date = date(2014, 11, 15) share.number = 45 member2.shares.append(share)
def _insert_members(self): with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) member2 = C3sMember( # german firstname=u'AAASomeFirstnäme', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAR', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) member3 = C3sMember( # german firstname=u'BBBSomeFirstnäme', lastname=u'AAASomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"DE", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAZ', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) DBSession.add(member1) DBSession.add(member2) DBSession.add(member3) DBSession.flush()
def main(argv=sys.argv): """ initialize the database """ if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) Base.metadata.create_all(engine) # Setup alembic database migration information. # This creates the alembic_version table in the database # which is the basis for migrations and the "alembic current" # command. alembic_cfg = Config('alembic.ini') command.stamp(alembic_cfg, 'head') # add some content with transaction.manager: # a group for accountants/staff accountants_group = Group(name=u"staff") try: DBSession.add(accountants_group) DBSession.flush() except: # pragma: no cover print("could not add group staff.") # pass with transaction.manager: # staff personnel staffer1 = C3sStaff( login=u"rut", password=u"berries", email=u"*****@*****.**", ) staffer1.groups = [accountants_group] try: DBSession.add(staffer1) DBSession.flush() except: # pragma: no cover print("it borked! (rut)") # one more staffer with transaction.manager: staffer2 = C3sStaff( login=u"reel", password=u"boo", email=u"*****@*****.**", ) staffer2.groups = [accountants_group] try: DBSession.add(staffer2) DBSession.flush() except: # pragma: no cover print("it borked! (reel)") # a member, actually a membership form submission with transaction.manager: member1 = C3sMember( firstname=u"Firstnäme", # includes umlaut lastname=u"Lastname", email=u"*****@*****.**", password=u"berries", address1=u"address one", address2=u"address two", postcode=u"12345 foo", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date(1971, 02, 03), email_is_confirmed=False, email_confirm_code=u"ABCDEFGHIJ", num_shares=u'10', date_of_submission=datetime.now(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", ) try: DBSession.add(member1) except: # pragma: no cover pass with transaction.manager: normal_de = C3sMember( # german normal firstname=u'Ada Traumhaft', lastname=u'Musiziert', email=u'*****@*****.**', address1=u"Musikergasse 34", address2=u"Hinterhaus", postcode=u"12345", city=u"Foostadt Ada", country=u"Germany", locale=u"de", date_of_birth=date(1971, 3, 4), email_is_confirmed=False, email_confirm_code=u'NORMAL_DE1', password=u'adasrandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) normal_en = C3sMember( # english normal firstname=u'James', lastname=u'Musician', email=u'*****@*****.**', address1=u"james addr 1", address2=u"james appartment 2", postcode=u"12345", city=u"Jamestown", country=u"Jamescountry", locale=u"en", date_of_birth=date(1972, 4, 5), email_is_confirmed=False, email_confirm_code=u'NORMAL_DE', password=u'jamesrandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"", num_shares=u'2', ) investing_de = C3sMember( # german investing firstname=u'Herman', lastname=u'Investor', email=u'*****@*****.**', address1=u"c/o Mutti", address2=u"addr two4", postcode=u"12344", city=u"Footown M44", country=u"Austria", locale=u"de", date_of_birth=date(1974, 9, 8), email_is_confirmed=False, email_confirm_code=u'INVESTING_DE', password=u'arandompasswor4', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'6', ) investing_en = C3sMember( # english investing firstname=u'Britany', lastname=u'Investing', email=u'*****@*****.**', address1=u"aone5", address2=u"atwo5", postcode=u"12355", city=u"London", country=u"United Kingdom", locale=u"en", date_of_birth=date(1978, 4, 1), email_is_confirmed=False, email_confirm_code=u'INVESTING_EN', password=u'arandompasswor5', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=True, name_of_colsoc=u"", num_shares=u'60', ) legal_entity_de = C3sMember( # german investing legal entity firstname=u'Günther Vorstand', lastname=u'Deutscher Musikverlag', email=u'*****@*****.**', address1=u"Ährenweg 1", address2=u"", postcode=u"98765", city=u"Teststadt", country=u"Germany", locale=u"de", date_of_birth=date(1987, 3, 6), email_is_confirmed=False, email_confirm_code=u'VERLAG_DE', password=u'arandompasswor6', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'60', ) legal_entity_en = C3sMember( # english investing legal entity firstname=u'John BigBoss', lastname=u'Some Company', email=u'*****@*****.**', address1=u"foo boulevard", address2=u"123-345", postcode=u"98765", city=u"London", country=u"United Kingdom", locale=u"en", date_of_birth=date(1982, 4, 2), email_is_confirmed=False, email_confirm_code=u'COMPANY_EN', password=u'arandompasswor6', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'60', ) DBSession.add(normal_de) DBSession.add(normal_en) DBSession.add(investing_de) DBSession.add(investing_en) legal_entity_de.is_legalentity = True DBSession.add(legal_entity_de) legal_entity_en.is_legalentity = True DBSession.add(legal_entity_en)
def _initTestingDB(): """ Set up a database for these tests: have some test data. """ my_settings = { 'sqlalchemy.url': 'sqlite:///:memory:', } engine = engine_from_config(my_settings) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: normal_de = C3sMember( # german normal firstname=u'Ada Musiziert', lastname=u'Traumhaft ÄÖÜ', email=u'*****@*****.**', address1=u"ada addr one", address2=u"ada addr two", postcode=u"12345", city=u"Foostadt Ada", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'NORMAL_DE1', password=u'adasrandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) normal_en = C3sMember( # english normal firstname=u'James', lastname=u'Musician', email=u'*****@*****.**', address1=u"james addr 1", address2=u"james appartment 2", postcode=u"12345", city=u"Jamestown", country=u"Jamescountry", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'NORMAL_DE', password=u'jamesrandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"", num_shares=u'2', ) investing_de = C3sMember( # german investing firstname=u'Herman', lastname=u'Investorius', email=u'*****@*****.**', address1=u"addr one4", address2=u"addr two4", postcode=u"12344", city=u"Footown M44", country=u"Foocountr4", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'INVESTING_DE', password=u'arandompasswor4', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'60', ) investing_en = C3sMember( # english investing firstname=u'Britany', lastname=u'Investing', email=u'*****@*****.**', address1=u"aone5", address2=u"atwo5", postcode=u"12355", city=u"Footown M45", country=u"Foocountr5", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'INVESTING_EN', password=u'arandompasswor5', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'60', ) legal_entity_de = C3sMember( # german investing legal entity firstname=u'Deutscher', lastname=u'Musikverlag', email=u'*****@*****.**', address1=u"foo bulevard", address2=u"123-345", postcode=u"98765", city=u"Foo", country=u"Bar", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'VERLAG_DE', password=u'arandompasswor6', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'60', ) legal_entity_en = C3sMember( # english investing legal entity firstname=u'Francoise', lastname=u'Company', email=u'*****@*****.**', address1=u"foo bulevard", address2=u"123-345", postcode=u"98765", city=u"Foo", country=u"Bar", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'COMPANY_EN', password=u'arandompasswor6', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=False, name_of_colsoc=u"", num_shares=u'60', ) DBSession.add(normal_de) DBSession.add(normal_en) DBSession.add(investing_de) DBSession.add(investing_en) legal_entity_de.is_legalentity = True DBSession.add(legal_entity_en) legal_entity_en.is_legalentity = True DBSession.add(legal_entity_de) return DBSession
def setUp(self): """ Setup test cases """ self.config = testing.setUp() self.config.include('pyramid_mailer.testing') DBSession.close() DBSession.remove() my_settings = { 'sqlalchemy.url': 'sqlite:///:memory:', 'available_languages': 'da de en es fr', 'c3smembership.dashboard_number': '30' } engine = engine_from_config(my_settings) DBSession.configure(bind=engine) Base.metadata.create_all(engine) # self._insert_members() with transaction.manager: # a group for accountants/staff accountants_group = Group(name=u"staff") DBSession.add(accountants_group) DBSession.flush() # staff personnel staffer1 = C3sStaff( login=u"rut", password=u"berries", email=u"*****@*****.**", ) staffer1.groups = [accountants_group] DBSession.add(accountants_group) DBSession.add(staffer1) DBSession.flush() with transaction.manager: member1 = C3sMember( # german firstname=u'SomeFirstnäme', lastname=u'SomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) shares1_m1 = Shares( number=2, date_of_acquisition=date.today(), reference_code=u'ABCDEFGH', signature_received=True, signature_received_date=date(2014, 6, 7), payment_received=True, payment_received_date=date(2014, 6, 8), signature_confirmed=True, signature_confirmed_date=date(2014, 6, 8), payment_confirmed=True, payment_confirmed_date=date(2014, 6, 9), accountant_comment=u'no comment', ) member1.shares = [shares1_m1] shares2_m1 = Shares( number=23, date_of_acquisition=date.today(), reference_code=u'IJKLMNO', signature_received=True, signature_received_date=date(2014, 1, 7), payment_received=True, payment_received_date=date(2014, 1, 8), signature_confirmed=True, signature_confirmed_date=date(2014, 1, 8), payment_confirmed=True, payment_confirmed_date=date(2014, 1, 9), accountant_comment=u'not connected', ) member1.shares.append(shares2_m1) member1.membership_accepted = True member2 = C3sMember( # english firstname=u'AAASomeFirstnäme', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAR', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'2', ) founding_member3 = C3sMember( # english firstname=u'BBBSomeFirstnäme', lastname=u'YYYSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCBARdungHH_', password=u'anotherrandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'2', ) member4_lost = C3sMember( firstname=u'Resigned', lastname=u'Smith', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"en", date_of_birth=date(1980, 1, 2), email_is_confirmed=False, email_confirm_code=u'RESIGNEDSMITH', password=u'arandompassword', date_of_submission=date.today() - timedelta(days=370), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'2', ) DBSession.add(shares1_m1) DBSession.add(shares2_m1) DBSession.add(member1) DBSession.add(member2) DBSession.add(founding_member3) DBSession.add(member4_lost) app = main({}, **my_settings) self.testapp = TestApp(app)
def setUp(self): self.config = testing.setUp() self.config.include('pyramid_mailer.testing') engine = create_engine(u'sqlite://') from c3smembership.models import Base DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: member1 = C3sMember( firstname=u'firsie', lastname=u'lastie', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGFOO', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) member2 = C3sMember( # german firstname=u'AAASomeFirstnäme', lastname=u'XXXSomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAR', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'normal', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=u'23', ) member3 = C3sMember( # german firstname=u'BBBSomeFirstnäme', lastname=u'AAASomeLastnäme', email=u'*****@*****.**', address1=u"addr one", address2=u"addr two", postcode=u"12345", city=u"Footown Mäh", country=u"Foocountry", locale=u"de", date_of_birth=date.today(), email_is_confirmed=False, email_confirm_code=u'ABCDEFGBAZ', password=u'arandompassword', date_of_submission=date.today(), membership_type=u'investing', member_of_colsoc=True, name_of_colsoc=u"GEMA", num_shares=23, ) DBSession.add(member1) DBSession.add(member2) DBSession.add(member3) 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 staffer1 = C3sStaff( login=u"rut", password=u"berries", email=u"*****@*****.**", ) staffer1.groups = [accountants_group] try: DBSession.add(accountants_group) DBSession.add(staffer1) DBSession.flush() except: print("it borked! (rut)")
def join_c3s(request): """ This is the main membership application form view: Join C3S as member """ # if another language was chosen by clicking on a flag # the add_locale_to_cookie subscriber has planted an attr on the request if hasattr(request, '_REDIRECT_'): _query = request._REDIRECT_ # set language cookie # ToDo: the proper cookie name is _LOCALE_ (pyramid) request.response.set_cookie('locale', _query) request.locale = _query locale_name = _query return HTTPFound(location=request.route_url('join'), headers=request.response.headers) else: locale_name = get_locale_name(request) if DEBUG: print "-- locale_name: " + str(locale_name) # set default of Country select widget according to locale try: country_default = customization.locale_country_mapping.get(locale_name) except AttributeError: print(dir(customization)) country_default = 'GB' if DEBUG: print("== locale is :" + str(locale_name)) print("== choosing :" + str(country_default)) class PersonalData(colander.MappingSchema): """ colander schema for membership application form """ firstname = colander.SchemaNode( colander.String(), title=_(u"(Real) First Name"), oid="firstname", ) lastname = colander.SchemaNode( colander.String(), title=_(u"(Real) Last Name"), oid="lastname", ) email = colander.SchemaNode( colander.String(), title=_(u'Email Address'), validator=colander.Email(), oid="email", ) password = colander.SchemaNode( colander.String(), validator=colander.Length(min=5, max=100), widget=deform.widget.CheckedPasswordWidget(size=20), title=_(u'Password (to protect access to your data)'), description=_(u'We need a password to protect your data. After ' u'verifying your email you will have to enter it.'), oid='password', ) 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'Postal 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", ) date_of_birth = colander.SchemaNode( colander.Date(), title=_(u'Date of Birth'), # css_class="hasDatePicker", widget=deform.widget.DatePartsWidget(), default=date(2013, 1, 1), validator=Range( min=date(1913, 1, 1), # max 18th birthday, no minors through web formular max=date(date.today().year - 18, date.today().month, date.today().day), min_err=_(u'Sorry, we do not believe that you are that old'), max_err=_( u'Unfortunately, the membership application of an ' u'underaged person is currently not possible via our web ' u'form. Please send an email to [email protected].')), oid="date_of_birth", ) locale = colander.SchemaNode(colander.String(), widget=deform.widget.HiddenWidget(), default=locale_name) class MembershipInfo(colander.Schema): """ Basic member information. """ yes_no = ((u'yes', _(u'Yes')), (u'no', _(u'No'))) if len(customization.membership_types) > 1: membership_type = colander.SchemaNode( colander.String(), title=_( u'I want to become a ... ' u'(choose membership type, see C3S SCE statute sec. 4)'), description=_(u'choose the type of membership.'), widget=deform.widget.RadioChoiceWidget( values=((i['name'], i['description']) for i in customization.membership_types), ), oid='membership_type') if customization.enable_colsoc_association: member_of_colsoc = colander.SchemaNode( colander.String(), title=_(u'Currently, I am a member of (at least) one other ' u'collecting society.'), validator=colander.OneOf([x[0] for x in yes_no]), widget=deform.widget.RadioChoiceWidget(values=yes_no), oid="other_colsoc", # validator=colsoc_validator ) name_of_colsoc = colander.SchemaNode( colander.String(), title=_(u'If so, which one(s)? Please separate multiple ' u'collecting societies by comma.'), description=_( u'Please tell us which collecting societies ' u'you are a member of. ' u'If more than one, please separate them by comma.'), missing=unicode(''), oid="colsoc_name", ) class Fees(colander.Schema): member_type = colander.SchemaNode( colander.String(), title=_(u'Please tell us wether you\'re an individual, ' u'freelancer or company or want to support us ' u'generously as a sustaining member'), widget=deform.widget.RadioChoiceWidget( values=[(member_type, t_description) for fee, member_type, t_description in customization.membership_fees]), oid='member_type') # not validating here: depends on ^ # http://deformdemo.repoze.org/require_one_or_another/ member_custom_fee = colander.SchemaNode( colander.Decimal('1.00'), title=_(u'custom membership fee'), widget=deform.widget.MoneyInputWidget( symbol=customization.currency, showSymbol=True, defaultZero=True), description=_( u'Sustaining members: You can set your fees (minimum 100 €)'), oid='membership_custom_fee', default=customization.membership_fee_custom_min, validator=Range( min=customization.membership_fee_custom_min, max=None, min_err= _(u'please enter at least the minimum fee for sustaining members' ))) class Shares(colander.Schema): """ the number of shares a member wants to hold this involves a slider widget: added to deforms widgets. see README.Slider.rst """ num_shares = colander.SchemaNode( colander.Integer(), title=_(u"I want to buy the following number " u"of Shares (50 € each, up to 3000 €, see " u"C3S statute sec. 5)"), description=_( u'You can choose any amount of shares between 1 and 60.'), default="1", widget=TextInputSliderWidget(size=3, css_class='num_shares_input'), validator=colander.Range( min=1, max=60, min_err=_(u'You need at least one share of 50 €.'), max_err=_(u'You may choose 60 shares at most (3000 €).'), ), oid="num_shares") class TermsInfo(colander.Schema): """ some legal requirements """ def statute_validator(node, value): """ Validator for statute confirmation. """ if not value: # raise without additional error message as the description # already explains the necessity of the checkbox raise Invalid(node, u'') got_statute = colander.SchemaNode( colander.Bool(true_val=u'yes'), #title=(u''), title=_(u'I acknowledge that the statutes and membership dues ' u'regulations determine periodic contributions ' u'for full members.'), label=_( u'An electronic copy of the statute of the ' u'C3S SCE has been made available to me (see link below).'), description=_(u'You must confirm to have access to the statute.'), widget=deform.widget.CheckboxWidget(), validator=statute_validator, required=True, oid='got_statute', #label=_('Yes, really'), ) def dues_regulations_validator(node, value): """ Validator for dues regulations confirmation. """ if not value: # raise without additional error message as the description # already explains the necessity of the checkbox raise Invalid(node, u'') got_dues_regulations = colander.SchemaNode( colander.Bool(true_val=u'yes'), title=(u''), label=_( u'An electronic copy of the temporary membership dues ' u'regulations of the C3S SCE has been made available to me ' u'(see link below).'), description=_(u'You must confirm to have access to the temporary ' u'membership dues regulations.'), widget=deform.widget.CheckboxWidget(), validator=dues_regulations_validator, required=True, oid='got_dues_regulations', #label=_('Yes'), ) class MembershipForm(colander.Schema): """ The Form consists of - Personal Data - Membership Information - Shares """ person = PersonalData(title=_(u'Personal Data'), ) if len(customization.membership_types ) > 1 or customization.enable_colsoc_association: membership_info = MembershipInfo(title=_(u'Membership Data')) shares = Shares(title=_(u'Shares')) try: customization.membership_fees except NameError: pass else: fees = Fees(title=_(u'Membership Fees')) acknowledge_terms = TermsInfo(title=_(u'Acknowledgement')) schema = MembershipForm() form = deform.Form(schema, buttons=[ deform.Button('submit', _(u'Next')), deform.Button('reset', _(u'Reset')) ], use_ajax=True, renderer=ZPT_RENDERER) # if the form has NOT been used and submitted, remove error messages if any if 'submit' not in request.POST: request.session.pop_flash() # if the form has been used and SUBMITTED, check contents if 'submit' in request.POST: controls = request.POST.items() try: appstruct = form.validate(controls) # data sanity: if not in collecting society, don't save # collsoc name even if it was supplied through form if customization.membership_types and len( customization.membership_types ) > 1 and 'no' in appstruct['membership_info']['member_of_colsoc']: appstruct['membership_info']['name_of_colsoc'] = '' except ValidationFailure as validation_failure: request.session.flash(_(u'Please note: There were errors, ' u'please check the form below.'), 'message_above_form', allow_duplicate=False) # If the validation error was not caused by the password field, # manually set an error to the password field because the user # needs to re-enter it after a validation error. form = validation_failure.field if form['person']['password'].error is None: form['person']['password'].error = Invalid( None, _(u'Please re-enter your password.')) validation_failure = ValidationFailure(form, None, form.error) return {'form': validation_failure.render()} def make_random_string(): """ used as email confirmation code """ import random import string return u''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(10)) # make confirmation code and randomstring = make_random_string() # check if confirmation code is already used while C3sMember.check_for_existing_confirm_code(randomstring): # create a new one, if the new one already exists in the database randomstring = make_random_string() # pragma: no cover # to store the data in the DB, an objet is created coopMemberArgs = dict( firstname=appstruct['person']['firstname'], lastname=appstruct['person']['lastname'], email=appstruct['person']['email'], password=appstruct['person']['password'], address1=appstruct['person']['address1'], address2=appstruct['person']['address2'], postcode=appstruct['person']['postcode'], city=appstruct['person']['city'], country=appstruct['person']['country'], locale=appstruct['person']['locale'], date_of_birth=appstruct['person']['date_of_birth'], email_is_confirmed=False, email_confirm_code=randomstring, date_of_submission=datetime.now(), num_shares=appstruct['shares']['num_shares'], ) if customization.enable_colsoc_association: coopMemberArgs['member_of_colsoc'] = ( appstruct['membership_info']['member_of_colsoc'] == u'yes'), coopMemberArgs['name_of_colsoc'] = appstruct['membership_info'][ 'name_of_colsoc'] if customization.membership_types and len( customization.membership_types) > 1: coopMemberArgs['membership_type'] = appstruct['membership_info'][ 'membership_type'] member = C3sMember(**coopMemberArgs) dbsession = DBSession() try: dbsession.add(member) appstruct['email_confirm_code'] = randomstring if appstruct['fees']['member_type'] == 'sustaining': appstruct['fees']['fee'] = appstruct['fees'][ 'member_custom_fee'] else: appstruct['fees']['fee'] = [ v for v, t, d in customization.membership_fees if t == appstruct['fees']['member_type'] ][0] except InvalidRequestError as ire: # pragma: no cover print("InvalidRequestError! %s") % ire except IntegrityError as integrity_error: # pragma: no cover print("IntegrityError! %s") % integrity_error # redirect to success page, then return the PDF # first, store appstruct in session request.session['appstruct'] = appstruct request.session['appstruct']['locale'] = \ appstruct['person']['locale'] # empty the messages queue (as validation worked anyways) deleted_msg = request.session.pop_flash() del deleted_msg return HTTPFound( # redirect to success page location=request.route_url('success'), ) # if the form was submitted and gathered info shown on the success page, # BUT the user wants to correct their information: else: if 'edit' in request.POST: print(request.POST['edit']) # remove annoying message from other session deleted_msg = request.session.pop_flash() del deleted_msg if 'appstruct' in request.session: appstruct = request.session['appstruct'] # pre-fill the form with the values from last time form.set_appstruct(appstruct) html = form.render() return {'form': html}
def join_c3s(request): """ This is the main form view: Join C3S as member """ import datetime from colander import Range #LOGGING = True #if LOGGING: # pragma: no cover #import logging #log = logging.getLogger(__name__) #log.info("join...") # if another language was chosen by clicking on a flag # the add_locale_to_cookie subscriber has planted an attr on the request if hasattr(request, '_REDIRECT_'): #print("request._REDIRECT_: " + str(request._REDIRECT_)) _query = request._REDIRECT_ #print("_query: " + _query) # set language cookie request.response.set_cookie('_LOCALE_', _query) request._LOCALE_ = _query locale_name = _query #print("locale_name (from query_string): " + locale_name) #from pyramid.httpexceptions import HTTPFound #print("XXXXXXXXXXXXXXX ==> REDIRECTING ") return HTTPFound(location=request.route_url('join'), headers=request.response.headers) # # if another language was chosen, pick it # if request._REDIRECT_ is not '': # print("request.query_string: " + str(request.query_string)) # _query = request.query_string # print("_query: " + _query) # # set language cookie # request.response.set_cookie('_LOCALE_', _query) # request._LOCALE_ = _query # locale_name = _query # print("locale_name (from query_string): " + locale_name) # from pyramid.httpexceptions import HTTPFound # print("XXXXXXXXXXXXXXX ==> REDIRECTING ") # return HTTPFound(location=request.route_url('intent'), # headers=request.response.headers) else: #locale_name = request._LOCALE_ locale_name = get_locale_name(request) #print("locale_name (from request): " + locale_name) # check if user clicked on language symbol to have page translated # #print("request.query_string: " + str(request.query_string)) # if 'l' in request.query_string: # print("request.query_string: " + str(request.query_string)) # print("request.query_string[0]: " + str(request.query_string[0])) # from pyramid.httpexceptions import HTTPFound # if (request.query_string == '_LOCALE_=%s' % (locale_name)) or ( # request.query_string == 'l=%s' % (locale_name)): # # set language cookie # request.response.set_cookie('_LOCALE_', locale_name) # return HTTPFound(location=request.route_url('intent'), # headers=request.response.headers) if DEBUG: # pragma: no cover print "-- locale_name: " + str(locale_name) country_codes = [('AT', _(u'Austria')), ('BE', _(u'Belgium')), ('BG', _(u'Bulgaria')), ('CH', _(u'Switzerland')), ('CZ', _(u'Czech Republic')), ('DE', _(u'Germany')), ('DK', _(u'Denmark')), ('ES', _(u'Spain')), ('EE', _(u'Estonia')), ('FI', _(u'Finland')), ('FR', _(u'France')), ('GB', _(u'United Kingdom')), ('GR', _(u'Greece')), ('HU', _(u'Hungary')), ('HR', _(u'Croatia')), ('IL', _(u'Israel')), ('IE', _(u'Ireland')), ('IT', _(u'Italy')), ('LT', _(u'Lithuania')), ('LV', _(u'Latvia')), ('LU', _(u'Luxembourg')), ('MT', _(u'Malta')), ('NL', _(u'Netherlands')), ('PL', _(u'Poland')), ('PT', _(u'Portugal')), ('SK', _(u'Slovakia')), ('SI', _(u'Slovenia')), ('SE', _(u'Sweden')), ('XX', _(u'other'))] # 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)) class PersonalData(colander.MappingSchema): """ colander schema for membership application form """ firstname = colander.SchemaNode( colander.String(), title=_(u"(Real) First Name"), oid="firstname", ) lastname = colander.SchemaNode( colander.String(), title=_(u"(Real) Last Name"), oid="lastname", ) email = colander.SchemaNode( colander.String(), title=_(u'Email'), validator=colander.Email(), oid="email", ) password = colander.SchemaNode( colander.String(), validator=colander.Length(min=5, max=100), widget=deform.widget.PasswordWidget(size=20), title=_(u"Password"), description=_("We need a password to protect your data. After " "verifying your email you will have to enter it."), oid="password", ) 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'Post Code'), oid="postcode") city = colander.SchemaNode( colander.String(), title=_(u'City'), oid="city", ) # region = colander.SchemaNode( # colander.String(), # title=_(u'Federal State / Province / County'), # missing=unicode('')) country = colander.SchemaNode( colander.String(), title=_(u'Country'), default=country_default, widget=deform.widget.SelectWidget(values=country_codes), oid="country", ) # TODO: # Date of birth (dd/mm/yyyy) (three fields) # size doesn't have any effect?! date_of_birth = colander.SchemaNode( colander.Date(), title=_(u'Date of Birth'), #css_class="hasDatePicker", widget=deform.widget.DatePartsWidget(), default=datetime.date(2013, 1, 1), validator=Range( min=datetime.date(1913, 1, 1), max=datetime.date(2000, 1, 1), min_err=_(u'${val} is earlier than earliest date ${min}'), max_err=_(u'${val} is later than latest date ${max}')), oid="date_of_birth", ) # type_of_creator = (('composer', _(u'composer')), # ('lyricist', _(u'lyricist')), # ('music producer', _(u'music producer')), # ('remixer', _(u'remixer')), # ('dj', _(u'DJ'))) # activity = colander.SchemaNode( # deform.Set(allow_empty=True), # title=_( # u"I'm musically involved in creating at least three songs, " # "and I\'m considering to ask C3S to administer the rights " # " to some of my songs. I am active as a " # "(multiple selection possible)"), # widget=deform.widget.CheckboxChoiceWidget( # values=type_of_creator), # missing=unicode(''), # oid="activity",) _LOCALE_ = colander.SchemaNode(colander.String(), widget=deform.widget.HiddenWidget(), default=locale_name) class MembershipInfo(colander.Schema): yes_no = ((u'yes', _(u'Yes')), (u'no', _(u'No'))) # at_least_three_works = colander.SchemaNode( # colander.String(), # title=_(u'I have been the (co-)creator of at least three titles ' # 'in one of the functions mentioned under (1)'), # validator=colander.OneOf([x[0] for x in yes_no]), # widget=deform.widget.RadioChoiceWidget(values=yes_no)) ## TODO: inColSocName if member_of_colsoc = yes ## css/jquery: fixed; TODO: validator #def colsoc_validator(node, form): #log.info("validating...........................................") #print(value['member_of_colsoc']) #log.info(node.get('other_colsoc')) #log.info(node.get('other_colsoc-1')) #log.info(node.cstruct_children('other_colsoc')) #log.info(node.get_value('other_colsoc-1')) #log.info(dir(node)) #log.info(node['member_of_colsoc']) #import pdb; pdb.set_trace() #if value['member_of_colsoc'] #exc = colander.Invalid( # form, "if colsoc, give name!") #exc['name_of_colsoc'] = "if colsoc, give name!" #log.info("end----------------------------------------") #pass membership_type = colander.SchemaNode( colander.String(), title=_(u'I want to become a ... (choose membership type)'), description=_(u'choose the type of membership.'), widget=deform.widget.RadioChoiceWidget(values=( (u'normal', _(u'normal member. ' 'Normal members have to be natural persons ' 'who register at least three works with C3S ' 'they created themselves. This applies to composers, ' 'lyricists and remixers. They get a vote.')), (u'investing', _(u'investing member. ' 'Investing members can be natural persons or legal ' 'bodies that do not register works with C3S. ' 'They do not get a vote, but may counsel.'))), )) # member_is_artist = colander.SchemaNode( # colander.String(), # title=_( # u'I am at least one of: composer, lyricist, ' # 'remixer, arranger, producer, DJ (i.e. musician)'), # description=_( # u'You have to be a musician to become a regular member of C3S SCE.' # 'Or choose to become a supporting member.'), # validator=colander.OneOf([x[0] for x in yes_no]), # widget=deform.widget.RadioChoiceWidget( # values=(yes_no), # ), # ) member_of_colsoc = colander.SchemaNode( colander.String(), title= _(u'Currently, I am a member of (at least) one other collecting society.' ), validator=colander.OneOf([x[0] for x in yes_no]), widget=deform.widget.RadioChoiceWidget(values=yes_no), oid="other_colsoc", #validator=colsoc_validator ) name_of_colsoc = colander.SchemaNode( colander.String(), title=_(u'If so, which one(s)? (comma separated)'), description=_( u'Please tell us which collecting societies ' 'you are a member of. ' 'If more than one, please separate them by comma(s).'), missing=unicode(''), oid="colsoc_name", #validator=colander.All( # colsoc_validator, #) ) def statute_validator(node, value): if not value: raise Invalid( node, _(u'You must confirm to have access ' u'to the C3S SCE statute')) got_statute = colander.SchemaNode( #colander.String(), colander.Bool(true_val=u'yes'), title=_(u'I got to read an electronic copy of the ' u'C3S SCE statute'), description=_(u'You must confirm to have access to the statute.'), #widget=deform.widget.CheckboxChoiceWidget( # values=(('yes', _(u'Yes')),)), widget=deform.widget.CheckboxWidget(), #validator=colander.OneOf(['yes', ]), validator=statute_validator, required=True, label=_('Yes'), ) class Shares(colander.Schema): """ the number of shares a member wants to hold this involves a slider widget: added to deforms widgets. see README.slider.rst """ num_shares = colander.SchemaNode( colander.Integer(), title=_(u"I want to buy the following number " u"of Shares (50€ each, up to 3000€)"), description=_( u'You can choose any amount of shares between 1 and 60.'), default="1", widget=deform.widget.TextInputSliderWidget( size=3, css_class='num_shares_input'), validator=colander.Range( min=1, max=60, min_err=_(u"You need at least one share of 50 Euro."), max_err=_(u"You may choose 60 shares at most. (3000 Euro)"), ), oid="num_shares") class MembershipForm(colander.Schema): """ The Form consists of - Personal Data - Membership Information - Shares """ person = PersonalData( title=_(u"Personal Data"), #description=_(u"this is a test"), #css_class="thisisjustatest" ) membership_info = MembershipInfo(title=_(u"Membership Requirements")) shares = Shares(title=_(u"Shares")) schema = MembershipForm() form = deform.Form(schema, buttons=[ deform.Button('submit', _(u'Submit')), deform.Button('reset', _(u'Reset')) ], use_ajax=True, renderer=zpt_renderer) # if the form has NOT been used and submitted, remove error messages if any if not 'submit' in request.POST: request.session.pop_flash() # if the form has been used and SUBMITTED, check contents if 'submit' in request.POST: controls = request.POST.items() try: appstruct = form.validate(controls) #print("the appstruct from the form: %s \n") % appstruct #for thing in appstruct: # print("the thing: %s") % thing # print("type: %s") % type(thing) # data sanity: if not in collecting society, don't save # collsoc name even if it was supplied through form if 'no' in appstruct['membership_info']['member_of_colsoc']: appstruct['membership_info']['name_of_colsoc'] = '' print appstruct['membership_info']['name_of_colsoc'] #print '-'*80 except ValidationFailure, e: #print("the appstruct from the form: %s \n") % appstruct #for thing in appstruct: # print("the thing: %s") % thing # print("type: %s") % type(thing) print(e) #message.append( request.session.flash(_(u"Please note: There were errors, " "please check the form below."), 'message_above_form', allow_duplicate=False) return {'form': e.render()} def make_random_string(): """ used as email confirmation code """ import random import string return ''.join( random.choice(string.ascii_uppercase + string.digits) for x in range(10)) # make confirmation code and randomstring = make_random_string() # check if confirmation code is already used while (C3sMember.check_for_existing_confirm_code(randomstring)): # create a new one, if the new one already exists in the database randomstring = make_random_string() # pragma: no cover from datetime import datetime from sqlalchemy.exc import (InvalidRequestError, IntegrityError) # to store the data in the DB, an objet is created member = C3sMember( firstname=appstruct['person']['firstname'], lastname=appstruct['person']['lastname'], email=appstruct['person']['email'], password=appstruct['person']['password'], address1=appstruct['person']['address1'], address2=appstruct['person']['address2'], postcode=appstruct['person']['postcode'], city=appstruct['person']['city'], country=appstruct['person']['country'], locale=appstruct['person']['_LOCALE_'], date_of_birth=appstruct['person']['date_of_birth'], email_is_confirmed=False, email_confirm_code=randomstring, #is_composer=('composer' in appstruct['activity']), #is_lyricist=('lyricist' in appstruct['activity']), #is_producer=('music producer' in appstruct['activity']), #is_remixer=('remixer' in appstruct['activity']), #is_dj=('dj' in appstruct['activity']), date_of_submission=datetime.now(), #invest_member=( # appstruct['membership_info']['invest_member'] == u'yes'), membership_type=appstruct['membership_info']['membership_type'], member_of_colsoc=( appstruct['membership_info']['member_of_colsoc'] == u'yes'), name_of_colsoc=appstruct['membership_info']['name_of_colsoc'], #opt_band=appstruct['opt_band'], #opt_URL=appstruct['opt_URL'], num_shares=appstruct['shares']['num_shares'], ) dbsession = DBSession() try: dbsession.add(member) appstruct['email_confirm_code'] = randomstring except InvalidRequestError, e: # pragma: no cover print("InvalidRequestError! %s") % e