def delete_event(event_id): # noqa: E501 """Delete event This can only be done by the logged in member. # noqa: E501 :param event_id: The name that needs to be deleted :type event_id: int :rtype: Success """ deleteEvent = db_session.query( dbModels.Event).filter(dbModels.Event.id == event_id).first() if deleteEvent is None: return connexion.problem(404, "Not Found", "event_id not found.", "Lookup") db_session.delete(deleteEvent) try: db_session.commit() except: db_session.rollback() raise return {"success": True, "Description": "Event has been deleted."}
def delete_member(username): # noqa: E501 """Delete member This can only be done by the logged in member. Attention! All information, events and pictures associated with this member will be deleted! # noqa: E501 :param username: The name that needs to be deleted :type username: str :rtype: Success """ deleteMember = db_session.query( dbModels.Member).filter(dbModels.Member.username == username).first() if deleteMember is None: return connexion.problem(404, "Not Found", "Username not found.", "Lookup") db_session.delete(deleteMember) try: db_session.commit() except: db_session.rollback() return connexion.problem(500, "Internal Server Error", "Database", "Database") return {"success": True, "Description": "Member has been deleted."}
def update_member(username, body): # noqa: E501 """Updated member This can only be done by the logged in member. # noqa: E501 :param username: name that need to be updated :type username: str :param body: Update member object :type body: dict | bytes :rtype: MemberPrivate """ if connexion.request.is_json: body = MemberUpdate.from_dict( connexion.request.get_json()) # noqa: E501 updateMember = db_session.query( dbModels.Member).filter(dbModels.Member.username == username).first() updateMember.update(body.name, body.password, body.name, body.email, body.description) db_session.add(updateMember) try: db_session.commit() except: db_session.rollback() # Bad Error Handling TODO return connexion.problem( 400, "Bad Request", "Username, Name, Passwd and Email are required.", "Parsing") return updateMember.get_privat(), 200
def create_event(body): # noqa: E501 """Create event This can only be done by the logged in member. # noqa: E501 :param body: Created event object :type body: dict | bytes :rtype: Event """ auth = flask.request.authorization if connexion.request.is_json: body = EventRequest.from_dict( connexion.request.get_json()) # noqa: E501 member_id = db_session.query(dbModels.Member.id).filter( dbModels.Member.username == auth.username).first()[0] # TODO validate event body function if not body.start_datetime or not body.end_datetime or not body.start_datetime <= body.end_datetime: return connexion.problem( 400, "Bad Request", "Date: Dates not valid; Is the start_date earlier than the end_date?", "Parsing") images = None if body.images: images = db_session.query(dbModels.Image).filter( dbModels.Image.uuid.in_(body.images)).all() newEvent = dbModels.Event(member_id, body.name, body.description, body.start_datetime, body.end_datetime, body.age_from, body.age_to, body.location, images) db_session.add(newEvent) try: db_session.commit() except: db_session.rollback() # Bad Error Handling TODO raise return connexion.problem(400, "Bad Request", "Event: model failed", "Parsing") return newEvent.to_dict(), 201
def update_event(event_id, body): # noqa: E501 """Updated event This can only be done by the logged in member. # noqa: E501 :param event_id: name that need to be updated :type event_id: int :param body: Updated event object :type body: dict | bytes :rtype: Event """ if connexion.request.is_json: body = EventRequest.from_dict( connexion.request.get_json()) # noqa: E501 putEvent = db_session.query( dbModels.Event).filter(dbModels.Event.id == event_id).first() if putEvent is None: return connexion.problem(404, "Not Found", "event_id not found.", "Lookup") # TODO validate event body function if not body.start_datetime or not body.end_datetime or not body.start_datetime <= body.end_datetime: return connexion.problem( 400, "Bad Request", "Date: Dates not valid; Is the start_date earlier than the end_date?", "Parsing") images = None if body.images: images = db_session.query(dbModels.Image).filter( dbModels.Image.uuid.in_(body.images)).all() putEvent.update(body.name, body.description, body.start_datetime, body.end_datetime, body.age_from, body.age_to, body.location, images) db_session.add(putEvent) try: db_session.commit() except: db_session.rollback() raise return putEvent.to_dict()
def image_upload_post(image, description=None): # noqa: E501 """Uploads a file. # noqa: E501 :param image: The file to upload. :type image: werkzeug.datastructures.FileStorage :param description: Description of the Image. :type description: str :rtype: Image """ # check file if image.content_type not in ALLOWED_MIME_TYPES: # 415 Unsupported Media Type - https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16 return connexion.problem( 415, "Unsupported Media Type", "Image: only files with mime-type " + str(ALLOWED_MIME_TYPES) + " are allowed", "Parsing") # File Handling unique_id = str(uuid.uuid1()) path = IMAGE_DATA_PATH + unique_id create_path(path) # Database auth = flask.request.authorization member_id = db_session.query(dbModels.Member.id).filter( dbModels.Member.username == auth.username).first()[0] newImage = dbModels.Image(member_id, unique_id, path, image.content_type, description) db_session.add(newImage) try: db_session.commit() except: db_session.rollback() # Error Handling TODO raise # Save file if database was successful image.save(path) return newImage.to_dict()
def add_pet(body): """ Add a new pet to the store :param body: Pet object that needs to be added to the store :type body: dict | bytes :rtype: None """ if connexion.request.is_json: body = Pet.from_dict(connexion.request.get_json()) ses.add(body) try: ses.commit() return body.to_str() except Exception as e: ses.rollback() return e
def create_member(body): # noqa: E501 """Create member This creates a new Member # noqa: E501 :param body: Created member object :type body: dict | bytes :rtype: MemberPrivate """ if connexion.request.is_json: body = MemberCreate.from_dict( connexion.request.get_json()) # noqa: E501 if not body.username or not re.match( r'^(?=.{4,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$', body.username): return connexion.problem(400, "Bad Request", "Username validation failed.", "Parsing") if db_session.query(dbModels.Member.id).filter_by( username=body.username).scalar() is not None: return connexion.problem(409, "Conflict", "Username already used", "IntegrityError") newMember = dbModels.Member(body.username, body.password, body.name, body.email, body.description) db_session.add(newMember) try: db_session.commit() except Exception as e: logging.error(e) db_session.rollback() if isinstance(e, sqlalchemy.exc.IntegrityError): return connexion.problem( 400, "Bad Request", "'username', 'name', 'password' and 'email' are required.", "Parsing") raise e return newMember.get_privat(), 201
def attend_event(event_id, state): # noqa: E501 """change attendence to a event This can only be done by the logged in member. # noqa: E501 :param event_id: The Event ID :type event_id: int :param state: The status :type state: bool :rtype: Success """ auth = flask.request.authorization member = db_session.query(dbModels.Member).filter( dbModels.Member.username == auth.username).first() event = db_session.query( dbModels.Event).filter(dbModels.Event.id == event_id).first() if state == True: event.participants.append(member) else: event.participants.remove(member) db_session.add(event) try: db_session.commit() except: db_session.rollback() # Bad Error Handling TODO raise return connexion.problem(400, "Bad Request", "Event: model failed", "Parsing") return event.to_dict()
def update_user(user, body): user.update(body) db_session.commit()
def delete_user(user): db_session.delete(user) db_session.commit()
def add_user(user_dict): db_session.add(user_dict) db_session.commit()