Esempio n. 1
0
def get_group_tasklist():
    ret = []
    for group in current_user.get_groups():
        ret.extend([task for task in group.get_tasks()])
    ret = sorted(ret, key=lambda v: v.finish_time)
    ret = [task.get_info_map() for task in ret]
    return Validity(True, {'group task list': ret}).get_resp()
Esempio n. 2
0
def profile():              # need to add aws reconfiguration somehow and allow for individual field updates
    form = RegisterForm()
    user = {'username': current_user['username'], 'email': current_user['email']}
    groups = current_user.get_groups(admin=True)
    if request.method == 'POST':
        if form.validate_on_submit():
            current_user.update_user(request.form)
            return redirect(url_for('profile'))
        else:
            flash_errors(form)
    return render_template('profile.html', rForm = form, user=user, groups=groups)
def index():
	recent_posts = get_recent_posts(current_user)

	post_form = CreatePostAnyGroupForm()
	post_form.group.choices = [(str(group.GroupID), group.Name) for group in current_user.get_groups()]
	comment_form = PostCommentForm()

	if request.method == 'POST':
		if post_form.validate_on_submit():
			new_post = Post(post_form.group.data,
							current_user.UserID,
							post_form.title.data,
							post_form.body.data)
			
			db.session.add(new_post)
			db.session.commit()

			flash(u"Post created.", 'success')
			return redirect(url_for('index'))

		elif comment_form.validate_on_submit():
			new_comment = PostComment(comment_form.parent_post_id.data,
									current_user.UserID,
									comment_form.body.data)

			db.session.add(new_comment)
			db.session.commit()
			return redirect(url_for('index'))

	return render_template('index.html', 
							title='Home', 
							recent_posts=recent_posts,
							post_form=post_form,
							comment_form=comment_form,
							format_local_time=format_local_time
							)
Esempio n. 4
0
def get_grouplist():
    ret = sorted([group for group in current_user.get_groups()],
                 key=lambda v: v.name)
    ret = [group.get_info_map() for group in ret]
    return Validity(True, {'group list': ret}).get_resp()
Esempio n. 5
0
def account():
    groups = current_user.get_groups()
    if current_user.self_group:
        if not current_user.self_group.aws:
            return redirect(url_for('aws')) # require user to offer AWS information before accessing main UI
    return render_template('account.html', groups=groups)
