Пример #1
0
    def post(self):
        username = request.form['username']
        password = request.form['password']

        user = db_session.query(User).filter(User.username == username,
                                             User.password == password)
        user_exists = db_session.query(user.exists()).scalar()

        if user_exists:
            session['loggedIn'] = True
            session['username'] = username
            return redirect('/')
        else:
            return redirect('/login')
Пример #2
0
    def get_event(self, event_id):
        event = Event.query.filter(Event.event_id == event_id).first()
        if not event: return None

        user = UserController().current_user
        if user:
            user_event = UserEvent.query.filter(
                and_(UserEvent.event_id == event.event_id,
                     UserEvent.user_id == user.user_id)).first()

            if user_event:
                event.current_user_event = user_event

            following_event_users = db_session.query(User).filter(
                and_(UserEvent.event_id == event.event_id,
                     UserEvent.user_id == User.user_id,
                     Follow.user_id == user.user_id,
                     Follow.follow_id == User.user_id,
                     User.user_id != user.user_id, Follow.active == True))
            event.card_event_users = [{
                "user_id": u.user_id,
                "username": u.username,
                "image_url": u.image_url
            } for u in following_event_users]

        user_event_count = UserEvent.query.filter(
            and_(UserEvent.event_id == event_id,
                 UserEvent.interest > UserEvent.interest_level(UserEvent.SKIP),
                 UserEvent.interest <= (max(UserEvent.DONE_LEVELS)))).count()

        event.card_user_count = user_event_count

        return event
Пример #3
0
    def _filter_events(klass,
                       events,
                       query=None,
                       categories=None,
                       tags=None,
                       flags=None):
        query_tags = None
        if query:
            tags_matching_query = Tag.query.filter(
                Tag.tag_name.ilike("{}%".format(query)))
            query_tags = {t.tag_name for t in tags_matching_query}

            if query_tags:
                if tags is None:
                    tags = query_tags
                else:
                    tags |= query_tags
            else:
                events = klass._filter_events_by_query(events=events,
                                                       query=query)

        if tags or categories:
            # Filter out valid_tags
            valid_tags = db_session.query(func.distinct(Tag.tag_name)).filter(
                Tag.tag_name.in_(tags))
            tags = set(t[0] for t in valid_tags)

            events = klass._filter_events_by_tags(events,
                                                  tags=tags,
                                                  categories=categories)

        if flags:
            events = klass._filter_events_by_flags(events, flags=flags)

        return events, tags
Пример #4
0
    def get_suggested(self, user=None, query=None):
        if user is None: user = self.current_user
        if user is None: return []

        already_following_user_ids = alias(
            db_session.query(Follow.follow_id).select_from(Follow).filter(
                and_(Follow.user_id == user.user_id, Follow.active)),
            'already_following_user_ids')

        blocking_user_ids = alias(user.all_blocks_table(), 'blocking_user_ids')

        suggested_users = db_session.query(User).filter(
            and_(User.user_id != user.user_id,
                 ~User.user_id.in_(already_following_user_ids),
                 ~User.user_id.in_(blocking_user_ids)))

        return suggested_users
Пример #5
0
    def get_events(self,
                   query=None,
                   categories=None,
                   tags=None,
                   cities=None,
                   flags=None,
                   page=1,
                   future_only=False):
        current_user = UserController().current_user
        selected_categories = set(categories.split(',') if categories else [])
        selected_tags = set(tags.split(',') if tags else [])

        events_with_counts = db_session.query(
            Event,
            func.count(func.distinct(
                UserEvent.user_id)).label('ct')).outerjoin(
                    UserEvent, UserEvent.event_id == Event.event_id).group_by(
                        Event.event_id).order_by(desc('ct'))

        if current_user:
            current_user_events_table = alias(current_user.user_events(),
                                              'current_user_events_table')
            events_with_counts = events_with_counts.filter(~Event.event_id.in_(
                db_session.query(
                    current_user_events_table.c.user_events_event_id)))

        results, categories, tags, event_cities, results_table = self._process_events(
            events=events_with_counts,
            cities=cities,
            page=page,
            query=query,
            user=current_user,
            selected_categories=selected_categories,
            selected_tags=selected_tags,
            flags=flags,
            future_only=future_only)

        return results, categories, tags, event_cities
