def add_members_to_db(fin=members_json): from PyQt4 import QtCore from models import Member from assets.validators import valid_email, valid_phone with open(fin, 'r') as members_file: data = members_file.read() data = json.loads(data) sheets = data.keys() for s in sheets: print s for member in data[s]: if len(member) != 5: print 'invalid member' return mob = member[0] mob = mob if valid_phone(mob) else None email = member[1] email = email if valid_email(email) else None grade = member[2] name = member[3] num = member[4] if name and num.isdigit(): m = Member.add(name=name, mob=mob, email=email, note=grade) if type(m) in [str, QtCore.QString]: print m, name, num
def add(self, name, email=None, mob=None, note=None): if not valid_name(name): return translate('dbModels', 'invalid name') if email and not valid_email(email): return translate('dbModels', 'invalid email') if email and self.by_email(email): return translate('dbModels', "email exists") if mob and not valid_phone(mob): return translate('dbModels', 'invalid mob no.') if mob and self.by_mob(mob): return translate('dbModels', "mob no. exists") self = self(name=name, email=email, mob=mob, note=note) return self.put()
def add(self, name, email=None, mob=None): if not valid_name(name): return 'invalid name' if email and not valid_email(email): return 'invalid email' if email and self.by_email(email): return "email exists" if mob and not valid_phone(mob): return 'invalid mob no.' if mob and self.by_mob(mob): return "mob no. exists" self = self(name=name, email=email, mob=mob) return self.put()
def update(self, name=None, email=None, mob=None, fine=None): if name and not valid_name(name): return 'invalid name' if email and self.by_email(email): return "email exists" if email and not valid_email(email): return 'invalid email' if mob and self.by_mob(mob): return "mob exists" if mob and not valid_phone(mob): return 'invalid mob no.' edits = {} edits['name'] = name if name else self.name edits['email'] = email if email else self.email edits['mob'] = mob if mob else self.mob edits['fine'] = fine if fine else self.fine with self.session() as session: entity = session.query(type(self)).filter(type(self).id == self.id) entity.update(edits) session.commit() session.expunge_all() return self
def update(self, name=None, email=None, mob=None, fine=None): if name and not valid_name(name): return 'invalid name' if email and self.by_email(email): return "email exists" if email and not valid_email(email): return 'invalid email' if mob and self.by_mob(mob): return "mob exists" if mob and not valid_phone(mob): return 'invalid mob no.' edits = {} edits['name'] = name if name else self.name edits['email'] = email if email else self.email edits['mob'] = mob if mob else self.mob edits['fine'] = fine if fine else self.fine with self.session() as session: entity = session.query(type(self)).filter(type(self).id==self.id) entity.update(edits) session.commit() session.expunge_all() return self