Esempio n. 6
0
def group_route():
    if not GROUPS_ENABLED:
        abort(403)

    if request.method == 'GET':
        action = get_arg_or_400('action')

        if action == 'my':
            return render_template('my_groups.html',
                                   groups=current_user.get_groups())

        elif action == 'new':
            new_group_form = NewGroupFrom()
            return render_template('new_group.html',
                                   form=new_group_form,
                                   groups=current_user.get_groups())
        elif action == 'search':
            search_group_form = SearchGroupForm()
            sport = get_arg_or_none('sport')
            if sport is None:
                groups = Group.query.limit(30).all()
            else:
                groups = Group.get_by_sport(sport)
            return render_template("search_group.html",
                                   query=groups,
                                   form=search_group_form)

        elif action == 'accept_invitation' or action == 'reject_invitation':
            group_id = get_arg_or_400('group_id')
            group: Group = Group.get_or_404(group_id)
            if current_user.id != group.admin_id:
                abort(403)
            invitation = Invitation.get_or_404(get_arg_or_400('id'))
            if action == 'accept_invitation':
                invitation.accept()
            else:
                invitation.reject()
            return redirect(
                url_for("group_route", action='invitations', id=group_id))

        group_id = get_arg_or_400('id', to_int=True)
        group = Group.get_or_404(group_id)
        members = group.get_members()
        is_member = current_user in members
        events = group.get_events()
        is_group_admin = group.admin_id == current_user.id
        if not is_member:
            is_member = None

        if action == 'delete' and is_group_admin:
            group.delete()
            return redirect(url_for('group_route', action='my'))

        elif action == 'edit' and is_group_admin:
            edit_group_form = NewGroupFrom(closed=group.closed,
                                           name=group.name,
                                           sport=group.sport)
            return render_template('edit_group.html',
                                   form=edit_group_form,
                                   group=group)

        elif action == 'show':
            return render_template('group.html',
                                   group=group,
                                   members=members,
                                   is_member=is_member,
                                   events=events,
                                   is_group_admin=is_group_admin)

        elif action == 'invitations':
            group_invitations: List[Invitation] = Invitation.get_all_for_group(
                group_id)
            return render_template("group_invitations.html",
                                   invitations=group_invitations,
                                   group_id=group_id)

        elif action == 'join':
            if current_user not in members:
                if group.closed:
                    Invitation.add(type=InvitationType.TO_GROUP,
                                   recipient_id=group.id,
                                   referrer_id=current_user.id)
                    flash(gettext("Invitation sent!"), "success")
                else:
                    new_row = GroupMember(user_id=current_user.id,
                                          group_id=group.id,
                                          time=timestamp())
                    db.session.add(new_row)
                    db.session.commit()
                    members.append(current_user)
                    is_member = True
            return render_template('group.html',
                                   group=group,
                                   members=members,
                                   is_member=is_member,
                                   events=events,
                                   is_group_admin=is_group_admin)

        elif action == 'leave':
            if current_user in members:
                row = GroupMember.query.filter_by(user_id=current_user.id,
                                                  group_id=group.id).first()
                db.session.delete(row)
                db.session.commit()
                members.remove(current_user)
                is_member = None
            return render_template('group.html',
                                   group=group,
                                   members=members,
                                   is_member=is_member,
                                   events=events,
                                   is_admin=is_group_admin)
        else:
            abort(403)

    else:
        action = get_arg_or_400('action')

        if action == 'edit':
            group_id = get_arg_or_400('id', to_int=True)
            group = Group.get_or_404(group_id)

            edit_group_form = NewGroupFrom(current_group=group)
            if edit_group_form.validate_on_submit():
                group.closed = edit_group_form.closed.data
                group.name = edit_group_form.name.data
                group.sport = edit_group_form.sport.data
                db.session.add(group)
                db.session.commit()
                return redirect(
                    url_for('group_route', action='show', id=group_id))
            else:
                return render_template('edit_group.html',
                                       form=edit_group_form,
                                       group=group)

        elif action == 'new':
            new_group_form = NewGroupFrom()
            if new_group_form.validate_on_submit():
                group = Group(admin_id=current_user.id,
                              name=new_group_form.name.data,
                              sport=new_group_form.sport.data,
                              closed=new_group_form.closed.data)
                db.session.add(group)
                db.session.commit()
                new_row = GroupMember(user_id=current_user.id,
                                      group_id=group.id,
                                      time=timestamp())
                db.session.add(new_row)
                db.session.commit()
                return redirect(
                    url_for('group_route', action='show', id=group.id))
            else:
                return render_template('new_group.html',
                                       form=new_group_form,
                                       groups=current_user.get_groups())
        elif action == 'search':
            search_group_form = SearchGroupForm()
            name = search_group_form.name.data
            sport = search_group_form.sport.data
            groups = Group.query.filter(Group.name.ilike(f"%{name}%")). \
                filter(Group.sport == sport if sport != "None" else Group.sport == Group.sport).all()
            return render_template("search_group.html",
                                   query=groups,
                                   form=search_group_form)
        else:
            abort(400)