Пример #6
0
    def _cities_for_events(klass, events, limit=None):
        cities_query = db_session.query(
            Event.city,
            func.count(distinct(
                Event.event_id)).label('ct')).filter(Event.city != None)

        events_table = alias(events, 'events_table')

        cities_query = cities_query.join(
            events_table, Event.event_id == events_table.c.events_event_id)

        cities_query = cities_query.group_by(Event.city).order_by(desc('ct'))

        if limit:
            cities_query = cities_query.limit(limit)

        return [{
            'chip_name': dat[0] or 'Unknown',
            'ct': dat[1],
        } for dat in cities_query if dat[1] > 0]
Пример #7
0
    def _filter_events_by_tags(klass, events, tags, categories=None):
        event_matches = db_session.query(
            EventTag.event_id.label('event_id'),
            func.count(distinct(Tag.tag_id)).label('ct')).join(
                Tag, Tag.tag_id == EventTag.tag_id)

        if tags and categories:
            event_matches = event_matches.filter(
                and_(Tag.tag_name.in_(tags), Tag.tag_type.in_(categories)))
        elif tags:
            event_matches = event_matches.filter(Tag.tag_name.in_(tags))
        elif categories:
            event_matches = event_matches.filter(Tag.tag_type.in_(categories))

        event_matches = event_matches.group_by(EventTag.event_id)

        event_matches = alias(event_matches, 'event_matches')

        return events.join(event_matches,
                           Event.event_id == event_matches.c.event_id).filter(
                               event_matches.c.ct >= len(tags))
Пример #8
0
    def get_users(self, query=None, tag=None, page=1):
        current_user = self.current_user

        relationship_types = User.relationship_types()

        users_table = None
        if tag and tag in relationship_types:
            users_table = self.get(relationship_type=tag,
                                   query=query,
                                   table_only=True)

            if tag == User.FOLLOWING:
                suggested_users_table = self.get(
                    relationship_type=User.SUGGESTED,
                    query=query,
                    table_only=True,
                    limit=5)
                users_table = users_table.union(suggested_users_table)

        if users_table:
            if page:
                users_table = users_table.limit(self.PAGE_SIZE).offset(
                    (page - 1) * self.PAGE_SIZE)
            users = users_table.all()
        else:
            users = []

        if users:
            all_user_ids = set(u.user_id for u in users)

            event_counts_by_user_id = db_session.query(
                UserEvent.user_id,
                func.count(UserEvent.event_id).label('ct')).filter(
                    and_(UserEvent.user_id.in_(all_user_ids),
                         UserEvent.interest.in_(
                             UserEvent.INTERESTED_LEVELS))).group_by(
                                 UserEvent.user_id)
            event_counts_by_user_id = {
                str(x[0]): x[1]
                for x in event_counts_by_user_id
            }

            follower_counts_by_user_id = db_session.query(
                Follow.follow_id,
                func.count(Follow.user_id).label('ct')).filter(
                    and_(Follow.follow_id.in_(all_user_ids),
                         Follow.active == True)).group_by(Follow.follow_id)
            follower_counts_by_user_id = {
                str(x[0]): x[1]
                for x in follower_counts_by_user_id
            }

            is_follower_by_user_id = db_session.query(Follow.follow_id).filter(
                and_(Follow.follow_id == current_user.user_id,
                     Follow.user_id.in_(all_user_ids), Follow.active == True))
            is_follower_by_user_id = set(
                str(x[0]) for x in is_follower_by_user_id)

            is_following_by_user_id = db_session.query(
                Follow.follow_id).filter(
                    and_(Follow.user_id == current_user.user_id,
                         Follow.follow_id.in_(all_user_ids),
                         Follow.active == True))
            is_following_by_user_id = set(
                str(x[0]) for x in is_following_by_user_id)

            is_blocked_by_user_id = db_session.query(Block.block_id).filter(
                and_(Block.user_id == current_user.user_id,
                     Block.block_id.in_(all_user_ids), Block.active == True))
            is_blocked_by_user_id = set(
                str(x[0]) for x in is_blocked_by_user_id)

            for user in users:
                uid = str(user.user_id)
                user.card_event_count = get_from(event_counts_by_user_id,
                                                 [uid], 0)
                user.card_follower_count = get_from(follower_counts_by_user_id,
                                                    [uid], 0)
                user.card_is_follower = uid in is_follower_by_user_id
                user.card_is_following = uid in is_following_by_user_id
                user.card_is_blocked = uid in is_blocked_by_user_id

                if not (user.card_is_follower or user.card_is_following
                        or user.card_is_blocked):
                    user.card_is_suggested = True

        return users, relationship_types
