Exemple #1
0
def process_image(request_files, input_name):
    if input_name not in request_files:
        return False

    input_file = request_files[input_name]

    if input_file:
        is_image_valid = input_file and input_file.filename != '' and allowed_photo(
            input_file.filename)
        if not is_image_valid:
            raise
        else:
            icon_res = uploadFile(input_file)
            image_dict = {
                "image_id": icon_res["upload_result"]["public_id"],
                "image_link": icon_res["upload_result"]["secure_url"]
            }
            return image_dict
    else:
        return False
Exemple #2
0
def update_background(request, sse):
    try:
        if 'background_photo' not in request.files:
            return jsonify(
                error=True,
                message=
                'file with filename "background_photo" was not found in request'
            )

        file = request.files['background_photo']
        if file and file.filename != '' and chamber.allowed_photo(
                file.filename):
            prev_ref = request.form['prev_ref']

            link = chamber.uploadFile(file=file, prev_ref=prev_ref)

            you = db_session.query(Accounts).filter_by(
                id=user_session['account_id']).one()
            you.background = link

            db_session.add(you)
            db_session.commit()

            return jsonify(account=you.serialize,
                           message='Background Updated Successfully!',
                           background=link)

        else:
            return jsonify(
                error=True,
                message=
                'file with filename "background_photo" was not of type: image')

    except Exception as err:
        print(err)
        return jsonify(error=True,
                       errorMessage=str(err),
                       message='error processing...')
Exemple #3
0
def update_event(request, sse, event_id):
    try:
        if not request.form:
            return jsonify(error=True, message='no request form was sent')

        # print('update event form ------- ', request.form)
        # print('event_id --- ', event_id)

        event = db_session.query(Events).filter_by(id=event_id).first()
        if event == None:
            message = '''event not found.
            either id is incorrect or current user does not own it'''
            return jsonify(error=True, message=message)

        if event.host_id != user_session['account_id']:
            message = '''Current account does not own this event'''
            return jsonify(error=True, message=message)

        title = str(request.form['title']).encode()
        desc = str(request.form['desc']).encode()
        categories = str(request.form['categories']).encode()
        location = str(request.form['location']).encode()
        link = str(request.form['link']).encode()
        date_str = str(request.form['date_str']).encode()
        date_concat = str(request.form['date_concat']).encode()
        event_date = datetime.strptime(date_str, '%Y-%m-%d')
        event_date_time = datetime.strptime(date_concat, '%Y-%m-%d %H:%M:%S')

        event.title = title
        event.desc = desc
        event.categories = categories
        event.location = location
        event.link = link
        event.event_date = event_date
        event.event_date_time = event_date_time

        if 'event_photo' not in request.files:
            db_session.add(event)
            db_session.commit()
            return jsonify(message='Event Updated Successfully!',
                           event=event.serialize)

        else:
            file = request.files['event_photo']
            if file and file.filename != '' and chamber.allowed_photo(
                    file.filename):
                icon = chamber.uploadFile(file=file, prev_ref=prev_ref)
                event.icon = icon
                db_session.add(event)
                db_session.commit()
                return jsonify(message='Event Updated Successfully!',
                               event=event.serialize)

            else:
                return jsonify(error=True,
                               message='file was not of type: image')

    except Exception as err:
        print(err)
        return jsonify(error=True,
                       errorMessage=str(err),
                       message='error processing...')
