def seed_user(email, role, dept): ''' Creates a new user in the database. ''' from purchasing.users.models import User, Department seed_email = email if email else app.config.get('SEED_EMAIL') user_exists = User.query.filter(User.email == seed_email).first() department = Department.query.filter( Department.name == db.func.lower(dept) ).first() if user_exists: print 'User {email} already exists'.format(email=seed_email) else: try: new_user = User.create( email=seed_email, created_at=datetime.datetime.utcnow(), role_id=role, department=department if department else None ) db.session.add(new_user) db.session.commit() print 'User {email} successfully created!'.format(email=seed_email) except Exception, e: print 'Something went wrong: {exception}'.format(exception=e.message)
def parse_contact(contact_email, department): # get our department contact, build it if we don't have it yet contact = User.query.filter(User.email == contact_email).first() if contact is None: contact = User.create( email=contact_email, role=Role.query.filter(Role.name == 'staff').first(), department=department ) return contact.id
def auth(): '''Endpoint from AJAX request for authentication from persona ''' data = urllib.urlencode({ 'assertion': request.form.get('assertion'), 'audience': current_app.config.get('BROWSERID_URL') }) req = urllib2.Request('https://verifier.login.persona.org/verify', data) response = json.loads(urllib2.urlopen(req).read()) if response.get('status') != 'okay': current_app.logger.debug( 'REJECTEDUSER: User login rejected from persona. Messages: {}'. format(response)) abort(403) next_url = request.args.get('next', None) email = response.get('email') user = User.query.filter(User.email == email).first() domain = email.split('@')[1] if len(email.split('@')) > 1 else None if user: login_user(user) flash('Logged in successfully!', 'alert-success') current_app.logger.debug( 'LOGIN: User {} logged in successfully'.format(user.email)) return next_url if next_url else '/' elif AcceptedEmailDomains.valid_domain(domain): user = User.create( email=email, role=Role.query.filter(Role.name == 'staff').first(), department=Department.query.filter( Department.name == 'New User').first()) login_user(user) current_app.logger.debug( 'NEWUSER: New User {} successfully created'.format(user.email)) return '/users/profile' else: current_app.logger.debug( 'NOTINDB: User {} not in DB -- aborting!'.format(email)) abort(403)
def build_subscribers(self): """Build a list of subscribers and others to populate contacts in conductor """ department_users, county_purchasers, eorc = User.get_subscriber_groups(self.department_id) if self.parent is None: followers = [] else: followers = [i for i in self.parent.followers if i not in department_users] subscribers = { "Department Users": department_users, "Followers": followers, "County Purchasers": [i for i in county_purchasers if i not in department_users], "EORC": eorc, } return subscribers, sum([len(i) for i in subscribers.values()])
def auth(): ''' Endpoint from AJAX request for authentication from persona ''' data = urllib.urlencode({ 'assertion': request.form.get('assertion'), 'audience': current_app.config.get('BROWSERID_URL') }) req = urllib2.Request('https://verifier.login.persona.org/verify', data) response = json.loads(urllib2.urlopen(req).read()) if response.get('status') != 'okay': current_app.logger.debug('REJECTEDUSER: User login rejected from persona. Messages: {}'.format(response)) abort(403) next_url = request.args.get('next', None) email = response.get('email') user = User.query.filter(User.email == email).first() domain = email.split('@')[1] if len(email.split('@')) > 1 else None if user: login_user(user) flash('Logged in successfully!', 'alert-success') current_app.logger.debug('LOGIN: User {} logged in successfully'.format(user.email)) return next_url if next_url else '/' elif AcceptedEmailDomains.valid_domain(domain): user = User.create( email=email, role=Role.query.filter(Role.name == 'staff').first(), department=Department.query.filter(Department.name == 'New User').first() ) login_user(user) current_app.logger.debug('NEWUSER: New User {} successfully created'.format(user.email)) return '/users/profile' else: current_app.logger.debug('NOTINDB: User {} not in DB -- aborting!'.format(email)) abort(403)
def parse_contact(contact_email, department): '''Finds or creates a :py:class:`purchasing.users.models.User` as the contact :param contact_email: The email address of the user. If the user cannot be found in the database, the domain of their email must match the configured ``CITY_DOMAIN`` :param department: The :py:class:`purchasing.users.models.Department` of the user :return: The ID of the new/existing contact ''' # get our department contact, build it if we don't have it yet contact = User.query.filter(User.email == contact_email).first() if contact is None: contact = User.create( email=contact_email, role=Role.query.filter(Role.name == 'staff').first(), department=department ) return contact.id
def parse_contact(contact_email, department): '''Finds or creates a user as the contact Arguments: contact_email: The email address of the :py:class:`~purchasing.users.models.User`. If the user cannot be found in the database, the domain of their email must match the configured ``CITY_DOMAIN`` department: The :py:class:`~purchasing.users.models.Department` of the user Returns: The ID of the new/existing contact ''' # get our department contact, build it if we don't have it yet contact = User.query.filter(User.email == contact_email).first() if contact is None: contact = User.create( email=contact_email, roles=[Role.query.filter(Role.name == 'staff').first()], department=department) return contact.id
def build_subscribers(self): '''Build a list of subscribers and others to populate contacts in conductor ''' department_users, county_purchasers, eorc = User.get_subscriber_groups( self.department_id) if self.parent is None: followers = [] else: followers = [ i for i in self.parent.followers if i not in department_users ] subscribers = { 'Department Users': department_users, 'Followers': followers, 'County Purchasers': [i for i in county_purchasers if i not in department_users], 'EORC': eorc } return subscribers, sum([len(i) for i in subscribers.values()])
def load_user(userid): return User.get_by_id(int(userid))
def create_a_user(email='*****@*****.**', department='Other', role=None): return User(email=email, first_name='foo', last_name='foo', department=department, role_id=role)