Пример #9
0
def run():
    row_user = db_session.query(User).first()
Пример #10
0
    def get_events_for_user_by_interested(self,
                                          interested,
                                          user=None,
                                          query=None,
                                          categories=None,
                                          tags=None,
                                          cities=None,
                                          flags=None,
                                          page=1,
                                          future_only=False):
        current_user = UserController().current_user
        if not user: user = current_user
        selected_categories = set(categories.split(',') if categories else [])
        selected_tags = set(tags.split(',') if tags else [])

        results = []
        categories = []
        tags = []
        event_cities = []
        if user:
            events_with_counts = db_session.query(
                Event,
                func.count(Event.user_events).label('ct')).join(
                    UserEvent,
                    UserEvent.user_id == user.user_id,
                ).filter(
                    and_(UserEvent.interest != None,
                         Event.event_id == UserEvent.event_id)).group_by(
                             Event.event_id)

            if interested:
                filter_conditions = []
                if UserEvent.DONE in interested:
                    filter_conditions.extend(UserEvent.DONE_LEVELS)
                if UserEvent.INTERESTED in interested:
                    filter_conditions.extend([
                        UserEvent.interest_level(UserEvent.GO),
                        UserEvent.interest_level(UserEvent.MAYBE)
                    ])
                for il in [UserEvent.GO, UserEvent.MAYBE, UserEvent.SKIP]:
                    if il in interested:
                        filter_conditions.append(UserEvent.interest_level(il))

                if filter_conditions:
                    events_with_counts = events_with_counts.filter(
                        UserEvent.interest.in_(filter_conditions))

            results, categories, tags, event_cities, results_table = self._process_events(
                events=events_with_counts,
                cities=cities,
                page=page,
                query=query,
                user=user,
                selected_categories=selected_categories,
                selected_tags=selected_tags,
                flags=flags,
                future_only=future_only)

            if current_user and results:
                result_events_table = alias(results_table, 'events_table')

                current_user_events = db_session.query(UserEvent).filter(
                    UserEvent.user_id == current_user.user_id).join(
                        result_events_table,
                        result_events_table.c.events_event_id ==
                        UserEvent.event_id).all()
                if current_user_events is not None:
                    current_user_events_by_event_id = {
                        x.event_id: x
                        for x in current_user_events
                    }

                    for event in results:
                        event.current_user_event = get_from(
                            current_user_events_by_event_id, [event.event_id])

        return results, categories, tags, event_cities
Пример #11
0
    def _process_events(klass,
                        events,
                        page,
                        query=None,
                        cities=None,
                        user=None,
                        flags=None,
                        selected_tags=None,
                        selected_categories=None,
                        future_only=None):
        if future_only:
            events_with_counts = events_with_counts.filter(
                or_(Event.start_time >= datetime.datetime.now(),
                    Event.end_time >= datetime.datetime.now()))

        events = events.filter(Event.status != Event.STATUS_CLOSED_PERM)

        events, selected_tags = klass._filter_events(
            events,
            query=query,
            categories=selected_categories,
            tags=selected_tags,
            flags=flags)

        event_cities = klass._cities_for_events(events)
        if cities:
            events = events.filter(Event.city.in_(cities))
            for city in event_cities:
                city['selected'] = city['chip_name'] in cities

        tags, categories = klass._tags_for_events(
            events=events,
            selected_categories=selected_categories,
            selected_tags=selected_tags)

        event_user_ids = None
        if user:
            event_ids = {e[0].event_id for e in events if e[1]}

            following_user_ids = alias(
                db_session.query(func.distinct(Follow.follow_id)).filter(
                    and_(UserEvent.event_id.in_(event_ids),
                         UserEvent.user_id == Follow.follow_id,
                         Follow.user_id == user.user_id,
                         Follow.follow_id != user.user_id,
                         Follow.active == True)), "following_user_ids")

            event_users = {
                str(u.user_id): {
                    'user_id': u.user_id,
                    'username': u.username,
                    'image_url': u.image_url
                }
                for u in User.query.filter(User.user_id.in_(
                    following_user_ids))
            }

            events_with_following_counts = db_session.query(
                UserEvent.event_id,
                func.array_agg(func.distinct(
                    User.user_id)).label('user_ids')).filter(
                        and_(Follow.user_id == user.user_id,
                             UserEvent.user_id == Follow.follow_id,
                             UserEvent.event_id.in_(event_ids),
                             Follow.follow_id != user.user_id)).group_by(
                                 UserEvent.event_id)
            event_user_ids = {
                row[0]: set(str(follower_id) for follower_id in row[1])
                for row in events_with_following_counts
            }

        events = klass._order_events(events)
        events = events.limit(klass.PAGE_SIZE).offset(
            (page - 1) * klass.PAGE_SIZE)

        results = []
        for event, user_count in events:
            event.card_user_count = user_count
            if event_user_ids and event.event_id in event_user_ids:
                event.card_event_users = [
                    event_users[x] for x in event_user_ids[event.event_id]
                    if x in event_users
                ]
            results.append(event)

        return results, categories, tags, event_cities, events