Exemple #4
0
def create_event(request, sse):
    try:
        if logged_in() == False:
            return jsonify(error=True,
                           message='no session found with this request.')

        if not request.form:
            return jsonify(error=True, message='no request form was sent')

        print(request.form)

        you = db_session.query(Accounts).filter_by(
            id=user_session['account_id']).one()
        if you.type != "VENUE":
            return jsonify(error=True, message='current user is not a VENUE')

        title = str(request.form['title']).encode()
        desc = str(request.form['desc']).encode()
        categories = str(request.form['categories']).encode()
        location = str(request.form['location']).encode()
        link = str(request.form['link']).encode()
        date_str = str(request.form['date_str']).encode()
        date_concat = str(request.form['date_concat']).encode()
        event_date = datetime.strptime(date_str, '%Y-%m-%d')
        event_date_time = datetime.strptime(date_concat, '%Y-%m-%d %H:%M:%S')

        new_event = Events(title=title,
                           desc=desc,
                           categories=categories,
                           location=location,
                           link=link,
                           event_date=event_date,
                           event_date_time=event_date_time,
                           host_id=you.id)

        message = you.username + ' created an event: ' + new_event.title

        if 'event_photo' not in request.files:
            db_session.add(new_event)
            db_session.commit()

            sse.publish({
                "message": message,
                "account_id": you.id
            },
                        type='notify')

            return jsonify(message='Event Created Successfully!',
                           event=new_event.serialize_small)

        else:
            file = request.files['event_photo']
            if file and file.filename != '' and chamber.allowed_photo(
                    file.filename):
                icon = chamber.uploadFile(file=file, prev_ref='')

                new_event.icon = icon

                db_session.add(new_event)
                db_session.commit()

                sse.publish({
                    "message": message,
                    "account_id": you.id
                },
                            type='notify')

                return jsonify(message='Event Created Successfully!',
                               event=new_event.serialize_small)

            else:
                return jsonify(error=True,
                               message='file was not of type: image')

    except Exception as err:
        print(err)
        return jsonify(error=True,
                       errorMessage=str(err),
                       message='error processing...')
Exemple #5
0
def update_account():
    if 'displayname' not in request.form:
        return jsonify(error=True, message='displayname is required')

    if 'username' not in request.form:
        return jsonify(error=True, message='username is required')

    if 'email' not in request.form:
        return jsonify(error=True, message='email is required')

    form_dict = {
        "displayname": str(request.form['displayname']).encode('utf-8'),
        "username": str(request.form['username']).encode('utf-8'),
        "email": str(request.form['email']).encode('utf-8'),
    }

    if 'weblink' in request.form:
        form_dict['weblink'] = str(request.form['weblink']).encode('utf-8')

    if 'bio' in request.form:
        form_dict['bio'] = str(request.form['bio']).encode('utf-8')

    if form_dict['bio']:
        if len(form_dict['bio']) > 100:
            return jsonify(error=True,
                           message='bio input max of 100 characters exceeded')

    if 'tags' in request.form:
        form_dict['tags'] = str(request.form['tags']).encode('utf-8')

    if form_dict['tags']:
        if len(form_dict['tags']) > 100:
            return jsonify(error=True,
                           message='tags input max of 100 characters exceeded')

    if 'visibility' in request.form:
        if request.form['visibility'] == 'private':
            form_dict['visibility'] = True
        else:
            form_dict['visibility'] = False
    else:
        form_dict['visibility'] = False

    if 'password' in request.form:
        if 'old_password' not in request.form:
            return jsonify(error=True,
                           message='old_password input is required')
        if 'password_confirm' not in request.form:
            return jsonify(error=True,
                           message='password_confirm input is required')
        old_password = str(request.form['old_password']).encode('utf-8')
        password = str(request.form['password']).encode('utf-8')
        password_confirm = str(
            request.form['password_confirm']).encode('utf-8')
        if not password:
            if old_password or password_confirm:
                return jsonify(
                    error=True,
                    message=
                    'all password inputs are required when changing password')
        if password:
            if not old_password:
                return jsonify(error=True,
                               message='old_password input is empty/invalid')
            check_user = db_select_queries.check_user_by_email(
                user_session['you']['email'])
            pw_check = check_password(old_password, check_user['password'])
            if not pw_check:
                return jsonify(error=True, message='old password is invalid')
            passwords_do_not_match = password != password_confirm
            if passwords_do_not_match:
                return jsonify(error=True,
                               message='new passwords do not match')
            form_dict['password'] = password

    email_changed = form_dict[
        'email'] and form_dict['email'] != user_session['you']['email']
    if email_changed:
        check_email = db_select_queries.check_user_by_email(form_dict['email'])
        if check_email:
            return jsonify(error=True, message='email already in use')
    else:
        del form_dict['email']

    username_changed = form_dict['username'] and form_dict[
        'username'] != user_session['you']['username']
    if username_changed:
        check_username = db_select_queries.check_user_by_username(
            form_dict['username'])
        if check_username:
            return jsonify(error=True, message='username already in use')
    else:
        del form_dict['username']

    profile_icon_file = request.files['profile-icon']
    if profile_icon_file:
        is_icon_valid = profile_icon_file and profile_icon_file.filename != '' and allowed_photo(
            profile_icon_file.filename)
        if not is_icon_valid:
            return jsonify(error=True, message='file not valid')
        else:
            icon_res = uploadFile(profile_icon_file)
            print('icon_res', icon_res)
            form_dict['icon_id_old'] = user_session['you']['icon_id']
            form_dict['icon_id'] = icon_res["upload_result"]["public_id"]
            form_dict['icon_link'] = icon_res["upload_result"]["secure_url"]

    profile_wallpaper_file = request.files['profile-wallpaper']
    if profile_wallpaper_file:
        is_wallpaper_valid = profile_wallpaper_file and profile_wallpaper_file.filename != '' and allowed_photo(
            profile_wallpaper_file.filename)
        if not is_wallpaper_valid:
            return jsonify(error=True, message='file not valid')
        else:
            wallpaper_res = uploadFile(profile_wallpaper_file)
            print('wallpaper_res', wallpaper_res)
            form_dict['wallpaper_id_old'] = user_session['you']['wallpaper_id']
            form_dict['wallpaper_id'] = wallpaper_res["upload_result"][
                "public_id"]
            form_dict['wallpaper_link'] = wallpaper_res["upload_result"][
                "secure_url"]

    db_update_queries.update_account(form_dict, user_session['you'])
    user_updated = db_select_queries.get_user_by_id(user_session['you_id'])
    user_session['you_username'] = user_updated['username']
    user_session['you'] = user_updated
    return jsonify(message='Updated Successfully!', user=user_updated)