Esempio n. 7
0
def profile_route():
    if request.method == 'GET':
        action = get_arg_or_400('action')
        if action == 'my':
            groups = current_user.get_groups()
            friends = current_user.friends_get()
            videos: dict = UserVideos.get_all_for_user_id(current_user.id)
            return render_template("profile.html",
                                   current_user=current_user,
                                   user=current_user,
                                   groups=groups,
                                   friends=friends,
                                   my=True,
                                   videos=videos)
        elif action == 'show':
            id = get_arg_or_400('id', to_int=True)
            if current_user.id == id:
                return redirect(url_for('profile_route', action='my'))
            user = User.get_or_404(id)
            groups = user.get_groups()
            friends = user.friends_get()
            chat = ChatMember.get_private_chat(current_user.id, id)
            is_friend = True if current_user in friends else None
            videos: dict = UserVideos.get_all_for_user_id(user.id)
            return render_template("profile.html",
                                   chat_id=chat.id if chat else None,
                                   current_user=current_user,
                                   groups=groups,
                                   friends=friends,
                                   is_friend=is_friend,
                                   user=user,
                                   videos=videos)
        elif action == 'edit':
            form = EditProfileForm(name=current_user.name,
                                   last_name=current_user.last_name,
                                   age=current_user.age,
                                   gender=current_user.gender,
                                   sport=current_user.sport,
                                   times_values=PlayTime.get_all_for_user_id(
                                       current_user.id),
                                   city=current_user.city)
            return render_template("edit_profile.html",
                                   title="Edit profile",
                                   form=form,
                                   current_user=current_user)
        elif action == 'add_play_time':
            add_play_time_form = AddPlayTimeForm()
            all_play_times = PlayTime.get_all_for_user_id(current_user.id)
            current_play_time: Optional[PlayTime] = PlayTime.get(
                get_arg_or_none("id"))
            initial_location = nominatim.geocode(current_user.city)
            init_lat = initial_location.latitude if initial_location else None
            init_lng = initial_location.longitude if initial_location else None
            markers = []
            initial_zoom = 13
            if current_play_time is not None:
                add_play_time_form.day_of_week.data = current_play_time.day_of_week
                add_play_time_form.start_time.data = current_play_time.start_time
                add_play_time_form.end_time.data = current_play_time.end_time
                add_play_time_form.address.data = current_play_time.address.short_address
                location = Location.get(current_play_time.location_id)
                if location is not None:
                    markers.append((location.latitude, location.longitude))
                    init_lat = location.latitude
                    init_lng = location.longitude
                    initial_zoom = 14.5
            loc_map = Map(zoom=initial_zoom,
                          identifier="loc_map",
                          lat=init_lat,
                          lng=init_lng,
                          style="height:600px;width:600px;margin:8;",
                          language=current_user.language,
                          markers=markers)
            return render_template("add_play_time.html",
                                   all_play_times=all_play_times,
                                   form=add_play_time_form,
                                   map=loc_map)
        elif action == 'show_play_times':
            initial_zoom = 13
            initial_location = nominatim.geocode(current_user.city)
            init_lat = initial_location.latitude if initial_location else None
            init_lng = initial_location.longitude if initial_location else None
            markers = []
            current_play_time: Optional[PlayTime] = PlayTime.get(
                get_arg_or_none("id"))
            if current_play_time is not None:
                location = Location.get(current_play_time.location_id)
                if location is not None:
                    markers.append((location.latitude, location.longitude))
                    init_lat = location.latitude
                    init_lng = location.longitude
                    initial_zoom = 14.5
            loc_map = Map(zoom=initial_zoom,
                          identifier="loc_map",
                          lat=init_lat,
                          lng=init_lng,
                          style="height:600px;width:730px;margin:8;",
                          language=current_user.language,
                          markers=markers)
            user_id = get_arg_or_400('user_id', to_int=True)
            all_play_times = PlayTime.get_all_for_user_id(user_id)
            return render_template("show_play_times.html",
                                   map=loc_map,
                                   all_play_times=all_play_times,
                                   user_id=user_id,
                                   my=user_id == current_user.id)
        elif action == 'edit_videos':
            group = namedtuple('Group', ['sport', 'video'])
            videos = UserVideos.get_all_for_user_id(current_user.id)
            arr = [
                group(sport, (make_link(vid) if
                              (vid := videos.get(sport)) is not None else ""))
                for sport in current_user.sport
            ]
