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 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 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."}
예제 #6
0
def check_auth(username: str, password: str):
    '''This function is called to check if a username /
    password combination is valid.'''

    getMember = db_session.query(Member).filter(
        Member.username == username).first()
    if getMember is None:
        return False

    return getMember.compare_member_passwd(password)
예제 #7
0
def get_pet_by_id(petId):
    """
    Find pet by ID
    Returns a single pet
    :param petId: ID of pet to return
    :type petId: int

    :rtype: Pet
    """
    return ses.query(Pet).get(petId)
def get_event_list(start_id=None,
                   end_id=None,
                   start_date=None,
                   end_date=None,
                   max_items=None):  # noqa: E501
    """Get eventList

    Returns a list of reduced Event objects # noqa: E501

    :param start_id: Member_id the list starts with
    :type start_id: int
    :param end_id: Member_id the list ends with
    :type end_id: int
    :param start_date: Member_Date the list starts with
    :type start_date: str
    :param end_date: Member_Date the list ends with
    :type end_date: str
    :param max_items: max items count in the list
    :type max_items: int

    :rtype: None
    """

    if not max_items:
        max_items = 100

    evalBuilder = EvalBuilder()

    if start_id:
        evalBuilder.append('start_id <= dbModels.Event.id')
    if end_id:
        evalBuilder.append('dbModels.Event.id <= end_id')
    if start_date:
        start_date = util.deserialize_datetime(start_date)
        evalBuilder.append('start_date <= dbModels.Event.start_datetime')
    if end_date:
        end_date = util.deserialize_datetime(end_date)
        evalBuilder.append('dbModels.Event.end_datetime <= end_date')

    events = db_session.query(dbModels.Event).filter(
        eval(evalBuilder.getEvalStr())).order_by(
            dbModels.Event.start_datetime).limit(max_items).all()

    event_list = []
    for event in events:
        event_list.append(event.to_dict_list())

    if start_date:
        event_list = sorted(event_list, key=lambda k: k['start_datetime'])

    return {"event_list": event_list}
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 get_member_list():  # noqa: E501
    """Get memberList

    Returns a list of reduced Member objects # noqa: E501


    :rtype: None
    """
    members = db_session.query(dbModels.Member).order_by(dbModels.Member.name)

    member_list = []
    for member in members:
        member_list.append(member.to_dict_list())

    return {"member_list": member_list}
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 get_member_by_name_private(username):  # noqa: E501
    """Get private member information by member name

    This can only be done by the logged in member. # noqa: E501

    :param username: The name that needs to be fetched.
    :type username: str

    :rtype: MemberPrivate
    """
    getMember = db_session.query(
        dbModels.Member).filter(dbModels.Member.username == username).first()

    if getMember is None:
        return connexion.problem(404, "Not Found", "Username not found.",
                                 "Lookup")

    return getMember.get_privat()
def image_uuid_get(uuid):  # noqa: E501
    """Returns a image.

     # noqa: E501

    :param uuid: Returns an Image
    :type uuid: str

    :rtype: None
    """

    mimetype_query = db_session.query(
        dbModels.Image.mimetype).filter(dbModels.Image.uuid == uuid).first()

    if not mimetype_query:
        return connexion.problem(404, "Not Found", "uuid not found.", "Lookup")

    return flask.send_file(IMAGE_DATA_PATH + uuid, mimetype=mimetype_query[0])
def get_member_by_name(username):  # noqa: E501
    """Get public member information by member name

    Returns public Member information # noqa: E501

    :param username: The name that needs to be fetched.
    :type username: str

    :rtype: MemberPublic
    """

    getMember = db_session.query(
        dbModels.Member).filter(dbModels.Member.username == username).first()

    if getMember is None:
        return connexion.problem(404, "Not Found", "Username not found.",
                                 "Lookup")

    return getMember.get_public()
def get_event_by_id(event_id):  # noqa: E501
    """Get event by event_id

    Returns a full Event object # noqa: E501

    :param event_id: The name that needs to be fetched.
    :type event_id: int

    :rtype: Event
    """

    getEvent = db_session.query(
        dbModels.Event).filter(dbModels.Event.id == event_id).first()

    if getEvent is None:
        return connexion.problem(404, "Not Found", "event_id not found.",
                                 "Lookup")

    return getEvent.to_dict()
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
예제 #17
0
def check_owner(event_id, username):
    event = db_session.query(Event).filter(Event.id == event_id).first()
    if event is None:
        return False
    return event.host_member.username == username