def post(siret, date_maj): """ Create a siren institution based on the sent information """ existing_institution = SireneInstitutionRepository.get(siret=siret) if existing_institution: return bad_request('duplicate value for uai_number') institution = SireneInstitutionRepository.create(siret=siret, date_maj=date_maj) if institution: return jsonify({'institution': institution.to_dict()}) return bad_request('unable to create the institution')
def post(last_name, first_name, age): """ Create an user based on the sent information """ existing_user = UserRepository.get(last_name=last_name, first_name=first_name) if existing_user: return bad_request('user already in database') user = UserRepository.create(last_name=last_name, first_name=first_name, age=age) if user: return jsonify({'user': user.json}) return bad_request('unable to create user')
def post(uai, is_institution): """ Create an institution based on the sent information """ existing_institution = BceInstitutionRepository.get(uai=uai) if existing_institution: return bad_request('duplicate value for uai number') if not (is_institution == 'False' or is_institution == 'True'): return bad_request('is_institution must be a boolean') institution = BceInstitutionRepository.create( uai=uai, is_institution=is_institution, ) if institution: return jsonify({'institution': institution.json}) return bad_request('unable to create the institution')
def put(uai, is_institution, id_esr): """ Update an user based on the sent information """ if not (is_institution == 'False' or is_institution == 'True'): return bad_request('is_institution must be a boolean') if is_institution == 'False': response = delete_institution(id_esr, request.headers['Authorization']) if not response == 200: return bad_request('unable to delete id_esr') repository = BceInstitutionRepository() institution = repository.update( uai=uai, is_institution=is_institution, ) if institution: return jsonify({'institution': institution.json}) return jsonify({'message': 'institution deleted'})
def put(last_name, first_name, age): """ Update an user based on the sent information """ repository = UserRepository() user = repository.update(last_name=last_name, first_name=first_name, age=age) if user: return jsonify({'user': user.json}) return bad_request('unable to update user')
def add_rating(request): if request.method == "GET": return util.bad_request("GET not allowed") user_profile = util.auth_user(request) if not user_profile: return util.auth_failed() try: u_id, rating = util.get_post_args(request, ["uid", "rating"]) rating = int(rating) if rating < 0 or rating > 5: return util.bad_request("Invalid rating") except KeyError: return util.bad_request("Invalid args") except TypeError: return util.bad_request("Invalid integer") try: bathroom = Bathroom.objects.get(uid=u_id) except ObjectDoesNotExist as e: return util.bad_request("bathroom not found") if len(user_profile.ratings.filter(uid=u_id)) > 0: response_data = { 'error': "none", 'id': bathroom.uid, 'fn': "already rated", 'current rating': str(bathroom.rating) } return HttpResponse(json.dumps(response_data), content_type="application/json", status=200) else: user_profile.ratings.add(bathroom) bathroom.num_ratings += 1 bathroom.total_rating += int(rating) bathroom.rating = bathroom.total_rating / bathroom.num_ratings bathroom.save() response_data = { 'error': "none", 'id': bathroom.uid, 'fn': "rated", 'current rating': bathroom.rating } return HttpResponse(json.dumps(response_data), content_type="application/json", status=200)
def check_in(request): if request.method == "GET": return util.bad_request("GET not allowed") user_profile = util.auth_user(request) if not user_profile: return util.auth_failed() try: u_id = util.get_post_args(request, ["uid"]) except KeyError: return util.bad_request("Invalid args") try: bathroom = Bathroom.objects.get(uid=u_id) except ObjectDoesNotExist: return util.bad_request("bathroom not found") if user_profile.check_ins.filter(uid=u_id) > 0: return util.bad_request("already checked in before") else: user_profile.check_ins.add(bathroom) bathroom.num_visitors += 1
def get_nearby_bathrooms(request): if request.method == "GET": return util.bad_request("GET not allowed") user_profile = util.auth_user(request) if not user_profile: return util.auth_failed() try: lat, lon, has_twoply = util.get_post_args(request, ["lat", "lon", "has_twoply"]) lat, lon = float(lat), float(lon) except KeyError: return util.bad_request("Invalid args") except TypeError: return util.bad_request("Integer error") if has_twoply: rooms = Bathroom.objects.filter(has_twoply=True) else: rooms = Bathroom.objects.all() nearby = [] [ nearby.append(room) for room in rooms if util.is_nearby((lat, lon), (room.lat, room.lon)) ] output = [] for room in nearby: thesaurus = { 'name': room.name, 'uid': room.uid, 'rating': str(room.rating), 'num_visitors': str(room.num_visitors), 'num_hearts': str(room.num_hearts), 'has_twoply': room.has_twoply } output.append(thesaurus) response_data = {'rooms': output} return HttpResponse(json.dumps(response_data), content_type="application/json", status=200)
def heart_bathroom(request): if request.method == "GET": return util.bad_request("GET not allowed") user_profile = util.auth_user(request) if not user_profile: return util.auth_failed() try: u_id = util.get_post_args(request, ["uid"]) except KeyError: return util.bad_request("Invalid args") try: bathroom = Bathroom.objects.get(uid=u_id) except ObjectDoesNotExist: return util.bad_request("bathroom not found") if len(user_profile.hearts.filter(uid=u_id)) > 0: bathroom.num_hearts -= 1 bathroom.save() user_profile.hearts.remove(bathroom) response_data = { 'error': "none", 'id': bathroom.uid, 'fn': "downvoted", 'hearts': bathroom.num_hearts } return HttpResponse(json.dumps(response_data), content_type="application/json", status=200) else: bathroom.num_hearts += 1 bathroom.save() user_profile.hearts.add(bathroom) response_data = { 'error': "none", 'id': bathroom.uid, 'fn': "upvoted", 'hearts': bathroom.num_hearts } return HttpResponse(json.dumps(response_data), content_type="application/json", status=200)
def new_bathroom(request): if request.method == "GET": return util.bad_request("GET not allowed") user_profile = util.auth_user(request) if not user_profile: return util.auth_failed() try: lat, lon, name = util.get_post_args(request, ("lat", "lon", "name")) has_twoply = request.POST.get("has_twoply", False) except KeyError: return util.bad_request("invalid args") if not user_profile: return util.auth_failed() bathroom = Bathroom(name=name, lat=lat, lon=lon, has_twoply=has_twoply, uid=str(uuid.uuid4())) bathroom.save() response_data = {'error': "none", 'id': bathroom.uid} return HttpResponse(json.dumps(response_data), content_type="application/json", status=200)
def get(siret): """ Return a siren institution key information based on its siret """ institution = SireneInstitutionRepository.get(siret=siret) if institution: return jsonify({'institution': institution.to_dict()}) return bad_request('sirene institution not found in database')
def get(uai): """ Return an institution key information based on its uai number """ institution = BceInstitutionRepository.get(uai=uai) if institution: return jsonify({'institution': institution.json}) return bad_request('institution not found in database')
def get(last_name, first_name): """ Return an user key information based on his name """ user = UserRepository.get(last_name=last_name, first_name=first_name) if user: return jsonify({'user': user.json}) return bad_request('user not found')