def test_get_by_id(self): """Get user by ID.""" user = User('foo', '*****@*****.**') user.save() retrieved = User.get_by_id(user.id) assert retrieved == user
def test_check_password(self): """Check password.""" user = User.create(username='******', email='*****@*****.**', password='******') assert user.check_password('foobarbaz123') is True assert user.check_password('barfoobaz') is False
def update_build(): id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] # TODO: Check if build exists build = Build.get_by_id(id) original_value = None if field == 'notes': original_value = build.notes build.update(notes=field_value) elif field == 'owner': if build.owner: original_value = build.owner.get_name() owner = User.get_by_id(field_value) build.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'purchase_order': original_value = build.purchase_order build.update(purchase_order=field_value) elif field == 'vendor': if build.vendor: original_value = build.vendor.name vendor = Company.get_by_id(field_value) build.update(vendor=vendor) field_value = vendor.name if vendor else None build.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def update_anomaly(): id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] original_value = None anomaly = Anomaly.get_by_id(id) if field == 'name': original_value = anomaly.name anomaly.update(name=field_value) elif field == 'summary': original_value = anomaly.summary anomaly.update(summary=field_value) elif field == 'analysis': original_value = anomaly.analysis anomaly.update(analysis=field_value) elif field == 'software_version': original_value = anomaly.software_version anomaly.update(software_version=field_value) elif field == 'criticality': if anomaly.criticality: original_value = anomaly.criticality.name criticality = Criticality.get_by_id(field_value) anomaly.update(criticality=criticality) field_value = criticality.name if criticality else None elif field == 'corrective_action': original_value = anomaly.corrective_action anomaly.update(corrective_action=field_value) elif field == 'project': if anomaly.project: original_value = anomaly.project.name project = Project.get_by_id(field_value) anomaly.update(project=project) field_value = project.name if project else None elif field == 'owner': if anomaly.owner: original_value = anomaly.owner.get_name() if anomaly.owner.padawan: for approver in anomaly.approvers: if approver.approver == anomaly.owner.supervisor and approver.capacity == 'Supervisor': anomaly.approvers.remove(approver) approver.delete() owner = User.get_by_id(field_value) if owner.padawan: approver = Approver.create(approver_id=owner.supervisor_id, capacity='Supervisor') anomaly.approvers.append(approver) anomaly.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value anomaly.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=anomaly) anomaly.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def save_user(username, first_name, last_name, email, roles): # First check if user is already in DB, and create if not, and update if is user = User.get_by_username(username) if not user: # Create new user if not in database user = User.create(username=username, first_name=first_name, last_name=last_name, email=email, roles=roles) elif user.first_name != first_name or user.last_name != last_name or user.email != email or user.roles != roles: # Update user if details have changed. Username should never change user.first_name = first_name user.last_name = last_name user.email = email user.roles = roles user = User.update(user) return user
def update_product(): id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] # TODO: Check if product exists product = Product.get_by_id(id) original_value = None if field == 'hardware_type': if product.hardware_type: original_value = product.hardware_type.name hardware_type = HardwareType.get_by_id(field_value) product.update(hardware_type=hardware_type) field_value = hardware_type.name if hardware_type else None elif field == 'measured_mass': original_value = product.measured_mass product.update(measured_mass=float(field_value)) elif field == 'owner': if product.owner: original_value = product.owner.get_name() if product.owner.padawan: for approver in product.approvers: if approver.approver == product.owner.supervisor and approver.capacity == 'Supervisor': product.approvers.remove(approver) approver.delete() owner = User.get_by_id(field_value) if owner.padawan: approver = Approver.create(approver_id=owner.supervisor_id, capacity='Supervisor') product.approvers.append(approver) product.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'project': if product.project: original_value = product.project.name project = Project.get_by_id(field_value) product.update(project=project) field_value = project.name if project else None elif field == 'notes': original_value = product.notes product.update(notes=field_value) elif field == 'summary': original_value = product.summary product.update(summary=field_value) elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value product.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=product) product.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def update_task(): task_id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] task = Task.get_by_id(task_id) original_value = None if field == 'title': original_value = task.title task.update(title=field_value) elif field == 'summary': original_value = task.summary task.update(summary=field_value) elif field == 'state': original_value = task.state task.update(state=field_value) elif field == 'urgency': original_value = task.urgency task.update(urgency=field_value) field = 'criticality' # To match what is visually displayed elif field == 'assigned_to': if task.assigned_to: original_value = task.assigned_to.get_name() assigned_to = User.get_by_id(field_value) task.update(assigned_to=assigned_to) send_email(subject='Task Assigned: {0}'.format(task.title), recipients=[task.assigned_to.email], text_body=render_template('mail/new_task.txt', task=task), html_body=render_template('mail/new_task.html', task=task)) field_value = assigned_to.get_name() if assigned_to else None elif field == 'need_date': try: original_value = task.need_date.date() # Need to append UTC hours due to moment.js and timezones task.update(need_date=parse(field_value).date() + relativedelta(hours=+datetime.utcnow().hour)) except ValueError: return "Incorrect date format: " + field_value, 500, { 'ContentType': 'application/json' } elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value task.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=task) task.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def authenticate(self, username, password): # TODO: Check if app is able to reach LDAP server # TODO: Check if user is active in AD # Try to authenticate user via AD result = ldap_manager.authenticate(username, password) if result.status == AuthenticationResponseStatus.success: # Grab relevant roles for PID (users, superusers, admin) roles = [ 'employees' ] # Add employees for staging server purposes. TODO: Find a better way to manage for group in result.user_groups: if group['name'].startswith('plaid-'): roles.append(group['name']) # Check if user is part of plaid-users (required to access app) if current_app.config['PLAID_USERS_GROUP'] not in roles: flash( 'You are not part of PLAID users group, contact Sean or Jarle if you should have access', 'warning') return None user = ldap_manager._save_user(result.user_id, result.user_info['givenName'], result.user_info['sn'], result.user_info['mail'], ', '.join(roles)) return user else: # Query local SQLite DB in DEV if current_app.config['ENV'] is 'dev': user = User.get_by_username(username) if user is None: flash( 'Could not authenticate with AD or find a local user', 'warning') return None flash('Logged in as local user', 'info') return user else: flash( 'Could not authenticate your username ({0}) with AD, did you enter correct password?' .format(self.username.data), 'warning') return None return None
def update_specification(): id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] specification = Specification.get_by_id(id) original_value = None if field == 'name': original_value = specification.name specification.update(name=field_value) elif field == 'scope': original_value = specification.scope specification.update(scope=field_value) elif field == 'summary': original_value = specification.summary specification.update(summary=field_value) elif field == 'owner': if specification.owner: original_value = specification.owner.get_name() if specification.owner.padawan: for approver in specification.approvers: if approver.approver == specification.owner.supervisor and approver.capacity == 'Supervisor': specification.approvers.remove(approver) approver.delete() owner = User.get_by_id(field_value) if owner.padawan: approver = Approver.create(approver_id=owner.supervisor_id, capacity='Supervisor') specification.approvers.append(approver) specification.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value specification.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=specification) specification.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def get_tasks_for_user(username, task_type='assigned'): # TODO: Change this away from this kind of URL, but task table is tricky right now user = None if username == 'efab': settings = Settings.get_settings() user = settings.efab_user elif username == 'mfab': settings = Settings.get_settings() user = settings.mfab_user elif username == 'plaid_admin': settings = Settings.get_settings() user = settings.plaid_admin else: user = User.get_by_username(username) tasks = Task.find_all_tasks_for_user(user, task_type) task_columns = Task.__table__.columns._data.keys() results = [] for task in tasks: task_dict = {} for column in task_columns: if column not in ['assigned_to_id', 'requested_by_id']: task_dict[column] = getattr(task, column) task_dict['assigned_to'] = { 'id': task.assigned_to.id, 'get_name': task.assigned_to.get_name(), 'username': task.assigned_to.username } task_dict['requested_by'] = { 'id': task.requested_by.id, 'get_name': task.requested_by.get_name(), 'username': task.requested_by.username } results.append(task_dict) return jsonify({ 'success': True, 'data': results }), 200, { 'ContentType': 'application/json' }
def update_vendor_part(): # TODO: Check that field should actually be allowed to change. As in, don't change design number for instance id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] original_value = None # TODO: Check if design exists vendor_part = VendorPart.get_by_id(id) if field == 'name': original_value = vendor_part.name vendor_part.update(name=field_value) elif field == 'summary': original_value = vendor_part.summary vendor_part.update(summary=field_value) elif field == 'project': if vendor_part.project: original_value = vendor_part.project.name project = Project.get_by_id(field_value) vendor_part.update(project=project) field_value = project.name if project else None elif field == 'owner': if vendor_part.owner: original_value = vendor_part.owner.get_name() if vendor_part.owner.padawan: for approver in vendor_part.approvers: if approver.approver == vendor_part.owner.supervisor and approver.capacity == 'Supervisor': vendor_part.approvers.remove(approver) approver.delete() owner = User.get_by_id(field_value) if owner.padawan: approver = Approver.create(approver_id=owner.supervisor_id, capacity='Supervisor') vendor_part.approvers.append(approver) vendor_part.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'vendor': if vendor_part.vendor: original_value = vendor_part.vendor.name vendor = Company.get_by_id(field_value) vendor_part.update(vendor=vendor) field_value = vendor.name if vendor else None elif field == 'notes': original_value = vendor_part.notes vendor_part.update(notes=field_value) elif field == 'material': original_material = None original_material_spec = None if vendor_part.material: original_material = vendor_part.material.name if vendor_part.material_specification: original_material_spec = vendor_part.material_specification.name material = Material.get_by_id(field_value) vendor_part.update( material=material, material_specification=None ) # To ensure we don't have a mat_spec linked with old material material_name = None material_specifications = None if material: material_name = material.name material_specifications = material.specifications vendor_part.add_change_log_entry(action='Edit', field='Material', original_value=original_material, new_value=material_name) if original_material_spec: vendor_part.add_change_log_entry( action='Edit', field='Material Specification', original_value=original_material_spec) variables = { 'vendor_part': vendor_part, 'material_specifications': material_specifications } return render_template( 'vendorpart/ajax_select_material_specification.html', **variables) elif field == 'material_specification': if vendor_part.material_specification: original_value = vendor_part.material_specification.name material_specification = MaterialSpecification.get_by_id(field_value) vendor_part.update(material_specification=material_specification) field_value = material_specification.name if material_specification else None elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value vendor_part.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=vendor_part) elif field == 'current_best_estimate': original_value = vendor_part.current_best_estimate original_predicted_best_estimate = vendor_part.predicted_best_estimate current_best_estimate = float(field_value) predicted_best_estimate = current_best_estimate * ( 1 + (vendor_part.uncertainty / 100)) # PBE = CBE * (1+%Unc) vendor_part.update(current_best_estimate=current_best_estimate, predicted_best_estimate=predicted_best_estimate) vendor_part.update_parents_mass( ) # Updates parts where this is a component vendor_part.add_change_log_entry(action='Edit', field='Current Best Estimate', original_value=original_value, new_value=current_best_estimate) vendor_part.add_change_log_entry( action='Edit', field='Predicted Best Estimate', original_value=original_predicted_best_estimate, new_value=predicted_best_estimate) return render_template('vendorpart/mass_fields.html', vendor_part=vendor_part) elif field == 'uncertainty': original_value = vendor_part.uncertainty original_predicted_best_estimate = vendor_part.predicted_best_estimate uncertainty = float(field_value) predicted_best_estimate = vendor_part.current_best_estimate * ( 1 + (uncertainty / 100)) # PBE = CBE * (1+%Unc) vendor_part.update(uncertainty=uncertainty, predicted_best_estimate=predicted_best_estimate) vendor_part.update_parents_mass( ) # Updates parts where this is a component vendor_part.add_change_log_entry(action='Edit', field='Uncertainty', original_value=original_value, new_value=uncertainty) vendor_part.add_change_log_entry( action='Edit', field='Predicted Best Estimate', original_value=original_predicted_best_estimate, new_value=predicted_best_estimate) return render_template('vendorpart/mass_fields.html', vendor_part=vendor_part) vendor_part.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def test_password_is_nullable(self): """Test null password.""" user = User(username='******', email='*****@*****.**') user.save() assert user.password is None
def test_created_at_defaults_to_datetime(self): """Test creation date.""" user = User(username='******', email='*****@*****.**') user.save() assert bool(user.created_at) assert isinstance(user.created_at, dt.datetime)
def update_vendor_product(): id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] # TODO: Check if product exists vendor_product = VendorProduct.get_by_id(id) original_value = None jsonData = {} if field == 'hardware_type': if vendor_product.hardware_type: original_value = vendor_product.hardware_type.name hardware_type = HardwareType.get_by_id(field_value) vendor_product.update(hardware_type=hardware_type) field_value = hardware_type.name if hardware_type else None elif field == 'measured_mass': original_value = vendor_product.measured_mass vendor_product.update(measured_mass=float(field_value)) vendor_product.add_change_log_entry(action='Edit', field='Measured Mass', original_value=original_value, new_value=field_value) variables = {'vendor_product': vendor_product} return render_template('vendorproduct/mass_field.html', **variables) elif field == 'owner': if vendor_product.owner: original_value = vendor_product.owner.get_name() if vendor_product.owner.padawan: for approver in vendor_product.approvers: if approver.approver == vendor_product.owner.supervisor and approver.capacity == 'Supervisor': vendor_product.approvers.remove(approver) approver.delete() owner = User.get_by_id(field_value) if owner.padawan: approver = Approver.create(approver_id=owner.supervisor_id, capacity='Supervisor') vendor_product.approvers.append(approver) vendor_product.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'project': if vendor_product.project: original_value = vendor_product.project.name project = Project.get_by_id(field_value) vendor_product.update(project=project) field_value = project.name if project else None elif field == 'notes': original_value = vendor_product.notes vendor_product.update(notes=field_value) elif field == 'summary': original_value = vendor_product.summary vendor_product.update(summary=field_value) elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value vendor_product.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=vendor_product) elif field == 'serial_number': original_value = vendor_product.serial_number try: vendor_product.update(serial_number=field_value) except: return jsonify(jsonData), 500, {'ContentType': 'application/json'} jsonData = {'url': vendor_product.get_url()} # Return URL to reload page with new S/N vendor_product.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify(jsonData), 200, {'ContentType': 'application/json'}
def load_user(username): """Load user by usename.""" return User.get_by_username(username)