class OperatorFactory(factory.Factory): class Meta: model = Operator first_name = factory.Faker('first_name') last_name = factory.Faker('last_name') email = factory.Faker('email') password = PassHash.hash('123456')
def post(self): """create a new user""" new_Beneficiary_request = request.json # TODO: get authenticated operator and assignee to new Beneficiary_request # new_Beneficiary_request["created_by"] = authenticated_oprator try: assert len(new_Beneficiary_request["password"]) >= MIN_PASSWORD_LEN, f"Password is to short, min length is {MIN_PASSWORD_LEN}" new_Beneficiary_request["password"] = PassHash.hash(new_Beneficiary_request["password"]) comment = Beneficiary_request(**new_Beneficiary_request) comment.save() return jsonify({"response": "success"}) except Exception as error: return jsonify({"error": str(error)}), 400
def put(self, operator_id, delete=False): """update a single user by id""" update = {} if not delete: for key, value in request.json: if key == "password": value = PassHash.hash(value) update[f"set__{key}"] = value else: update["set__is_active"] = False try: Operator.objects(id=operator_id).get().update(**update) return jsonify({"response": "success"}) except Exception as error: return jsonify({"error": str(error)}), 400
def updateBeneficiary(requestjson, beneficiary_id, delete=False): """update a single user by id""" print(beneficiary_id, '---') update = {} if not delete: for key, value in requestjson.items(): if key == '_id': continue if key == "password": value = PassHash.hash(value) update[f"set__{key}"] = value else: update["set__is_active"] = False try: obj = Beneficiary.objects(id=beneficiary_id).get() data = obj.clean_data() #and not data['is_active'] if ('set__is_active' in update and update['set__is_active'] and not data['is_active']) \ or ('set__offer' in update and update['set__offer'] != data['offer']) \ or ('set__address' in update and update['set__address'] != data['address']): #change to active or different volunteer category or different address #return jsonify(telegrambot.send_request(obj)) telegrambot.send_request(obj) elif 'set__volunteer' in update and requestjson[ 'volunteer'] != '' and requestjson[ 'volunteer'] is not None and ( 'volunteer' not in data or update['set__volunteer'] != data['volunteer']): telegrambot.send_assign(beneficiary_id, requestjson['volunteer']) elif 'set__status' in update and update['set__status'].lower( ) == 'cancelled': #if the volunteer refused the request, delete the link volunteer_updates = { "push__offer_list": { "id": beneficiary_id, "offer": data['offer'], "cancelled": True } } volunteer.update_volunteer(requestjson['volunteer'], volunteer_updates) update['set__volunteer'] = '' update['set__status'] = update['set__status'].lower() obj.update(**update) return jsonify({"response": "success"}) except Exception as error: return jsonify({"error": str(error)}), 400
def registerOperator(requestjson, created_by): """create a new user""" new_operator = requestjson if len(created_by) > 30: user = Operator.verify_auth_token(created_by) created_by = user.get().clean_data()['email'] # TODO: get authenticated operator and assignee to new Operator # new_operator["created_by"] = authenticated_oprator try: assert len( new_operator["password"] ) >= MIN_PASSWORD_LEN, f"Password is to short, min length is {MIN_PASSWORD_LEN}" new_operator["password"] = PassHash.hash(new_operator["password"]) new_operator['created_by'] = created_by assert not Operator.objects( email=new_operator['email']), "user with this email already exists" comment = Operator(**new_operator) comment.save() return jsonify({"response": "success", 'user': comment.clean_data()}) except Exception as error: return jsonify({"error": str(error)}), 400
class BeneficiaryFactory(factory.Factory): class Meta: model = Beneficiary first_name = factory.Faker('first_name') last_name = factory.Faker('last_name') email = factory.Faker('email') password = PassHash.hash('123456') age = 20 address = 'Botanica' zone_address = 'Botanica' secret = 'G900' phone = 11112222 address = 'B' zone_address = 'A' age = '1' offer = '1' comments = '' urgent = False curator = False has_disabilities = False black_list = False
def updateVolunteer(requestjson, volunteer_id, delete=False): """update a single user by id""" print(volunteer_id, '---') update = {} if not delete: for key, value in requestjson.items(): if key == '_id': continue if key == "telegram_id": value = value.replace('+','').replace(' ','').strip() if len(value)==0: update['unset__telegram_chat_id'] = '' if key == "password": value = PassHash.hash(value) update[f"set__{key}"] = value else: update["set__is_active"] = False try: Volunteer.objects(id=volunteer_id).get().update(**update) return jsonify({"response": "success"}) except Exception as error: return jsonify({"error": str(error)}), 400
def register_volunteer(request_json, created_by): """Creates and persists a new volunteer into database. Parameters ---------- request_json : dict A dictionary representing the volunteer profile details. created_by : str A string representing either name of user who is going to create a new volunteer, or the token Returns ------- 200: If the volunteer was successful created and saved. 400: If the volunteer wasn't created or saved, and there was raised some exception. """ log.debug("Relay offer for req:%s from ", request_json) try: if not vu.is_email(created_by): user = Operator.verify_auth_token(created_by) created_by = user.get().clean_data()['email'] if created_by == os.getenv("COVID_BACKEND_USER"): vu.exists_by_telegram_chat_id(request_json["chat_id"]) new_volunteer_data = process_volunteer_data_from_telegram(request_json) else: vu.exists_by_email(request_json["email"]) new_volunteer_data = request_json new_volunteer_data["password"] = PassHash.hash(new_volunteer_data["password"]) new_volunteer_data['created_by'] = created_by new_volunteer = Volunteer(**new_volunteer_data) new_volunteer.save() return jsonify({"response": "success", 'user': new_volunteer.clean_data()}) except Exception as error: return jsonify({"error": str(error)}), 400
def registerBeneficiary(requestjson, created_by, fixer_id): """create a new user""" new_beneficiary = requestjson if len(created_by) > 30: user = Operator.verify_auth_token(created_by) created_by = user.get().clean_data()['email'] try: new_beneficiary['password'] = '******' new_beneficiary['email'] = '*****@*****.**' assert len( new_beneficiary["password"] ) >= MIN_PASSWORD_LEN, f"Password is to short, min length is {MIN_PASSWORD_LEN}" new_beneficiary["password"] = PassHash.hash( new_beneficiary["password"]) new_beneficiary['created_by'] = created_by new_beneficiary['fixer'] = str(fixer_id) comment = Beneficiary(**new_beneficiary) comment.save() if 'is_active' in new_beneficiary and new_beneficiary['is_active']: #sent the request to the volunteer via telegram telegrambot.send_request(comment) return jsonify({"response": "success", 'user': comment.clean_data()}) except Exception as error: return jsonify({"error": str(error)}), 400