def edit_venue(venueId): venue = load_or_404(Venue, venueId) ensure_current_user_has_rights(app.model.RightsType.editor, venue.managingOffererId) update(venue, request.json) app.model.PcObject.check_and_save(venue) return jsonify(venue._asdict(include=VENUES_INCLUDES)), 200
def post_occasion(occasionType): model_name = inflect_engine.singular_noun(occasionType.title(), 1) # CREATE THE OCCASION (EVENT OR THING) occasion = app.model[model_name]() occasion_dict = request.json['occasion'] update(occasion, occasion_dict) app.model.PcObject.check_and_save(occasion) # DETERMINE OFFERER offerer = app.model.Offerer.query\ .filter_by(id=dehumanize(occasion_dict['offererId']))\ .first_or_404() # DETERMINE VENUE venue = app.model.Venue.query\ .filter_by(id=dehumanize(occasion_dict['venueId']))\ .first_or_404() # CREATE CORRESPONDING EVENT OCCURENCES event_occurences = request.json.get('eventOccurences') if event_occurences: for event_occurence_dict in event_occurences: create_event_occurence(event_occurence_dict, occasion, offerer, venue) return jsonify(occasion._asdict(include=OCCASION_INCLUDES)), 201
def patch_occasion(occasionType, occasionId): model_name = inflect_engine.singular_noun(occasionType.title(), 1) # UPDATE THE OCCASION occasion = app.model[model_name].query\ .filter_by(id=dehumanize(occasionId))\ .first_or_404() occasion_dict = request.json.get('occasion') if occasion_dict: update(occasion, occasion_dict) app.model.PcObject.check_and_save(occasion) first_occurence = occasion.occurences[0] venue = first_occurence.venue offerer = venue.managingOfferer # UPDATE CORRESPONDING EVENT OCCURENCES event_occurences = request.json.get('eventOccurences') if event_occurences: for event_occurence_dict in event_occurences: if event_occurence_dict.get('DELETE') == '_delete_': app.model.Offer\ .query\ .filter_by(eventOccurenceId=dehumanize(event_occurence_dict['id']))\ .delete() app.model.EventOccurence\ .query\ .filter_by(id=dehumanize(event_occurence_dict['id']))\ .delete() app.db.session.commit() else: create_event_occurence(event_occurence_dict, occasion, offerer, venue) return jsonify(occasion._asdict(include=OCCASION_INCLUDES)), 200
def patch_thing(id): thing = load_or_404(Thing, id) update(thing, request.json) app.model.PcObject.check_and_save(thing) return jsonify( thing._asdict(include=THING_INCLUDES, has_dehumanized_id=True, has_model_name=True)), 200
def post_thing(): thing = Thing() update(thing, request.json) app.model.PcObject.check_and_save(thing) return jsonify( thing._asdict(include=THING_INCLUDES, has_dehumanized_id=True, has_model_name=True)), 201
def edit_event_occurence(id): eo = load_or_404(EventOccurence, id) ensure_current_user_has_rights(app.model.RightsType.editor, eo.venue.managingOffererId) update(eo, request.json) #TODO: Si changement d'horaires et qu'il y a des réservations il faut envoyer des mails ! #TODO: Interdire la modification d'évenements passés app.model.PcObject.check_and_save(eo) return jsonify(eo._asdict(include=VENUES_INCLUDES)), 200
def patch_event(id): event = load_or_404(Event, id) update(event, request.json) app.model.PcObject.check_and_save(event) return jsonify( event._asdict( include=EVENT_INCLUDES, has_dehumanized_id=True, has_model_name=True ) ), 200
def create_event_occurence(json, occasion, offerer, venue): event_occurence = app.model.EventOccurence() event_occurence.event = occasion event_occurence.venue = venue update(event_occurence, json, **{"skipped_keys": ['offer']}) app.model.PcObject.check_and_save(event_occurence) offer = app.model.Offer() offer.eventOccurence = event_occurence offer.offerer = offerer update(offer, json['offer'][0]) app.model.PcObject.check_and_save(offer)
def edit_offer(offer_id): updated_offer_dict = request.json query = Offer.query.filter_by(id=dehumanize(offer_id)) offer = query.first_or_404() ensure_provider_can_update(offer) update(offer, updated_offer_dict) try: app.model.PcObject.check_and_save(offer) except InternalError as ie: if 'check_offer' in str(ie.orig): ae = ApiErrors() ae.addError( 'available', 'la quantité pour cette offre' + ' ne peut pas être inférieure' + ' au nombre de réservations existantes.') return jsonify(ae.errors), 400 else: raise ie return jsonify(offer._asdict(include=OFFERS_INCLUDES)), 200
def patch_offerer(offererId): offerer = app.model.Offerer\ .query.filter_by(id=dehumanize(offererId)) update(offerer, request.json) app.model.PcObject.check_and_save(offerer) return jsonify(offerer._asdict(include=OFFERERS_INCLUDES)), 200
def create_offerer(): offerer = app.model.Offerer() update(offerer, request.json) offerer.make_admin(current_user) app.model.PcObject.check_and_save(offerer) return jsonify(offerer._asdict(include=OFFERERS_INCLUDES)), 201
def edit_venue_provider(id): vp = load_or_404(VenueProvider, id) update(vp, request.json) app.model.PcObject.check_and_save(vp) return jsonify(vp._asdict()), 200
def signup(): if 'contact_ok' not in request.json or\ (request.json['contact_ok'] is not True and str(request.json['contact_ok']).lower() != 'true'): e = ApiErrors() e.addError('contact_ok', 'Vous devez obligatoirement cocher cette case') return jsonify(e.errors), 400 departement_code = None if 'email' in request.json: scope = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive' ] key_path = Path(path.dirname( path.realpath(__file__))) / '..' / 'private' / 'google_key.json' credentials = ServiceAccountCredentials.from_json_keyfile_name( key_path, scope) gc = gspread.authorize(credentials) spreadsheet = gc.open_by_key( '1YCLVZNU5Gzb2P4Jaf9OW50Oedm2-Z9S099FGitFG64s') worksheet = spreadsheet.worksheet('Utilisateurs') labels = worksheet.row_values(1) email_index = None departement_index = None for index, label in enumerate(labels): if label == 'Email': email_index = index elif label == 'Département': departement_index = index if email_index is None: raise ValueError("Can't find 'Email' column in users spreadsheet") if departement_index is None: raise ValueError( "Can't find 'Département' column in users spreadsheet") values = worksheet.get_all_values()[1:] authorized_emails = list(map(lambda v: v[email_index], values)) try: email_index = authorized_emails.index(request.json['email']) except ValueError: e = ApiErrors() e.addError('email', "Adresse non autorisée pour l'expérimentation") return jsonify(e.errors), 400 departement_code = values[email_index][departement_index] if departement_code.strip() == '': print( "[ERROR] Missing departement code in users spreadsheet for " + request.json['email']) e = ApiErrors() e.addError('email', "Adresse non autorisée pour l'expérimentation") return jsonify(e.errors), 400 new_user = app.model.User(from_dict=request.json) new_user.id = None new_user.departementCode = departement_code offerer = None user_offerer = None if 'siren' in request.json: #TODO: handle case of already existing Offerer new_user.canBook = False offerer = app.model.Offerer() update(offerer, request.json) user_offerer = offerer.make_admin(new_user) offerer.bookingEmail = new_user.email offerer.isActive = False app.model.PcObject.check_and_save(new_user, offerer, user_offerer) else: app.model.PcObject.check_and_save(new_user) login_user(new_user) return jsonify(new_user._asdict(include=USERS_INCLUDES)), 201
def patch_profile(): update(current_user, request.json) app.model.PcObject.check_and_save(current_user) return jsonify(current_user._asdict(include=USERS_INCLUDES)), 200