def newReport():
    # Verify Firebase auth.
    id_token = request.cookies.get("token")
    error_message = None
    claims = None
    theme_input = {}
    features = list(read_all_features(db))
    tags = list(get_all_tags(db))

    if id_token:
        try:
            # Verify the token against the Firebase Auth API. This example
            # verifies the token on each page load. For improved performance,
            # some applications may wish to cache results in an encrypted
            # session store (see for instance
            # http://flask.pocoo.org/docs/1.0/quickstart/#sessions).
            claims = google.oauth2.id_token.verify_firebase_token(
                id_token, firebase_request_adapter)

            feature_names = []
            # Get all themes/features in db
            mongo_all_themes = read_all_features(db)
            for t in mongo_all_themes:
                feature_names.append(t['feature_name'])
            theme_input['feature_names'] = feature_names    
        
        except ValueError as exc:
            # This will be raised if the token is expired or any other
            # verification checks fail.
            error_message = str(exc)

    return render_template(
        'new_report.html',
        features=features,
        tags=tags,
        user_data=claims,
        error_message=error_message,
        user_input=theme_input)
# [END gae_python37_datastore_render_user_times]
def getThemes():
    claims = None
    error_message = None
    theme_input = {}
    # Get all themes/features in db
    mongo_all_themes = read_all_features(db)

    feature_names = []
    feature_id = []
    feature_imgs = []

    themeJson = []

    for t in mongo_all_themes:
        feature_names.append(t['feature_name'])
        feature_id.append(t['_id'])
        report_imgs = find_photos_by_theme(db, t['feature_name'])
        report_img_id = []

        if report_imgs.count() != 0:
            report_img_id.append(report_imgs[0]['_id'])

        if report_imgs.count() == 0:
            themeJson.append({
                "feature_id": t['_id'],
                "feature_name": t['feature_name'],
                "feature_img_id": []
            })
        else:
            themeJson.append({
                "feature_id": t['_id'],
                "feature_name": t['feature_name'],
                "feature_img_id": report_img_id[0]
            })

        if report_imgs.count() != 0:
            feature_imgs.append(find_photo(db, report_img_id)[0])
        else:
            feature_imgs.append("0")
    theme_input['feature_names'] = feature_names
    theme_input['feature_id'] = feature_id
    theme_input['feature_imgs'] = feature_imgs

    if (request.path == '/json'):
        return dumps(themeJson)
    else:
        return render_template('theme_management.html',
                               user_data=claims,
                               error_message=error_message,
                               user_input=theme_input)
Example #3
0
def getPersonal():
    # Verify Firebase auth.
    id_token = request.cookies.get('token')
    error_message = None
    claims = None
    user_input = {}
    features = []

    if id_token:
        try:
            # Verify the token against the Firebase Auth API. This example
            # verifies the token on each page load. For improved performance,
            # some applications may wish to cache results in an encrypted
            # session store (see for instance
            # http://flask.pocoo.org/docs/1.0/quickstart/#sessions).
            claims = google.oauth2.id_token.verify_firebase_token(
                id_token, firebase_request_adapter)

            # Check user info in mongodb
            mongo_user_id = find_or_create_user(db, claims['name'],
                                                claims['email'])
            # Get report cursor
            mongo_reports = find_report(db, user_id=mongo_user_id)
            # mongo_reports = db.Reports.find({'user_id': mongo_user_id})
            reports_img = []
            reports_theme = []
            reports_date = []
            reports_loc = []
            reports_desc = []
            for r in mongo_reports:
                reports_theme.append(r['feature_name'])
                reports_date.append(r['date_in'])
                reports_loc.append(r['location'])
                reports_desc.append(r['description'])
                report_img = []
                if 'photos' in r and len(r['photos']) > 0:
                    report_img = find_photo(db, r['photos'])
                reports_img.append(report_img)
            user_input['reports_img'] = reports_img
            user_input['reports_theme'] = reports_theme
            user_input['reports_date'] = reports_date
            user_input['reports_loc'] = reports_loc
            user_input['reports_desc'] = reports_desc
            display_theme = find_userinfo_by_id(
                db, mongo_user_id)['subscribe_feature']
            display_theme_img = []
            # Randomly pick a cover photo for each theme
            for t in display_theme:
                t_reports = find_report(db, feature_name=t)
                for r in t_reports:
                    if 'photos' in r and len(r['photos']) > 0:
                        report_img = find_photo(db, r['photos'])
                        display_theme_img.append(random.choice(report_img))
                        break
            user_input['themes_name'] = display_theme
            user_input['themes_img'] = display_theme_img
            # All avaiable themes/features
            features = list(read_all_features(db))
        except ValueError as exc:
            # This will be raised if the token is expired or any other
            # verification checks fail.
            error_message = str(exc)

    return render_template('personal_management.html',
                           user_data=claims,
                           error_message=error_message,
                           user_input=user_input,
                           features=features)