Пример #1
0
def add_favorite_clubs():
    """
    POST endpoint that adds favorite club(s) for student user. Ordering is preserved
    based on *when* they favorited.
    """

    user = get_current_user()
    json = g.clean_json

    new_fav_clubs_query = NewOfficerUser.objects \
        .filter(confirmed=True) \
        .filter(club__link_name__in=json['clubs']) \
        .only('club.link_name')

    potential_clubs = [
        club['club']['link_name']
        for club in query_to_objects(new_fav_clubs_query)
    ]

    for club in potential_clubs:
        if club not in user.favorited_clubs:
            user.favorited_clubs += [club]

    user.save()

    return jsonify(_fetch_user_profile(user)['favorited_clubs'])
Пример #2
0
def _fetch_user_profile(user):
    """
    Helper function to fetch the user's profile as a dictionary ready to be sent as JSON.
    """

    user_obj = query_to_objects_full(user)

    full_fav_clubs_query = NewOfficerUser.objects \
        .filter(club__link_name__in=user.favorited_clubs) \
        .only('club.name', 'club.link_name', 'club.events', 'club.recruiting_events')

    full_fav_clubs = [
        obj['club'] for obj in query_to_objects(full_fav_clubs_query)
    ]

    full_club_board = {}

    for key in user.club_board:
        club_query = NewOfficerUser.objects \
            .filter(club__link_name__in=user.club_board[key]) \
            .only('club.name', 'club.link_name', 'club.logo_url', 'club.events', 'club.recruiting_events')

        column_clubs = [obj['club'] for obj in query_to_objects(club_query)]
        full_club_board[key] = column_clubs

    if CurrentConfig.DEBUG:
        recommended_clubs = _random_smart_club_recommendations(3)
    else:
        # TODO: Implement "smart" club recommendations.
        recommended_clubs = _random_smart_club_recommendations(3)

    return {
        'full_name': user.full_name,
        'email': user.email,
        'majors': user_obj['majors'],
        'minors': user_obj['minors'],
        'interests': user_obj['interests'],
        'favorited_clubs': full_fav_clubs,
        'club_board': full_club_board,
        'recommended_clubs': recommended_clubs,
    }
Пример #3
0
def fetch_profile():
    """
    GET endpoint that fetches the complete club profile from an officer user.
    """

    user = get_current_user()

    club_obj = query_to_objects(user.club)
    club_obj['owner'] = user.email
    club_obj['confirmed'] = user.confirmed

    return club_obj
Пример #4
0
def get_organizations():
    """
    GET endpoint that fetches the list of organizations without filters, sorted alphabetically.
    """

    limit = to_int_safe(request.args.get('limit'), 50)
    skip = to_int_safe(request.args.get('skip'), 0)

    query = NewOfficerUser.objects \
        .filter(confirmed=True) \
        .filter(club__reactivated=True) \
        .only(*CATALOG_VIEW_FIELDS) \
        .order_by('club.name') \
        .limit(limit) \
        .skip(skip)

    results = [obj['club'] for obj in query_to_objects(query)]

    return {'results': results, 'num_results': query.count()}
Пример #5
0
def _fetch_clubs():
    """
    Utility function to fetch the entire list of clubs by name, email, confirmation
    and reactivation status.
    """
    club_list_query = NewOfficerUser.objects.scalar('club.name', 'email',
                                                    'confirmed',
                                                    'club.reactivated')
    raw_club_list = query_to_objects(club_list_query)
    club_list = []

    for club in raw_club_list:
        del club['id']
        del club['_cls']
        club_list.append({
            'name': club['club']['name'],
            'email': club['email'],
            'confirmed': club['confirmed'],
            'reactivated': club['club']['reactivated'],
        })

    return club_list
Пример #6
0
def get_org_by_id(org_link_name):
    """
    GET endpoint that fetches all information of the requested club organization, including
    its similarly recommended clubs.
    """

    user = NewOfficerUser.objects(
        club__link_name=org_link_name,
        confirmed=True,
        club__reactivated=True,
    ).first()

    if user is None:
        raise JsonError(status='error',
                        reason='The requested club does not exist!',
                        status_=404)

    club_obj = query_to_objects(user.club)

    for event in club_obj['events']:
        del event['_cls']

    current_user = get_current_user()

    if current_user and current_user.role == 'student':
        # Save the club as part of the student's visiting history of clubs
        current_user.visited_clubs += [org_link_name]
        current_user.save()

    if CurrentConfig.DEBUG:
        club_obj['recommended_clubs'] = _random_generic_club_recommendations(3)
    else:
        recommended_club_link_names = flask_exts.club_recommender.recommend(
            org_link_name)
        club_obj['recommended_clubs'] = _fetch_recommended_club_info(
            recommended_club_link_names)

    return club_obj
Пример #7
0
def get_num_user_tags():
    """
    GET endpoint that fetches the set of "# of users" tags.
    """

    return query_to_objects(NumUsersTag.objects.all())
Пример #8
0
def get_tags():
    """
    GET endpoint that fetches the set of club tags.
    """

    return query_to_objects(Tag.objects.all())
Пример #9
0
from slugify import slugify
from models.officer import Question

from utils import try_parsing_datetime, random_slugify, pst_right_now

from init_app import flask_exts
from flask import Blueprint, request, g
from flask_json import as_json, JsonError
from flask_utils import validate_json, query_to_objects, role_required
from flask_jwt_extended import jwt_required, get_current_user

from models import *

admin_blueprint = Blueprint('admin', __name__, url_prefix='/api/admin')

_fetch_resources_list = lambda user: [query_to_objects(res) for res in user.club.resources]
_fetch_event_list = lambda user: [query_to_objects(event) for event in user.club.events]
_fetch_recruiting_events_list = lambda user: [query_to_objects(r_event) for r_event in user.club.recruiting_events]
_fetch_gallery_media_list = lambda user: [query_to_objects(gallery_media) for gallery_media in user.club.gallery_media]
_fetch_question_list = lambda user: [query_to_objects(question) for question in user.club.faq]


@admin_blueprint.route('/profile', methods=['GET'])
@jwt_required
@role_required(roles=['officer'])
def fetch_profile():
    """
    GET endpoint that fetches the complete club profile from an officer user.
    """

    user = get_current_user()
Пример #10
0
def get_student_years():
    """
    GET endpoint that fetches the set of student years.
    """

    return query_to_objects(StudentYear.objects.all())
Пример #11
0
def get_minors():
    """
    GET endpoint that fetches the set of minors in UC Berkeley.
    """

    return query_to_objects(Minor.objects.all())
Пример #12
0
from flask_jwt_extended import jwt_required, get_current_user

from models import *

from flask_jwt_extended import (jwt_required, jwt_refresh_token_required,
                                create_access_token, create_refresh_token,
                                get_raw_jwt, get_jti, get_current_user)

from authomatic.extras.flask import FlaskAuthomatic
from authomatic.providers import oauth2

from app_config import CurrentConfig

PSEUDO_PASSWORD_PREFIX = 'UNUSABLE_PASSWORD'
_fetch_fav_clubs_list = lambda user: [
    query_to_objects(club) for club in user.favorited_clubs
]

student_blueprint = Blueprint('student', __name__, url_prefix='/api/student')


def _random_smart_club_recommendations(size):
    """
    Generates random "smart" clubs recommendations, intended for testing purposes only.
    """

    random_recommended_users = NewOfficerUser.objects \
        .filter(confirmed=True) \
        .filter(club__reactivated=True) \
        .aggregate([{ '$sample': {'size': size} }])