Пример #12
0
    def _tags_for_events(klass,
                         events=None,
                         selected_categories=None,
                         selected_tags=None,
                         limit=None,
                         future_only=False):
        tag_query = db_session.query(
            Tag.tag_name,
            func.count(distinct(EventTag.event_id)).label('ct')).join(
                EventTag, Tag.tag_id == EventTag.tag_id).join(
                    Event, EventTag.event_id == Event.event_id)

        category_query = db_session.query(
            Tag.tag_type,
            func.count(distinct(EventTag.event_id)).label('ct')).join(
                EventTag, Tag.tag_id == EventTag.tag_id)

        if events:
            events_table = alias(events, 'events_table')

            tag_query = tag_query.join(events_table)
            category_query = category_query.join(events_table)

        if future_only:
            tag_query = tag_query.filter(
                Event.end_time >= datetime.datetime.now())
            category_query = category_query.filter(
                Event.end_time >= datetime.datetime.now())

        tag_query = tag_query.group_by(Tag.tag_name).order_by(desc('ct'))

        category_query = category_query.group_by(Tag.tag_type).order_by(
            desc('ct'))

        if limit:
            tag_query = tag_query.limit(limit)
            category_query = category_query.limit(limit)

        distinct_tags = {t: {'chip_name': t, 'ct': 0} for t in selected_tags}
        for t in tag_query:
            chip_name = t[0]
            chip_ct = t[1]
            if chip_ct > 0:
                if chip_name not in distinct_tags:
                    distinct_tags[chip_name] = {'chip_name': chip_name}
                distinct_tags[chip_name]['ct'] = chip_ct
        tags = distinct_tags.values()

        # category_cts = {c[0]: c[1] for c in category_query}
        # categories = [
        #   {
        #     'chip_name': c,
        #     'ct': get_from(category_cts, [c], ""),
        #   } for c in Tag.TYPES
        # ]
        categories = []
        #

        if selected_categories:
            is_any_category_selected = False
            if categories:
                for event_category in categories:
                    is_category_selected = event_category[
                        'chip_name'] in selected_categories
                    event_category['selected'] = is_category_selected
                    if not is_any_category_selected and is_category_selected:
                        is_any_category_selected = is_category_selected
            categories = sorted(
                [c for c in categories if c['selected']],
                key=lambda c: c['chip_name']) + sorted(
                    [c for c in categories if not c['selected']],
                    key=lambda c: c['ct'] * -1)

        if selected_tags:
            is_any_tag_selected = False
            if tags:
                for event_tag in tags:
                    is_tag_selected = event_tag['chip_name'] in selected_tags
                    event_tag['selected'] = is_tag_selected
                    if not is_any_tag_selected and is_tag_selected:
                        is_any_tag_selected = is_tag_selected
            tags = sorted([t for t in tags if t['selected']],
                          key=lambda t: t['chip_name']) + sorted(
                              [t for t in tags if not t['selected']],
                              key=lambda t: t['ct'] * -1)

        return tags, categories