Exemple #6
0
def create_story():
    if 'title' not in request.form:
        return jsonify(error=True, message='title is required')

    if 'body' not in request.form:
        return jsonify(error=True, message='body is required')

    if not request.form['title']:
        return jsonify(error=True, message='title is required')

    if not request.form['body']:
        return jsonify(error=True, message='body is required')

    try:
        title = request.form['title'].decode()
        body = request.form['body'].decode()
    except Exception as e:
        print(e)
        raise e
        return

    form_dict = {
        "title": title,
        "body": body,
    }

    if 'tags' in request.form:
        form_dict['tags'] = str(request.form['tags']).encode('utf-8')
        if form_dict['tags']:
            if len(form_dict['tags']) > 100:
                return jsonify(
                    error=True,
                    message='tags input max of 100 characters exceeded')
    else:
        form_dict['tags'] = ''

    if 'explicit' in request.form:
        if request.form['explicit'] == 'yes':
            form_dict['is_explicit'] = True
        else:
            form_dict['is_explicit'] = False
    else:
        form_dict['is_explicit'] = False

    if 'visibility' in request.form:
        if request.form['visibility'] == 'private':
            form_dict['is_private'] = True
        else:
            form_dict['is_private'] = False
    else:
        form_dict['is_private'] = False

    story_icon_file = request.files['story-icon']
    if story_icon_file:
        is_icon_valid = story_icon_file and story_icon_file.filename != '' and allowed_photo(
            story_icon_file.filename)
        if not is_icon_valid:
            return jsonify(error=True, message='file not valid')
        else:
            icon_res = uploadFile(story_icon_file)
            print('icon_res', icon_res)
            form_dict['image_id'] = icon_res["upload_result"]["public_id"]
            form_dict['image_link'] = icon_res["upload_result"]["secure_url"]
    else:
        form_dict['image_id'] = ''
        form_dict['image_link'] = ''

    new_story_id = db_insert_queries.create_new_story(form_dict,
                                                      user_session['you'])
    story = db_select_queries.get_story_full_by_id(new_story_id)
    return jsonify(message='Story Created Successfully!', story=story)