Ejemplo n.º 1
0
def get_activities(student):
    event = EventsFinder.get_default_event()
    date = request.args.get('date', None)
    if date is None:
        activities = event.activities
    else:
        activities = ActivitiesFinder.get_from_parameters({
            "event_id": event.id,
            "day": date
        })

    return StudentActivitiesValue(activities, student).json(200)
Ejemplo n.º 2
0
def update_activity(activity_external_id):
    activity = ActivitiesFinder.get_from_external_id(activity_external_id)
    company_activities = ActivitiesFinder.get_company_activities_from_activity_id(
        activity_external_id)
    speaker_activities = ActivitiesFinder.get_speaker_activities_from_activity_id(
        activity_external_id)
    activity_tags = TagsFinder.get_activity_tags_from_activity_id(
        activity_external_id)

    if activity is None:
        return APIErrorValue('Couldnt find activity').json(500)

    name = request.form.get('name')
    description = request.form.get('description')
    location = request.form.get('location')
    day = request.form.get('day')
    time = request.form.get('time')
    end_time = request.form.get('end_time')
    registration_link = request.form.get('registration_link')
    registration_open = request.form.get('registration_open')
    points = request.form.get('points') or None
    quest = request.form.get('quest')
    chat = request.form.get('chat')
    zoom_link = request.form.get('zoom_url')
    reward_id = request.form.get('reward') or None
    moderator = request.form.get('moderator') or None

    if time > end_time is None:
        return APIErrorValue('Activity starting time after ending time').json(
            500)

    if registration_open == 'True':
        registration_open = True
    else:
        registration_open = False

    if quest == 'True':
        quest = True
    else:
        quest = False

    chat_type = ActivityChatEnum[chat] if chat else None

    activity_type_external_id = request.form.get('type')
    activity_type = ActivityTypesFinder.get_from_external_id(
        uuid.UUID(activity_type_external_id))

    updated_activity = ActivitiesHandler.update_activity(
        activity=activity,
        activity_type=activity_type,
        name=name,
        description=description,
        location=location,
        day=day,
        time=time,
        end_time=end_time,
        registration_link=registration_link,
        registration_open=registration_open,
        points=points,
        quest=quest,
        zoom_link=zoom_link,
        chat_type=chat_type,
        chat=(chat == 'general'),
        reward_id=reward_id)

    if company_activities:
        for company_activity in company_activities:
            ActivitiesHandler.delete_company_activities(company_activity)

    if speaker_activities:
        for speaker_activity in speaker_activities:
            ActivitiesHandler.delete_speaker_activities(speaker_activity)

    if activity_tags:
        for activity_tag in activity_tags:
            TagsHandler.delete_activity_tag(activity_tag)

    # extract company names and speaker names from parameters
    companies = request.form.getlist('company')
    zoom_urls = request.form.getlist('url')
    speakers = request.form.getlist('speaker')
    tags = request.form.getlist('tag')

    # if company names where provided
    if companies:
        for index, name in enumerate(companies):
            company = CompaniesFinder.get_from_name(name)
            if company is None:
                return APIErrorValue('Couldnt find company').json(500)

            company_activity = ActivitiesHandler.add_company_activity(
                company, activity, zoom_urls[index])
            if company_activity is None:
                return APIErrorValue('Failed to create company activity').json(
                    500)

            if activity_type.name == 'Job Fair':
                job_fair_booth = ActivityTypesFinder.get_from_name(
                    'Job Fair Booth')
                if not ActivitiesFinder.get_from_parameters(
                    {
                        'name': company.name + " Booth",
                        'day': day
                    }):
                    job_fair_booth_activity = ActivitiesHandler.create_activity(
                        name=company.name + " Booth",
                        description="Visit " + company.name +
                        " booth to earn extra points",
                        activity_type=job_fair_booth,
                        event=activity.event,
                        location="Job Fair",
                        day=day,
                        time='10:30',
                        end_time='16:30',
                        points=40,
                        quest=False)
                    ActivitiesHandler.add_company_activity(
                        company, job_fair_booth_activity)

    if speakers:
        for name in speakers:
            speaker = SpeakersFinder.get_from_name(name)
            if speaker is None:
                return APIErrorValue('Couldnt find speaker').json(500)

            speaker_activity = ActivitiesHandler.add_speaker_activity(
                speaker, activity)
            if speaker_activity is None:
                return APIErrorValue('Failed to create speaker activity').json(
                    500)

        if (moderator and moderator in speakers):
            moderator = SpeakersFinder.get_from_name(moderator)
            if moderator is None:
                return APIErrorValue('Couldnt find moderator').json(500)

            ActivitiesHandler.update_activity(activity,
                                              activity_type,
                                              moderator_id=moderator.id)

        elif (not moderator):
            ActivitiesHandler.update_activity(activity,
                                              activity_type,
                                              moderator_id=None)

    if tags:
        for name in tags:
            tag = TagsFinder.get_by_name(name)
            if tag is None:
                return APIErrorValue('Couldnt find tag').json(500)

            activity_tag = TagsHandler.add_activity_tag(activity, tag)
            if activity_tag is None:
                return APIErrorValue('Failed to create activity tag').json(500)

    if updated_activity is None:
        event = EventsFinder.get_from_parameters({"default": True})

        if event is None or len(event) == 0:
            error = 'No default event found! Please set a default event in the menu "Events"'
            return render_template(
                'admin/activities/activities_dashboard.html',
                event=None,
                error=error,
                role=current_user.role.name)

        activity_types = event[0].activity_types

        try:
            minDate = datetime.strptime(event[0].start_date,
                                        '%d %b %Y, %a').strftime("%Y,%m,%d")
            maxDate = datetime.strptime(event[0].end_date,
                                        '%d %b %Y, %a').strftime("%Y,%m,%d")
        except:
            minDate = None
            maxDate = None

        return render_template('admin/activities/update_activity.html', \
            activity=activity, \
            types=activity_types, \
            companies=CompaniesFinder.get_all(), \
            speakers=SpeakersFinder.get_all(), \
            tags=TagsFinder.get_all(), \
            rewards=RewardsFinder.get_all_rewards(), \
            minDate=minDate, \
            maxDate=maxDate, \
            error="Failed to update activity!")

    return redirect(url_for('admin_api.activities_dashboard'))
