def add_reference(): by_id = request.form.get('by_id') by_class = request.form.get('by_class') to_id = request.form.get('to_id') to_class = request.form.get('to_class') variables = { 'by_id': by_id, 'by_class': by_class, 'to_id': to_id, 'to_class': to_class, } reference = Reference.create(**variables) by = get_record_by_id_and_class(by_id, by_class) to = get_record_by_id_and_class(to_id, to_class) log_entry = 'From {0} to {1}'.format(by.get_descriptive_url(), to.get_descriptive_url()) by.add_change_log_entry(action='Add', field='Reference', new_value=log_entry, changed_by=current_user) to.add_change_log_entry(action='Add', field='Reference', new_value=log_entry, changed_by=current_user) variables = {'ref': reference, 'success': True} return render_template('common/reference_to_row.html', **variables)
def put(self, id, parent_class, link_id): link = Link.get_by_id(link_id) if not link: abort(404, message="Link {} doesn't exist".format(id)) data = self.reqparse.parse_args() description = data.description url = formatUrl(data.url) old_url = '<a href="{0}" target="new">{1}</a>'.format( link.url, link.description or link.url) new_url = '<a href="{0}" target="new">{1}</a>'.format( url, description or url) link.update(url=url, description=description) link.save() parent = get_record_by_id_and_class(id, parent_class) parent.add_change_log_entry(action='Edit', field='URL', original_value=old_url, new_value=new_url) variables = { 'link': link, 'parent_object': get_record_by_id_and_class(id, parent_class), 'api_url': url_for("api.generic_links", id=id, parent_class=parent_class) } return render_template('shared/link_row.html', **variables)
def delete(self, record_id, image_id, record_class): record = get_record_by_id_and_class(record_id, record_class) image = Image.get_by_id(image_id) image_path = Path( os.path.join(current_app.config['UPLOAD_FOLDER'], image.path)) if not image: abort(404, message='Image {} does not exist'.format(id)) if image_path.is_file(): image_path.unlink() # Delete 'images' directory if empty image_path = image_path.parent if not os.listdir(str(image_path)): image_path.rmdir() # Delete PK directory if empty image_path = image_path.parent print(str(image_path)) if not os.listdir(str(image_path)): image_path.rmdir() record.images.remove(image) log_entry = 'Image deleted: <strong>{0}</strong>'.format(image.title or '') record.add_change_log_entry(action='Remove', field='Image', original_value=log_entry) image.delete() record.save() return jsonify({'message': 'Successfully deleted Image {}'.format(id)})
def put(self, record_id, record_class, image_id): image = Image.get_by_id(image_id) if not image: abort(404, message='Image {} does not exist'.format(image_id)) field = request.form['name'].split('-')[0] value = request.form['value'] old_value = '' new_value = '' if field == 'title': old_value = image.title image.update(title=value) new_value = value if field == 'description': old_value = '{0}<br><i>{1}</i>'.format(image.description or '', image.title) image.update(description=value) new_value = '{0}<br><i>{1}</i>'.format(value or '', image.title) record = get_record_by_id_and_class(record_id, record_class) field_edit = '{0}<br>Image'.format(field.capitalize()) record.add_change_log_entry(action='Edit', field=field_edit, original_value=old_value, new_value=new_value) record.save() variables = { 'image': image, 'api_url': url_for('api.generic_images', record_id=record_id, record_class=record_class) } return render_template('shared/image_gallery_item.html', **variables)
def delete(self, record_id, document_id, record_class): parent = get_record_by_id_and_class(record_id, record_class) document = Document.get_by_id(document_id) document_path = Path( os.path.join(current_app.config['UPLOAD_FOLDER'], document.path)) if not document: abort(404, message='Document {} does not exist'.format(record_id)) if document_path.is_file(): document_path.unlink() # Delete 'documents' directory if empty document_path = document_path.parent if not os.listdir(str(document_path)): document_path.rmdir() # Delete PK directory if empty document_path = document_path.parent if not os.listdir(str(document_path)): document_path.rmdir() parent.documents.remove(document) log_entry = 'Document deleted: <strong>{0}</strong>'.format( document.title or '') parent.add_change_log_entry(action='Remove', field='Document', original_value=log_entry) document.delete() db.session.add(parent) db.session.commit() return jsonify( {'message': 'Successfully deleted Document {}'.format(id)})
def get_revisionlog_modal(): record_id = request.form.get('record_id') record_class = request.form.get('record_class') variables = { 'parent_object': get_record_by_id_and_class(record_id, record_class) } return render_template('shared/view_revisionlog_modal.html', **variables)
def put(self, record_id, record_class, document_id): document = Document.get_by_id(document_id) if not document: abort(404, message='Document {} does not exist'.format(document_id)) data = self.reqparse.parse_args() description = data.description old_desc = '{0}<br><i>{1}</i>'.format(document.description or '', document.title) new_desc = '{0}<br><i>{1}</i>'.format(description or '', document.title) document.update(description=description) record = get_record_by_id_and_class(record_id, record_class) field = 'Description<br>Document' record.add_change_log_entry(action='Edit', field=field, original_value=old_desc, new_value=new_desc) variables = { 'document': document, 'parent_object': record, 'api_url': url_for('api.generic_documents', record_id=record_id, record_class=record_class) } return render_template('shared/document_row.html', **variables)
def post(self, id, parent_class, link_id=None): if id and link_id is not None: return self.put(id, parent_class, link_id) data = self.reqparse.parse_args() title = data.title description = data.description url = formatUrl(data.url) link = Link(url, title, description, current_user.id) db.session.add(link) db.session.commit() parent = get_record_by_id_and_class(id, parent_class) parent.links.append(link) log_entry = '<a href="{0}" target="new">{1}</a>'.format( url, description or url) parent.add_change_log_entry(action='Add', field='URL', new_value=log_entry) db.session.add(parent) db.session.commit() variables = { 'link': link, 'parent_object': parent, 'api_url': url_for("api.generic_links", id=id, parent_class=parent_class) } return render_template('shared/link_row.html', **variables)
def delete_reference(): id = request.form['pk'] reference = Reference.get_by_id(id) by = get_record_by_id_and_class(reference.by_id, reference.by_class) to = get_record_by_id_and_class(reference.to_id, reference.to_class) log_entry = 'From {0} to {1}'.format(by.get_descriptive_url(), to.get_descriptive_url()) by.add_change_log_entry(action='Remove', field='Reference', original_value=log_entry, changed_by=current_user) to.add_change_log_entry(action='Remove', field='Reference', original_value=log_entry, changed_by=current_user) reference.delete() return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def self_approve(): record_id = request.values['parent_id'] record_class = request.values['parent_class'].lower() approve = request.values['approve'] record = get_record_by_id_and_class(record_id, record_class) record.self_approved = approve record.save() return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def get_reference_object(self, direction): # Imports need to be here to prevent circular imports if direction == 'to': reference_object_class_name = self.to_class reference_object_id = self.to_id else: reference_object_class_name = self.by_class reference_object_id = self.by_id reference_object = get_record_by_id_and_class( reference_object_id, reference_object_class_name) return reference_object
def delete_approver(): # TODO: verify ID's actually have DB entires record_id = request.values['parent_id'] record_class = request.values['parent_class'].lower() approver_id = request.values['approver_id'] approver = Approver.get_by_id(approver_id) record = get_record_by_id_and_class(record_id, record_class) record.approvers.remove(approver) record.save() approver.delete() return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def get_approvals(): record_id = request.values['parent_id'] record_class = request.values['parent_class'].lower() record = get_record_by_id_and_class(record_id, record_class) approvals = [] for approval in record.approvers: if current_user == approval.approver and not approval.approved_at: approvals.append(approval) variables = { 'parent_object': record, 'approvals': approvals, 'users': User.query.all() } return render_template('shared/approvals_modal.html', **variables)
def delete(self, id, link_id, parent_class): parent = get_record_by_id_and_class(id, parent_class) link = Link.get_by_id(link_id) if not link: abort(404, message="Link {} doesn't exist".format(id)) parent.links.remove(link) log_entry = '<a href="{0}" target="new">{1}</a>'.format( link.url, link.description or link.url) parent.add_change_log_entry(action='Remove', field='URL', original_value=log_entry) link.delete() db.session.add(parent) db.session.commit() return jsonify({'message': "Successfully deleted Link {}".format(id)})
def post(self, record_id, record_class, document_id=None): if record_id and document_id: return self.put(record_id, document_id) file = request.files['file'] if file and allowed_document(file.filename): title = secure_filename(file.filename) extension = ''.join(Path(file.filename).suffixes ) # Should get extensions like .tar.gz as well basepath = get_basepath(record_id, record_class, 'documents') # Save file with temp UUID to begin with tmp_filename = uuid.uuid4().hex + '-' + title tmp_filepath = os.path.join(basepath, tmp_filename) file.save(tmp_filepath) document = Document.create(path=tmp_filepath, title=title) # Update filepath and DB entry based on PK filename = '{0}{1}'.format(document.id, extension) filepath = os.path.join(basepath, filename) os.rename(tmp_filepath, filepath) document.path = os.path.relpath( basepath, current_app.config['UPLOAD_FOLDER']) + '/' + filename document.save() record = get_record_by_id_and_class(record_id, record_class) record.documents.append(document) log_entry = 'Document added: <strong>{0}</strong>'.format( document.title or '') record.add_change_log_entry(action='Add', field='Document', new_value=log_entry) record.save() variables = { 'document': document, 'parent_object': record, 'api_url': url_for('api.generic_documents', record_id=record_id, record_class=record_class) } return render_template('shared/document_row.html', **variables) return "<strong>{0}</strong> is not a document file.".format( file.filename), 500
def post(self, record_id, record_class, image_id=None): if record_id and image_id is not None: return self.put(record_id, record_class, image_id) file = request.files['file'] if file and allowed_image(file.filename): title = secure_filename(file.filename) extension = ''.join(Path(file.filename).suffixes) basepath = get_basepath(record_id, record_class, 'images') # Save file with temp UUID to begin with tmp_filename = uuid.uuid4().hex + '-' + title tmp_filepath = os.path.join(basepath, tmp_filename) file.save(tmp_filepath) image = Image.create(path=tmp_filepath, title=title) # Update filepath and DB entry based on PK filename = '{0}{1}'.format(image.id, extension) filepath = os.path.join(basepath, filename) os.rename(tmp_filepath, filepath) image.path = os.path.relpath( basepath, current_app.config['UPLOAD_FOLDER']) + '/' + filename image.save() record = get_record_by_id_and_class(record_id, record_class) record.images.append(image) log_entry = 'Image added: <strong>{0}</strong>'.format(image.title or '') record.add_change_log_entry(action='Add', field='Image', new_value=log_entry) record.save() variables = { 'image': image, 'api_url': url_for('api.generic_images', record_id=record_id, record_class=record_class) } return render_template('shared/image_gallery_item.html', **variables) return '<strong>{0}</strong> is not an image file.'.format( file.filename), 500
def update_approver(): # TODO: verify ID's actually have DB entires record_id = request.values['parent_id'] record_class = request.values['parent_class'].lower() approver_id = request.values['approver_id'] approver = Approver.get_by_id(int(approver_id)) if approver.approver_id != request.values['approver']: approver.update(approver_id=request.values['approver'], commit=False) if approver.capacity != request.values['capacity']: approver.update(capacity=request.values['capacity'], commit=False) approver.save() record = get_record_by_id_and_class(record_id, record_class) variables = { 'approver': approver, 'users': User.query.all(), 'parent_object': record } return render_template('shared/approver_row.html', **variables)
def add_approver(): # TODO: verify ID's actually have DB entires record_id = request.values['pk'] record_class = request.values['class'].lower() record = get_record_by_id_and_class(record_id, record_class) variables = { 'approver_id': request.values['approver_id'], 'capacity': request.values['capacity'] } approver = Approver.create(**variables) record.approvers.append(approver) record.save() variables = { 'approver': approver, 'users': User.query.all(), 'parent_object': record } return render_template('shared/approver_row.html', **variables)
def update_discrepancy(): # This method differs from other update methods as it's an full table row edit and save id = request.values['pk'] parent_id = request.values['parent_id'] parent_class = request.values['parent_class'] parent = get_record_by_id_and_class(parent_id, parent_class) discrepancy = Discrepancy.get_by_id(int(id)) field = 'Discrepancy {0}'.format(discrepancy.discrepancy_number) if discrepancy.description != request.values['description']: old_value = discrepancy.description discrepancy.update(description=request.values['description'], commit=False) parent.add_change_log_entry(action='Edit', field=field, original_value=old_value, new_value=request.values['description']) if int(discrepancy.disposition_id) != int(request.values['disposition']): old_value = discrepancy.disposition.name if not request.values['disposition'] == 'None': disposition = Disposition.get_by_id(request.values['disposition']) new_value = disposition.name discrepancy.update(disposition_id=request.values['disposition'], commit=False) parent.add_change_log_entry(action='Edit', field=field, original_value=old_value, new_value=new_value) else: discrepancy.update(disposition_id=None, commit=False) parent.add_change_log_entry(action='Edit', field=field, original_value=old_value) if discrepancy.justification != request.values['justification']: old_value = discrepancy.justification discrepancy.update(justification=request.values['justification'], commit=False) parent.add_change_log_entry(action='Edit', field=field, original_value=old_value, new_value=request.values['justification']) if discrepancy.state != request.values['state']: old_value = discrepancy.state discrepancy.update(state=request.values['state'], commit=False) parent.add_change_log_entry(action='Edit', field=field, original_value=old_value, new_value=request.values['state']) discrepancy.save() variables = { 'discrepancy': discrepancy, 'dispositions': Disposition.query.all(), 'parent_object': parent } return render_template('product/discrepancy_row.html', **variables)
def create_discrepancy(): parent_id = request.values['pk'] parent_class = request.values['class'] description = request.values['description'] disposition_id = request.values['disposition_id'] justification = request.values['justification'] state = request.values['state'] parent = get_record_by_id_and_class(parent_id, parent_class) disposition = Disposition.get_by_id(disposition_id) discrepancy = Discrepancy.create(created_by=current_user, discrepancy_number='{0:02d}'.format(len(parent.discrepancies) + 1), description=description, disposition=disposition, justification=justification, state=state) parent.discrepancies.append(discrepancy) # parent should not be None at this point log_entry = 'Discrepancy {0}'.format(discrepancy.discrepancy_number) parent.add_change_log_entry(action='Add', field='Discrepancy', new_value=log_entry) parent.save() dispositions = Disposition.query.all() variables = { 'discrepancy': discrepancy, 'dispositions': dispositions, 'parent_object': parent } return render_template('product/discrepancy_row.html', **variables)
def get_bookmarked_object(self): bookmarked_class = self.bookmarked_class bookmarked_id = self.bookmarked_id bookmarked_object = get_record_by_id_and_class(bookmarked_id, bookmarked_class) return bookmarked_object
def approve(): approver_ids = request.values.getlist('approver_id') record_id = request.values['parent_id'] record_class = request.values['parent_class'].lower() record = get_record_by_id_and_class(record_id, record_class) for approver_id in approver_ids: approver = Approver.get_by_id(approver_id) resolution = request.values['approval_' + approver_id] comment = request.values['approval_comment_' + approver_id] if resolution == 'approve': approver.approved_at = dt.datetime.utcnow() approver.save() record.add_workflow_log_entry(current_user, capacity=approver.capacity, action='Approved', comment=comment) move_to_released = True for approval in record.approvers: if not approval.approved_at: move_to_released = False break if move_to_released: try: comment = 'Revision ' + record.revision except AttributeError: comment = None #TODO: PLAIDmin user record.add_workflow_log_entry( current_user, capacity='PLAIDmin', action=record.workflow.released_state, comment=comment) record.state = record.workflow.released_state send_email(subject='{0} {1}: {2}'.format( record.descriptor, record.state, record.get_name()), recipients=[record.owner.email], text_body=render_template( 'mail/approvals/record_released.txt', record=record), html_body=render_template( 'mail/approvals/record_released.html', record=record)) record.save() elif resolution == 'edit': record.add_workflow_log_entry(current_user, capacity=approver.capacity, action='Required Edit', comment=comment) record.state = 'In Work' record.save() variables = { 'record': record, 'approver': approver, 'comment': comment } send_email(subject='Edit Required on {0}: {1}'.format( record.descriptor, record.get_name()), recipients=[approver.approver.email], text_body=render_template( 'mail/approvals/record_required_edit.txt', **variables), html_body=render_template( 'mail/approvals/record_required_edit.html', **variables)) elif resolution == 'reassign': approver.approver_id = request.values['approval_reassign_' + approver_id] approver.save() record.add_workflow_log_entry(current_user, capacity=approver.capacity, action='Reassigned', comment=comment) record.save() variables = { 'record': record, 'approver': approver, 'comment': comment } # Send email to owner of record to let him know approver has changed send_email(subject='Approval Reassigned for {0}: {1}'.format( record.descriptor, record.get_name()), recipients=[record.owner.email], text_body=render_template( 'mail/approvals/changed_approver.txt', **variables), html_body=render_template( 'mail/approvals/changed_approver.html', **variables)) # Send email to new approver to let him know he needs to approve this record send_email( subject='Approval Required: {0}'.format(record.get_name()), recipients=[approver.approver.email], text_body=render_template('mail/approvals/new_approver.txt', **variables), html_body=render_template('mail/approvals/new_approver.html', **variables)) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def get_workflow_container(): record_id = request.values['record_id'] record_class = request.values['record_class'] record = get_record_by_id_and_class(record_id, record_class) return render_template('shared/workflow_container.html', record=record)
def get_thumbnail_modal(): record_id = request.form.get('record_id') record_class = request.form.get('record_class') record = get_record_by_id_and_class(record_id, record_class) variables = {'record': record} return render_template('shared/thumbnail_modal.html', **variables)