def save_plan(request): """ Save plan object to database :param request: :return: True if Ok """ plan = request.validated["plan"] patch = get_revision_changes(plan.serialize("plain"), request.validated["plan_src"]) if patch: append_revision(request, plan, patch) old_date_modified = plan.dateModified if getattr(plan, "modified", True): now = get_now() plan.dateModified = now if any(c for c in patch if c["path"].startswith("/cancellation/")): plan.cancellation.date = now with handle_store_exceptions(request): plan.store(request.registry.db) LOGGER.info( "Saved plan {}: dateModified {} -> {}".format( plan.id, old_date_modified and old_date_modified.isoformat(), plan.dateModified.isoformat()), extra=context_unpack(request, {"MESSAGE_ID": "save_plan"}, {"PLAN_REV": plan.rev}), ) return True
def save_plan(request): """ Save plan object to database :param request: :return: True if Ok """ plan = request.validated['plan'] patch = get_revision_changes(plan.serialize("plain"), request.validated['plan_src']) if patch: plan.revisions.append( Revision({ 'author': request.authenticated_userid, 'changes': patch, 'rev': plan.rev })) old_date_modified = plan.dateModified plan.dateModified = get_now() try: plan.store(request.registry.db) except ModelValidationError, e: # pragma: no cover for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except Exception, e: # pragma: no cover request.errors.add('body', 'data', str(e))
def save_tender(request): tender = request.validated["tender"] if tender.mode == u"test": set_modetest_titles(tender) patch = get_revision_changes(tender.serialize("plain"), request.validated["tender_src"]) if patch: now = get_now() append_tender_revision(request, tender, patch, now) old_date_modified = tender.dateModified if getattr(tender, "modified", True): tender.dateModified = now with handle_store_exceptions(request): tender.store(request.registry.db) LOGGER.info( "Saved tender {}: dateModified {} -> {}".format( tender.id, old_date_modified and old_date_modified.isoformat(), tender.dateModified.isoformat()), extra=context_unpack(request, {"MESSAGE_ID": "save_tender"}, {"RESULT": tender.rev}), ) return True
def save_contract(request): """ Save contract object to database :param request: :return: True if Ok """ contract = request.validated['contract'] if contract.mode == u'test': set_modetest_titles(contract) patch = get_revision_changes(contract.serialize("plain"), request.validated['contract_src']) if patch: contract.revisions.append( Revision({'author': request.authenticated_userid, 'changes': patch, 'rev': contract.rev})) old_date_modified = contract.dateModified contract.dateModified = get_now() try: contract.store(request.registry.db) except ModelValidationError, e: # pragma: no cover for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except Exception, e: # pragma: no cover request.errors.add('body', 'data', str(e))
def save_lot(request): lot = request.validated['lot'] if lot.mode == u'test': set_modetest_titles(lot) patch = get_revision_changes(lot.serialize("plain"), request.validated['lot_src']) if patch: return store_lot(lot, patch, request)
def save_object(request, obj_name, with_test_mode=True): obj = request.validated[obj_name] if with_test_mode and obj.mode == u"test": set_modetest_titles(obj) patch = get_revision_changes(obj.serialize("plain"), request.validated["%s_src" % obj_name]) if patch: now = get_now() append_framework_revision(request, obj, patch, now) old_date_modified = obj.dateModified if getattr(obj, "modified", True): obj.dateModified = now with handle_store_exceptions(request): obj.store(request.registry.db) LOGGER.info( "Saved {} {}: dateModified {} -> {}".format( obj_name, obj.id, old_date_modified and old_date_modified.isoformat(), obj.dateModified.isoformat() ), extra=context_unpack(request, {"MESSAGE_ID": "save_{}".format(obj_name)}, {"RESULT": obj.rev}), ) return True
def save_agreement(request): """ Save agreement object to database :param request: :return: True if Ok """ agreement = request.validated["agreement"] if agreement.mode == u"test": set_modetest_titles(agreement) patch = get_revision_changes(agreement.serialize("plain"), request.validated["agreement_src"]) if patch: append_revision(request, agreement, patch) old_date_modified = agreement.dateModified agreement.dateModified = get_now() with handle_store_exceptions(request): agreement.store(request.registry.db) LOGGER.info( "Saved agreement {}: dateModified {} -> {}".format( agreement.id, old_date_modified and old_date_modified.isoformat(), agreement.dateModified.isoformat(), ), extra=context_unpack(request, {"MESSAGE_ID": "save_agreement"}, {"AGREEMENT_REV": agreement.rev}), ) return True
def save_contract(request): """ Save contract object to database :param request: :return: True if Ok """ contract = request.validated["contract"] if contract.mode == "test": set_modetest_titles(contract) patch = get_revision_changes(contract.serialize("plain"), request.validated["contract_src"]) if patch: append_revision(request, contract, patch) old_date_modified = contract.dateModified contract.dateModified = get_now() with handle_store_exceptions(request): contract.store(request.registry.db) LOGGER.info( "Saved contract {}: dateModified {} -> {}".format( contract.id, old_date_modified and old_date_modified.isoformat(), contract.dateModified.isoformat()), extra=context_unpack(request, {"MESSAGE_ID": "save_contract"}, {"CONTRACT_REV": contract.rev}), ) return True
def apply_patch(request, data=None, save=True, src=None): data = request.validated["data"] if data is None else data patch = data and apply_data_patch(src or request.context.serialize(), data) if patch: request.context.import_data(patch) if save: return save_plan(request) if get_revision_changes(request.validated["plan"].serialize("plain"), request.validated["plan_src"]): return True # should return True if there're any actual changes (either save = True or False
def save_plan(request): plan = request.validated['plan'] patch = get_revision_changes(plan.serialize("plain"), request.validated['plan_src']) plan.revisions.append(Revision({'author': request.authenticated_userid, 'changes': patch, 'rev': plan.rev})) old_dateModified = plan.dateModified plan.dateModified = get_now() try: plan.store(request.registry.db_plan) except ModelValidationError, e: for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422
def save_auction(request): auction = request.validated['auction'] if request.authenticated_role == 'Administrator': can_be_changed = ['procurementMethodDetails'] for field in can_be_changed: if field in request.json['data']: auction[field] = request.json['data'][field] if auction.mode == u'test': set_modetest_titles(auction) patch = get_revision_changes(auction.serialize("plain"), request.validated['auction_src']) if patch: now = get_now() status_changes = [ p for p in patch if not p['path'].startswith('/bids/') and p['path'].endswith("/status") and p['op'] == "replace" ] for change in status_changes: obj = resolve_pointer(auction, change['path'].replace('/status', '')) if obj and hasattr(obj, "date"): date_path = change['path'].replace('/status', '/date') if obj.date and not any( [p for p in patch if date_path == p['path']]): patch.append({ "op": "replace", "path": date_path, "value": obj.date.isoformat() }) elif not obj.date: patch.append({"op": "remove", "path": date_path}) obj.date = now auction.revisions.append( type(auction).revisions.model_class({ 'author': request.authenticated_userid, 'changes': patch, 'rev': auction.rev })) old_dateModified = auction.dateModified if getattr(auction, 'modified', True): auction.dateModified = now try: auction.store(request.registry.db) except ModelValidationError, e: for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except ResourceConflict, e: # pragma: no cover request.errors.add('body', 'data', str(e)) request.errors.status = 409
def save_inspection(request, date_modified=None): inspection = request.validated['inspection'] patch = get_revision_changes(request.validated['inspection_src'], inspection.serialize("plain")) if patch: add_revision(request, inspection, patch) old_date_modified = inspection.dateModified inspection.dateModified = date_modified or get_now() try: inspection.store(request.registry.db) except ModelValidationError, e: # pragma: no cover for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except Exception, e: # pragma: no cover request.errors.add('body', 'data', str(e))
def save_tender(request): tender = request.validated['tender'] if tender.mode == u'test': set_modetest_titles(tender) patch = get_revision_changes(tender.serialize("plain"), request.validated['tender_src']) if patch: now = get_now() status_changes = [ p for p in patch if not p['path'].startswith('/bids/') and p['path'].endswith("/status") and p['op'] == "replace" ] for change in status_changes: obj = resolve_pointer(tender, change['path'].replace('/status', '')) if obj and hasattr(obj, "date"): date_path = change['path'].replace('/status', '/date') if obj.date and not any( [p for p in patch if date_path == p['path']]): patch.append({ "op": "replace", "path": date_path, "value": obj.date.isoformat() }) elif not obj.date: patch.append({"op": "remove", "path": date_path}) obj.date = now tender.revisions.append( type(tender).revisions.model_class({ 'author': request.authenticated_userid, 'changes': patch, 'rev': tender.rev })) old_dateModified = tender.dateModified if getattr(tender, 'modified', True): tender.dateModified = now try: tender.store(request.registry.db) except ModelValidationError, e: for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except ResourceConflict, e: # pragma: no cover request.errors.add('body', 'data', str(e)) request.errors.status = 409
def save_plan(request): plan = request.validated['plan'] patch = get_revision_changes(plan.serialize("plain"), request.validated['plan_src']) plan.revisions.append( Revision({ 'author': request.authenticated_userid, 'changes': patch, 'rev': plan.rev })) old_dateModified = plan.dateModified plan.dateModified = get_now() try: plan.store(request.registry.db_plan) except ModelValidationError, e: for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422
def save_plan(request): """ Save plan object to database :param request: :return: True if Ok """ plan = request.validated["plan"] patch = get_revision_changes(plan.serialize("plain"), request.validated["plan_src"]) if patch: plan.revisions.append(Revision({"author": request.authenticated_userid, "changes": patch, "rev": plan.rev})) old_date_modified = plan.dateModified plan.dateModified = get_now() try: plan.store(request.registry.db) except ModelValidationError, e: # pragma: no cover for i in e.message: request.errors.add("body", i, e.message[i]) request.errors.status = 422 except Exception, e: # pragma: no cover request.errors.add("body", "data", str(e))
def save_asset(request): asset = request.validated['asset'] if asset.mode == u'test': set_modetest_titles(asset) patch = get_revision_changes(asset.serialize("plain"), request.validated['asset_src']) if patch: revision = prepare_revision(asset, patch, request.authenticated_userid) asset.revisions.append(type(asset).revisions.model_class(revision)) old_dateModified = asset.dateModified if getattr(asset, 'modified', True): asset.dateModified = get_now() try: asset.store(request.registry.db) except ModelValidationError, e: for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except ResourceConflict, e: # pragma: no cover request.errors.add('body', 'data', str(e)) request.errors.status = 409
def save_monitor(request): monitor = request.validated['monitor'] patch = get_revision_changes(monitor.serialize("plain"), request.validated['monitor_src']) if patch: monitor.revisions.append( Revision({ 'author': request.authenticated_userid, 'changes': patch, 'rev': monitor.rev })) old_date_modified = monitor.dateModified monitor.dateModified = get_now() try: monitor.store(request.registry.db) except ModelValidationError, e: # pragma: no cover for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except Exception, e: # pragma: no cover request.errors.add('body', 'data', str(e))
def save_contract(request): """ Save contract object to database :param request: :return: True if Ok """ contract = request.validated['contract'] patch = get_revision_changes(contract.serialize("plain"), request.validated['contract_src']) if patch: contract.revisions.append( Revision({'author': request.authenticated_userid, 'changes': patch, 'rev': contract.rev})) old_date_modified = contract.dateModified contract.dateModified = get_now() try: contract.store(request.registry.db) except ModelValidationError, e: # pragma: no cover for i in e.message: request.errors.add('body', i, e.message[i]) request.errors.status = 422 except Exception, e: # pragma: no cover request.errors.add('body', 'data', str(e))
def save_object(request, obj_name, with_test_mode=True, additional_obj_names=""): obj = request.validated[obj_name] if with_test_mode and obj.mode == "test": set_modetest_titles(obj) patch = get_revision_changes(obj.serialize("plain"), request.validated["%s_src" % obj_name]) if patch: now = get_now() append_obj_revision(request, obj, patch, now) old_date_modified = obj.dateModified if getattr(obj, "modified", True): obj.dateModified = now for i in additional_obj_names: if i in request.validated: request.validated[i].dateModified = now with handle_store_exceptions(request): obj.store(request.registry.databases[f"{obj_name}s"] ) # TODO a better way to specify db name? LOGGER.info( "Saved {} {}: dateModified {} -> {}".format( obj_name, obj.id, old_date_modified and old_date_modified.isoformat(), obj.dateModified.isoformat()), extra=context_unpack( request, {"MESSAGE_ID": "save_{}".format(obj_name)}, {"RESULT": obj.rev}), ) return True