Esempio n. 8
0
def event_route():
    if request.method == 'GET':

        action = get_arg_or_400('action')
        if action == 'my':
            events = filter_not_none(current_user.get_events())
            return render_template(
                "my_events.html",
                events=events if len(events) > 0 else None
            )

        elif action == 'new':
            new_event_form = EditEventForm(groups=filter_not_none(current_user.get_groups()))
            return render_template("new_event.html", form=new_event_form)
        elif action == 'search':
            events = Event.query.all()
            user_vector = UserVector(
                age=current_user.age,
                gender=current_user.gender,
                sport=current_user.sport,
                city=current_user.city,
                last_login=current_user.last_login,
                include_play_times=True,
                play_times=PlayTime.get_all_for_user_id(current_user.id),
                user=current_user
            )
            sorted_events = list(
                map(
                    lambda x: x[0].event,
                    sorted(
                        map(
                            lambda x: (
                                x,
                                user_vector.calculate_diff_with_event(x)
                            ),
                            map(
                                lambda x: EventVector(
                                    sport=[x.sport],
                                    city=x.creator.city,
                                    group=x.group,
                                    closed=x.closed,
                                    last_login=x.creator.last_login,
                                    play_times=EventPlayTimes.get_all_for_event(x.id),
                                    event=x
                                ),
                                events
                            )
                        ),
                        key=lambda x: x[1],
                        reverse=True
                    )
                )
            )
            form = SearchEventForm()
            return render_template(
                "search_event.html",
                events=sorted_events,
                form=form
            )

        event_id = get_arg_or_400('event_id', to_int=True)
        event: Event = Event.get_or_404(event_id)
        group = event.group
        group: Group = group if group else None
        is_event_admin = event.creator_id == current_user.id or group is not None and group.admin_id == current_user.id
        all_play_times = EventPlayTimes.get_all_for_event(event_id)

        if action == 'delete':
            event.delete()
            return redirect(url_for("event_route", action='my'))

        elif action == 'accept_invitation' or action == 'reject_invitation':
            if current_user.id != event.creator_id:
                abort(403)
            invitation = Invitation.get_or_404(get_arg_or_400('id'))
            if action == 'accept_invitation':
                invitation.accept()
            else:
                invitation.reject()
            return redirect(url_for("event_route", action='invitations', event_id=event_id))

        elif action == 'show_play_times':
            return render_template(
                "show_event_play_times.html",
                map=get_loc_map(event, EventPlayTimes.get_or_none(get_arg_or_none("play_time_id"))),
                all_play_times=all_play_times,
                is_event_admin=is_event_admin,
                event_id=event_id
            )

        elif action == 'add_play_time':
            add_event_play_time_form = AddEventPlayTimeForm()
            current_play_time: EventPlayTimes = EventPlayTimes.get_or_none(get_arg_or_none("play_time_id"))
            if current_play_time:
                add_event_play_time_form.day_of_week.data = current_play_time.day_of_week
                add_event_play_time_form.start_time.data = current_play_time.start_time
                add_event_play_time_form.end_time.data = current_play_time.end_time
                add_event_play_time_form.address.data = Address.get(current_play_time.address_id).get_short_address()
            template = render_template(
                "add_event_play_time.html",
                map=get_loc_map(event, current_play_time, "height:600px;width:650px;margin:8;"),
                form=add_event_play_time_form,
                all_play_times=all_play_times,
                event_id=event_id
            )
            return template

        elif action == 'edit':
            if not is_event_admin:
                abort(403)
            play_time: Optional[EventPlayTimes] = pt if (pt := all_play_times[0] if all_play_times else None) else None
            edit_event_form = EditEventForm(
                groups=filter_not_none(current_user.get_groups()),
                closed=event.closed,
                name=event.name,
                description=event.description,
                sport=event.sport,
                recurring=event.recurring,
                day_of_week=play_time.day_of_week if play_time else None,
                start_time=play_time.start_time if play_time else None,
                end_time=play_time.end_time if play_time else None,
                address=play_time.address.short_address if play_time and play_time.address else None
            )
            initial_location = Location.get(play_time.location_id) if play_time else None
            loc_map = Map(
                zoom=14.5 if initial_location else 2,
                identifier="loc_map",
                lat=initial_location.latitude if initial_location else 0,
                lng=initial_location.longitude if initial_location else 0,
                style="height:600px;width:600px;margin:8;",
                language=current_user.language,
                markers=[(initial_location.latitude, initial_location.longitude)] if initial_location else []
            )
            return render_template("edit_event.html", form=edit_event_form, event=event, map=loc_map)

        elif action == 'invitations':
            if not is_event_admin:
                abort(403)
            event_invitations: List[Invitation] = Invitation.get_all_for_event(event_id)
            return render_template("event_invitations.html", invitations=event_invitations, event_id=event_id)

        elif action == 'show':
            members = event.get_members()
            is_member = True if current_user in members else None
            members = members if len(members) > 0 else None
            play_time_id = get_arg_or_none('play_time_id')
            selected_play_time = None
            if play_time_id is not None:
                selected_play_time = EventPlayTimes.query.filter_by(event_id=event_id, id=play_time_id).first()
            if selected_play_time is None:
                play_times = EventPlayTimes.get_all_for_event(event_id)
                # closest_play_time: EventPlayTimes = play_times[0] if play_times and len(play_times) else None # TODO
                current_day_of_week = get_current_day_of_week()
                current_time = get_current_time()
                valid_play_times = list(
                    filter(
                        lambda x:
                        x.day_of_week is not None and
                        x.start_time is not None and
                        (
                            (x.day_of_week == current_day_of_week and time_to_seconds(x.start_time) >= current_time) or
                            x.day_of_week != current_day_of_week
                        ),
                        play_times
                    )
                )
                sorted_play_times = list(
                    sorted(
                        valid_play_times,
                        key=lambda x: -10 * abs(current_day_of_week - x.day_of_week) - abs(current_time - time_to_seconds(x.start_time)),
                        reverse=True
                    )
                ) \
                    if valid_play_times \
                    else None
                closest_play_time = sorted_play_times[0] if sorted_play_times else None
                initial_location = LocationToAddress.get_location_for_address_id(closest_play_time.address_id) \
                    if closest_play_time \
                    else None
                loc_map = Map(
                    zoom=14.5 if initial_location else 2,
                    identifier="loc_map",
                    lat=initial_location.latitude if initial_location else 0,
                    lng=initial_location.longitude if initial_location else 0,
                    style="height:600px;width:600px;margin:8;",
                    language=current_user.language,
                    markers=[(initial_location.latitude, initial_location.longitude)] if initial_location else []
                ) if initial_location else None
                return render_template(
                    "event.html",
                    event=event,
                    group=group,
                    members=members,
                    is_member=is_member,
                    event_id=event_id,
                    is_event_admin=is_event_admin,
                    play_times=all_play_times,
                    closest_play_time=closest_play_time,
                    map=loc_map
                )
            else:
                initial_address: Address = Address.get(selected_play_time.address_id) if selected_play_time else None
                initial_location = LocationToAddress.get_location_for_address_id(
                    selected_play_time.address_id) if selected_play_time else None
                loc_map = Map(
                    zoom=14.5 if initial_location else 2,
                    identifier="loc_map",
                    lat=initial_location.latitude if initial_location else 0,
                    lng=initial_location.longitude if initial_location else 0,
                    style="height:600px;width:600px;margin:8;",
                    language=current_user.language,
                    markers=[(initial_location.latitude, initial_location.longitude)] if initial_location else []
                )
                return render_template(
                    "event.html",
                    event=event,
                    group=group,
                    members=members,
                    is_member=is_member,
                    event_id=event_id,
                    is_event_admin=is_event_admin,
                    play_times=all_play_times,
                    map_address=initial_address.get_short_address() if initial_address else None,
                    map=loc_map
                )

        elif action == 'join':
            if event.closed:
                Invitation.add(
                    InvitationType.TO_EVENT,
                    recipient_id=event.id,
                    referrer_id=current_user.id,
                    expiration_time=event.time
                )
                flash(gettext("Invitation sent!"), "success")
            else:
                event.add_member(current_user)
            return redirect(url_for('event_route', action='show', event_id=event_id))

        elif action == 'leave':
            event.remove_member(current_user)
            return redirect(url_for('event_route', action='show', event_id=event_id))

        elif action == 'find_people':
            # FIXME сделать нормальный фильтр
            event_users = set(event.get_members())
            invited = list(
                filter(
                    lambda x: x,
                    map(
                        lambda x: User.get_or_none(x.recipient_id),
                        Invitation.get_all_from_event(event_id)
                    )
                )
            )
            all_users = set(User.query.order_by(User.register_time).all())
            users: list = list(
                filter(
                    lambda user_tmp: event.sport in (user_tmp.sport if user_tmp.sport else []),
                    list(all_users - event_users - set(invited))
                )
            )
            return render_template(
                "find_people.html",
                form=SearchPlayerForm(),
                event_id=event.id,
                invited_people=invited,
                people=users if len(users) > 0 else None
            )
        elif action == 'send_invite':
            Invitation.add(
                type=InvitationType.FROM_EVENT,
                recipient_id=get_arg_or_400('user_id'),
                referrer_id=event_id
            )
            return redirect(url_for('event_route', action='find_people', event_id=event_id))
        elif action == 'revoke_invite':
            invitation = Invitation.query.filter(
                (Invitation.type==InvitationType.FROM_EVENT) &
                (Invitation.recipient_id==get_arg_or_400('user_id')) &
                (Invitation.referrer_id==event_id)
            ).first()
            if invitation:
                db.session.delete(invitation)
                db.session.commit()
            return redirect(url_for('event_route', action='find_people', event_id=event_id))
        else:
            abort(400)
Esempio n. 9
0
            ).first()
            if invitation:
                db.session.delete(invitation)
                db.session.commit()
            return redirect(url_for('event_route', action='find_people', event_id=event_id))
        else:
            abort(400)
# ------------------------------------------------------------------------------------------
# ------------------------------------- POST -----------------------------------------------
    elif request.method == 'POST':
        action = get_arg_or_400('action')

        if action == 'edit':
            event_id = get_arg_or_400('event_id', to_int=True)
            event = Event.get_or_404(event_id)
            edit_event_form = EditEventForm(current_event=event, groups=filter_not_none(current_user.get_groups()))
            if edit_event_form.validate_on_submit():
                event.closed = edit_event_form.closed.data
                event.name = edit_event_form.name.data
                event.description = edit_event_form.description.data
                event.sport = edit_event_form.sport.data
                event.recurring = edit_event_form.recurring.data
                db.session.add(event)
                db.session.commit()
                return redirect(url_for('event_route', action='show', event_id=event.id))
            else:
                return render_template("edit_event.html", form=edit_event_form, event=event, map=get_loc_map())

        elif action == 'new':
            new_event_form = EditEventForm(groups=filter_not_none(current_user.get_groups()))
            if new_event_form.validate_on_submit():