def set_logging_context(event): request = event.request params = {} if 'plan' in request.validated: params['PLAN_REV'] = request.validated['plan'].rev params['PLANID'] = request.validated['plan'].planID update_logging_context(request, params)
def validate_file_upload(request): update_logging_context(request, {'document_id': '__new__'}) if 'file' not in request.POST or not hasattr(request.POST['file'], 'filename'): request.errors.add('body', 'file', 'Not Found') request.errors.status = 404 else: request.validated['file'] = request.POST['file']
def set_logging_context(event): request = event.request params = {} if "plan" in request.validated: params["PLAN_REV"] = request.validated["plan"].rev params["PLANID"] = request.validated["plan"].planID update_logging_context(request, params)
def validate_complaint_data(request): if not request.check_accreditation(request.tender.edit_accreditation): request.errors.add('procurementMethodType', 'accreditation', 'Broker Accreditation level does not permit complaint creation') request.errors.status = 403 return update_logging_context(request, {'complaint_id': '__new__'}) model = type(request.tender).complaints.model_class return validate_data(request, model)
def validate_contract_data(request): update_logging_context(request, {'contract_id': '__new__'}) data = request.validated['json_data'] = validate_json_data(request) model = request.contract_from_data(data, create=False) if hasattr(request, 'check_accreditation') and not request.check_accreditation(model.create_accreditation): request.errors.add('contract', 'accreditation', 'Broker Accreditation level does not permit contract creation') request.errors.status = 403 raise error_handler(request.errors) return validate_data(request, model, data=data)
def validate_tender_data(request): update_logging_context(request, {'tender_id': '__new__'}) data = validate_json_data(request) if data is None: return model = request.tender_from_data(data, create=False) return validate_data(request, model, data=data)
def validate_file_upload(request): update_logging_context(request, {'document_id': '__new__'}) if request.registry.docservice_url and request.content_type == "application/json": return validate_document_data(request) if 'file' not in request.POST or not hasattr(request.POST['file'], 'filename'): request.errors.add('body', 'file', 'Not Found') request.errors.status = 404 raise error_handler(request.errors) else: request.validated['file'] = request.POST['file']
def upload_file(request, blacklisted_fields=DOCUMENT_BLACKLISTED_FIELDS): first_document = ( request.validated["documents"][0] if "documents" in request.validated and request.validated["documents"] else None ) if request.content_type == "multipart/form-data": data = request.validated["file"] filename = get_filename(data) content_type = data.type in_file = data.file else: filename = first_document.title content_type = request.content_type in_file = request.body_file if hasattr(request.context, "documents"): # upload new document model = type(request.context).documents.model_class else: # update document model = type(request.context) document = model({"title": filename, "format": content_type}) document.__parent__ = request.context if "document_id" in request.validated: document.id = request.validated["document_id"] if first_document: for attr_name in type(first_document)._fields: if attr_name not in blacklisted_fields: setattr(document, attr_name, getattr(first_document, attr_name)) key = generate_id() document_route = request.matched_route.name.replace("collection_", "") document_path = request.current_route_path( _route_name=document_route, document_id=document.id, _query={"download": key} ) document.url = "/" + "/".join(document_path.split("/")[3:]) conn = getattr(request.registry, "s3_connection", None) if conn: bucket = conn.get_bucket(request.registry.bucket_name) filename = "{}/{}/{}".format(request.validated["plan_id"], document.id, key) key = bucket.new_key(filename) key.set_metadata("Content-Type", document.format) key.set_metadata( "Content-Disposition", build_header(document.title, filename_compat=quote(document.title.encode("utf-8"))) ) key.set_contents_from_file(in_file) key.set_acl("private") else: filename = "{}_{}".format(document.id, key) request.validated["plan"]["_attachments"][filename] = { "content_type": document.format, "data": b64encode(in_file.read()), } update_logging_context(request, {"file_size": in_file.tell()}) return document
def set_logging_context(event): request = event.request params = {} params['ROLE'] = str(request.authenticated_role) if request.params: params['PARAMS'] = str(dict(request.params)) if request.matchdict: for x, j in request.matchdict.items(): params[x.upper()] = j update_logging_context(request, params)
def validate_plan_data(request): update_logging_context(request, {'plan_id': '__new__'}) data = validate_json_data(request) if data is None: return model = request.plan_from_data(data, create=False) if hasattr(request, 'check_accreditation') \ and not any([request.check_accreditation(acc) for acc in model.create_accreditations]): request.errors.add('plan', 'accreditation', 'Broker Accreditation level does not permit plan creation') request.errors.status = 403 return return validate_data(request, model, data=data)
def validate_question_data(request): if not request.check_accreditation(request.tender.edit_accreditation): request.errors.add('procurementMethodType', 'accreditation', 'Broker Accreditation level does not permit question creation') request.errors.status = 403 return if request.tender.get('mode', None) is None and request.check_accreditation('t'): request.errors.add('procurementMethodType', 'mode', 'Broker Accreditation level does not permit question creation') request.errors.status = 403 return update_logging_context(request, {'question_id': '__new__'}) model = type(request.tender).questions.model_class return validate_data(request, model)
def validate_tender_data(request): update_logging_context(request, {'tender_id': '__new__'}) data = validate_json_data(request) if data is None: return model = request.tender_from_data(data, create=False) if not request.check_accreditation(model.create_accreditation): request.errors.add('procurementMethodType', 'accreditation', 'Broker Accreditation level does not permit tender creation') request.errors.status = 403 return return validate_data(request, model, data=data)
def upload_file(request, blacklisted_fields=DOCUMENT_BLACKLISTED_FIELDS): first_document = request.validated['documents'][0] if 'documents' in request.validated and request.validated['documents'] else None if request.content_type == 'multipart/form-data': data = request.validated['file'] filename = get_filename(data) content_type = data.type in_file = data.file else: filename = first_document.title content_type = request.content_type in_file = request.body_file if hasattr(request.context, "documents"): # upload new document model = type(request.context).documents.model_class else: # update document model = type(request.context) document = model({'title': filename, 'format': content_type}) document.__parent__ = request.context if 'document_id' in request.validated: document.id = request.validated['document_id'] if first_document: for attr_name in type(first_document)._fields: if attr_name not in blacklisted_fields: setattr(document, attr_name, getattr(first_document, attr_name)) key = generate_id() document_route = request.matched_route.name.replace("collection_", "") document_path = request.current_route_path(_route_name=document_route, document_id=document.id, _query={'download': key}) document.url = '/' + '/'.join(document_path.split('/')[3:]) conn = getattr(request.registry, 's3_connection', None) if conn: bucket = conn.get_bucket(request.registry.bucket_name) filename = "{}/{}/{}".format(request.validated['contract_id'], document.id, key) key = bucket.new_key(filename) key.set_metadata('Content-Type', document.format) key.set_metadata("Content-Disposition", build_header(document.title, filename_compat=quote(document.title.encode('utf-8')))) key.set_contents_from_file(in_file) key.set_acl('private') else: filename = "{}_{}".format(document.id, key) request.validated['contract']['_attachments'][filename] = { "content_type": document.format, "data": b64encode(in_file.read()) } update_logging_context(request, {'file_size': in_file.tell()}) return document
def validate_complaint_data_stage2(request): if not request.check_accreditation(request.tender.edit_accreditation): request.errors.add('procurementMethodType', 'accreditation', 'Broker Accreditation level does not permit complaint creation') request.errors.status = 403 raise error_handler(request.errors) if request.tender.get('mode', None) is None and request.check_accreditation('t'): request.errors.add('procurementMethodType', 'mode', 'Broker Accreditation level does not permit complaint creation') request.errors.status = 403 raise error_handler(request.errors) update_logging_context(request, {'complaint_id': '__new__'}) data = validate_data(request, type(request.tender).complaints.model_class) if data: if validate_author(request, request.tender['shortlistedFirms'], request.validated['complaint']): return data # validate is OK else: return None # we catch errors return data
def validate_tender_data(request): update_logging_context(request, {'tender_id': '__new__'}) data = validate_json_data(request) if data is None: return model = request.tender_from_data(data, create=False) if not request.check_accreditation(model.create_accreditation): request.errors.add('procurementMethodType', 'accreditation', 'Broker Accreditation level does not permit tender creation') request.errors.status = 403 return data = validate_data(request, model, data=data) if data and data.get('procuringEntity', {}).get('kind', '') not in model.procuring_entity_kinds: request.errors.add('procuringEntity', 'kind', '{kind!r} procuringEntity cannot publish this type of procedure. ' 'Only {kinds} are allowed.'.format(kind=data.get('procuringEntity', {}).get('kind', ''), kinds=', '.join(model.procuring_entity_kinds))) request.errors.status = 403
def validate_change_data(request): update_logging_context(request, {'change_id': '__new__'}) data = validate_json_data(request) if data is None: return return validate_data(request, Change, data=data)
def validate_cancellation_data(request): update_logging_context(request, {'cancellation_id': '__new__'}) model = type(request.tender).cancellations.model_class return validate_data(request, model)
def validate_contract_data(request): update_logging_context(request, {'contract_id': '__new__'}) model = type(request.auction).contracts.model_class return validate_data(request, model)
def validate_cancellation_data(request): update_logging_context(request, {'cancellation_id': '__new__'}) return validate_data(request, Cancellation)
def validate_complaint_data(request): update_logging_context(request, {'complaint_id': '__new__'}) model = type(request.context).complaints.model_class return validate_data(request, model)
def validate_transfer_data(request): update_logging_context(request, {"transfer_id": "__new__"}) data = validate_json_data(request) model = Transfer return validate_data(request, model, data=data)
def validate_inspection_data(request): update_logging_context(request, {'INSPECTION_ID': '__new__'}) return validate_data(request, Inspection)
def validate_award_data(request): update_logging_context(request, {'award_id': '__new__'}) return validate_data(request, Award)
def validate_bid_data(request): update_logging_context(request, {'bid_id': '__new__'}) return validate_data(request, Bid)
def validate_tender_data(request): update_logging_context(request, {'tender_id': '__new__'}) return validate_data(request, Tender)
def validate_lot_data(request): update_logging_context(request, {'lot_id': '__new__'}) return validate_data(request, Lot)
def validate_contract_data(request): update_logging_context(request, {'contract_id': '__new__'}) return validate_data(request, Contract)
def validate_lot_data(request): update_logging_context(request, {'lot_id': '__new__'}) model = type(request.tender).lots.model_class return validate_data(request, model)
def validate_cancellation_data(request, **kwargs): update_logging_context(request, {'cancellation_id': '__new__'}) model = type(request.auction).cancellations.model_class return validate_data(request, model, "cancellation")
def validate_award_data(request): update_logging_context(request, {"award_id": "__new__"}) model = type(request.tender).awards.model_class return validate_data(request, model)
def validate_agreement_data(request, **kwargs): update_logging_context(request, {"agreement_id": "__new__"}) data = validate_json_data(request) model = request.agreement_from_data(data, create=False) _validate_agreement_accreditation_level(request, model) return validate_data(request, model, data=data)
def validate_milestone_data(request, **kwargs): update_logging_context(request, {"milestone_id": "__new__"}) model = type(request.validated["contract"]).milestones.model_class return validate_data(request, model)
def validate_lot_data(request, **kwargs): update_logging_context(request, {'lot_id': '__new__'}) model = type(request.auction).lots.model_class return validate_data(request, model)
def validate_agreement_data(request, **kwargs): update_logging_context(request, {"agreement_id": "__new__"}) model = type(request.tender).agreements.model_class return validate_data(request, model)
def validate_contract_data(request): update_logging_context(request, {"contract_id": "__new__"}) model = type(request.tender).contracts.model_class return validate_data(request, model)
def validate_complaint_data(request): update_logging_context(request, {"complaint_id": "__new__"}) validate_complaint_accreditation_level(request) model = type(request.tender).complaints.model_class return validate_data(request, model)
def validate_contract_data(request): update_logging_context(request, {"contract_id": "__new__"}) data = validate_json_data(request) model = request.contract_from_data(data, create=False) validate_contract_accreditation_level(request, model) return validate_data(request, model, data=data)
def validate_question_data(request): update_logging_context(request, {'question_id': '__new__'}) return validate_data(request, Question)
def validate_change_data(request): update_logging_context(request, {'change_id': '__new__'}) data = validate_json_data(request) return validate_data(request, Change, data=data)
def validate_change_data(request): update_logging_context(request, {"change_id": "__new__"}) data = validate_json_data(request) return validate_data(request, Change, data=data)
def validate_plan_data(request): update_logging_context(request, {'plan_id': '__new__'}) return validate_data(request, Plan)
def validate_award_data(request, **kwargs): update_logging_context(request, {'award_id': '__new__'}) model = type(request.auction).awards.model_class return validate_data(request, model, "award")