Ejemplo n.º 3
0
def get_activities():
    search_parameters = request.args
    name = request.args.get('name')
    speaker = request.args.get('speaker')
    company = request.args.get('company')

    event = request.args.get('event')
    if event is None:
        event = EventsFinder.get_default_event()
    else:
        event = EventsFinder.get_from_name(event)

    if event is None:
        return APIErrorValue("Event no found!").json(404)

    activities_list = []

    # handle search bar requests
    if name is not None:
        search = name
        activities_list = ActivitiesFinder.search_by_name_and_event(
            search, event)

    # handle parameter requests
    elif speaker is not None:
        search = speaker
        speaker = SpeakersFinder.get_from_name(search)

        if speaker:
            activities_list = ActivitiesFinder.get_activities_from_speaker_and_event(
                speaker, event)

    elif company is not None:
        search = company
        company = CompaniesFinder.get_from_name(search)

        if company:
            activities_list = ActivitiesFinder.get_activities_from_company_and_event(
                company, event)

    elif len(search_parameters) != 0:
        search = 'search name'

        try:
            search_parameters = request.args.to_dict()
            search_parameters['type'] = ActivityTypesFinder.get_from_name(
                search_parameters['type']).id
            search_parameters['activity_type_id'] = search_parameters.pop(
                'type')
        except:
            pass

        activities_list = ActivitiesFinder.get_from_parameters(
            search_parameters)

    # request endpoint with no parameters should return all activities
    else:
        search = None
        activities_list = ActivitiesFinder.get_activities_from_event(event)

    if activities_list is None:
        return APIErrorValue('No results found').json(400)

    return ActivitiesValue(activities_list).json(200)