def analytes(request, analyte_id=None): """Get, create, or update information about cannabis analysis analytes.""" # Initialize model_id = analyte_id model_type = 'analytes' model_type_singular = 'analyte' # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # GET data. if request.method == 'GET': docs = get_objects(request, claims, org_id, model_id, model_type) return Response({'success': True, 'data': docs}, status=200) # POST data. elif request.method == 'POST': data = update_object(request, claims, model_type, model_type_singular, org_id) if data: return Response({'success': True, 'data': data}, status=200) else: message = 'Data not recognized. Please post either a singular object or an array of objects.' return Response({'error': True, 'message': message}, status=400) # DELETE data. elif request.method == 'DELETE': success = delete_object(request, claims, model_id, model_type, model_type_singular, org_id) if not success: message = f'Your must be an owner or quality assurance to delete {model_type}.' return Response({'error': True, 'message': message}, status=403) return Response({'success': True, 'data': []}, status=200)
def create_coas(request): """Generate certificates of analysis.""" # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # Get posted samples. posted_data = loads(request.body.decode('utf-8')) sample_ids = posted_data['sample_ids'] # Create certificates for each sample. data = [] for sample_id in sample_ids: # Get the sample data. sample_data = get_document( f'organizations/{org_id}/samples/{sample_id}') # Get the results for each sample. If there are no results, # then get the measurements for each sample and calculate # the results for each sample. Add a empty dictionary if missing everything. sample_results = get_collection(f'organization/{org_id}/results', order_by='updated_at', desc=True, filters=[{ 'key': 'sample_id', 'operation': '==', 'value': sample_id }]) if not sample_results: sample_results = calculate_results(request) if not sample_results: sample_results = [{}] # Define the certificate context. context = {**sample_data, **sample_results[0]} # Get the certificate template. template_name = sample_data.get('coa_template_ref', DEFAULT_TEMPLATE) # Create the PDF, keeping the data. # Efficiency gain: Keep the template in /tmp so they don't have # to be downloaded each iteration. certificate = generate_coas( context, coa_template=template_name, # output_pages=pages, # limits=limits ) data.append(certificate) # Return list of certificate data. return Response({'data': data}, status=200)
def review_coas(request): """Review certificates of analysis so that they can be approved and released.""" # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # Call generate_coas # - Make sure to fill-in reviewers signature. # Update the sample's certificate_status. return NotImplementedError
def approve_coas(request): """Approve certificates of analysis for release after they have been reviewed.""" # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # Restrict approving certificates to QA and owners. qa = claims.get('qa', []) owner = claims.get('owner', []) if org_id not in owner and org_id not in qa: message = f'Your must be an owner or quality assurance manager of this organization for this operation.' return Response({'error': True, 'message': message}, status=403) # Call generate_coas # - Make sure to fill-in approvers signature. # Update the sample's certificate_status. return NotImplementedError
def transfers(request, transfer_id=None): """Get, create, or update transfers.""" # Initialize. model_id = transfer_id model_type = 'transfers' model_type_singular = 'transfer' # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # GET data. if request.method == 'GET': docs = get_objects(request, claims, org_id, model_id, model_type) return Response({'success': True, 'data': docs}, status=200) # POST data. # TODO: Send transfer to the organization. # TODO: Notify the receiving organization. # TODO: Post to Metrc if user specifies. elif request.method == 'POST': data = update_object(request, claims, model_type, model_type_singular, org_id) if data: return Response({'success': True, 'data': data}, status=200) else: message = 'Data not recognized. Please post either a singular object or an array of objects.' return Response({'error': True, 'message': message}, status=400) # DELETE data. elif request.method == 'DELETE': success = delete_object(request, claims, model_id, model_type, model_type_singular, org_id) if not success: message = f'Your must be an owner or quality assurance to delete {model_type}.' return Response({'error': True, 'message': message}, status=403) return Response({'success': True, 'data': []}, status=200)
def projects(request, project_id=None): """Get, create, or update laboratory projects, a group of samples submitted at the same time by a given organization.""" # Initialize. model_id = project_id model_type = 'projects' model_type_singular = 'project' # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # GET data. if request.method == 'GET': docs = get_objects(request, claims, org_id, model_id, model_type) return Response({'success': True, 'data': docs}, status=200) # POST data. elif request.method == 'POST': data = update_object(request, claims, model_type, model_type_singular, org_id) if data: return Response({'success': True, 'data': data}, status=200) else: message = 'Data not recognized. Please post either a singular object or an array of objects.' return Response({'error': True, 'message': message}, status=400) # DELETE data. elif request.method == 'DELETE': success = delete_object(request, claims, model_id, model_type, model_type_singular, org_id) if not success: message = f'Your must be an owner or quality assurance to delete {model_type}.' return Response({'error': True, 'message': message}, status=403) return Response({'success': True, 'data': []}, status=200)
def release_coas(request): """Release certificates of analysis to the client.""" # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # Restrict approving certificates to QA and owners. qa = claims.get('qa', []) owner = claims.get('owner', []) if org_id not in owner and org_id not in qa: message = f'Your must be an owner or quality assurance manager of this organization for this operation.' return Response({'error': True, 'message': message}, status=403) # Get the samples. posted_data = loads(request.body.decode('utf-8')) sample_ids = posted_data['sample_ids'] # Update the certificate_status in Firestore. # Send (email and/or text) to the client's recipients. return NotImplementedError
def post_coas(request): """Post certificates of analysis to the state traceability system.""" # Authenticate the user. claims, status, org_id = authorize_user(request) if status != 200: return Response(claims, status=status) # Restrict approving certificates to QA and owners. qa = claims.get('qa', []) owner = claims.get('owner', []) if org_id not in owner and org_id not in qa: message = f'Your must be an owner or quality assurance manager of this organization for this operation.' return Response({'error': True, 'message': message}, status=403) # Get sample IDs. posted_data = loads(request.body.decode('utf-8')) sample_ids = posted_data['sample_ids'] # Format data for API requests. # Post certificates 1 by 1. return NotImplementedError