Example #1
0
def list_events(request, location=None, template='event/list.html', queryset=None, extra_context=None, hide_top3=False):
    location = location or request.location
    request.location = location # stick user to this location going forward
    request.location_name = settings.LOCATION_DATA.get(location, settings.EMPTY_LOCATION_DATA)[3]
    if queryset is None:
        today, td = today_timedelta_by_location(location)
        queryset = Event.objects.active_by_location(location=location, attending_user=request.user_profile, today=today).order_by('event_date', 'event_start_time', 'pk')
        # queryset = queryset.exclude(ext_event_source='mlb2010', location='user-entered')
        queryset = queryset.exclude(location='user-entered') # don't show user entered events on city pages
    ctx = {
        'title':'Upcoming Events',
        'events':queryset,
        'location_name':Event.LOCATIONMAP.get(location, 'Boston, MA'),
    }
    if not hide_top3:
        userid = request.user.is_authenticated() and request.user.pk or 0
        key = shorten_key(u"popular-ourpick-destination:%s:%s" % (location, userid))
        px = cache.cache.get(key, None)
        if px is None:
            popular = Event.objects.active_by_location(
                location=location,
                attending_user=request.user_profile,
                event_date__lte=datetime.today() + timedelta(days=7),
            ).order_by('-tweet_count')[:1]
            ctx['popular'] = get_or_none(popular)
            ctx['ourpick'] = get_or_none(Event.objects.active_by_location(location=location, attending_user=request.user_profile, is_homepage_worthy=True).order_by('event_date', 'event_start_time', 'pk')[:1])
            ctx['destination'] = get_or_none(Event.objects.active_by_location(location='destination', attending_user=request.user_profile).order_by('-destination_timestamp')[:1])
            px = (ctx['popular'], ctx['ourpick'], ctx['destination'])
            cache.cache.set(key, px, 600)
        else:
            ctx['popular'], ctx['ourpick'], ctx['destination'] = px
    if extra_context:
        ctx.update(extra_context)
    other_cities = sorted(settings.LOCATION_DATA.keys())
    other_cities.remove(location)
    city_list_html = [
        ('<a href="http://%s.%s%s%s">%s</a>' % (settings.LOCATION_SUBDOMAIN_REVERSE_MAP[loc], settings.DISPLAY_SITE_DOMAIN,  _SUBDOMAIN_PORT, reverse("list_events"), settings.LOCATION_DATA.get(loc)[3])) for loc in other_cities
    ]
    ctx['other_cities'] = ', '.join(city_list_html)
    if request.mobile:
        template = 'mobile/event/city.html'
    if request.user.is_authenticated():
        # Get Friends' Favorites count
        ctx['num_ff'] = get_friends_favorites_count(request.user_profile, location)
    ctx['needs_fbml'] = True
    return render_view(request, template, ctx)
Example #2
0
def search_events(request, location=None):
    ctx = {'title':'Upcoming Events', 'searched':True, 'is_search':True, 'limit':None}
    ctx['rpp'] = int(request.REQUEST.get('rpp', 10))
    location = location or request.location
    request.location = location # stick user to this location going forward
    request.location_name = settings.LOCATION_DATA.get(location, settings.EMPTY_LOCATION_DATA)[3]
    today, td = today_timedelta_by_location(location)
    qx = Event.objects.active(attending_user=request.user_profile, today=today).order_by('event_date', 'event_start_time', 'pk')
    loc = request.REQUEST.get("location", None)
    any_loc = loc == 'any'
    if not any_loc:
        # qx = qx.exclude(ext_event_source='mlb2010', location='user-entered')
        qx = qx.exclude(location='user-entered') # don't show user entered events on city pages
    f = request.GET.get('filter', '').lower()
    sort_by = request.GET.get('sort', '').lower()
    by_date = request.GET.get('date', None)
    choices = request.GET.getlist('choices')    
    if 'free' in choices:
        qx = qx.filter(is_free=True)
    if 'checkins' in sort_by:
        # Checkins must automatically limit the view to today
        f = 'today'
    if f:
        if not request.user_profile and f in  ('favorites', 'my-calendar', 'recommended'):
            add_message(request, "You will need to sign in first.")
            return HttpResponseRedirect(u"%s?next=%s" % (reverse("signin"), request.get_full_path()))
        ctx['filter'] = f
        if f in ('favorites', 'my-calendar') and request.user_profile:
            ctx['title'] = "I'm In For:"
            qx = qx.filter(
               attendee__attendee_profile=request.user_profile
            ).distinct().order_by('event_date', 'event_start_time', 'pk')
            if settings.CALENDAR_FILTER_BY_LOCATION:
                qx = qx.filter(location__in=('destination', 'user-entered', location))
        elif f == 'recommended' and request.user_profile:
            ctx['title'] = "Friends' Events"
            qx = qx.filter(
               recommendedevent__user_profile=request.user_profile
            ).distinct().order_by('event_date', 'event_start_time', 'pk')
            if settings.RECOMMENDED_EVENTS_FILTER_BY_LOCATION:
                qx = qx.filter(location__in=('destination', 'user-entered', location))            
        elif f == 'most-popular':
            ctx['title'] = 'Most Popular Events'
            qx = qx.filter(location=location).order_by('-tweet_count', 'event_date', 'event_start_time', 'pk')
        elif f == 'our-picks':
            ctx['title'] = 'Our Picks'
            qx = qx.filter(location=location).filter(is_homepage_worthy=True).order_by('event_date', 'event_start_time', 'pk')
        elif f == 'destination':
            ctx['title'] = 'Destination Events'
            qx = qx.filter(location='destination').order_by('-destination_timestamp', 'event_date', 'event_start_time', 'pk')
        elif f == 'today':
            ctx['title'] = "Today's Events"
            qx = qx.filter(location=location).filter(
                event_date=today
            ).order_by('event_date', 'event_start_time', 'pk')
        elif f == 'date':
            # if date not specified, use today
            dt = by_date and datetime.strptime(by_date, "%m/%d/%Y") or date.today()
            ctx['title'] = "Events on %s" % dt.strftime("%A, %b %d, %Y")
            qx = qx.filter(location=location).filter(
                event_date=dt
            ).order_by('event_date', 'event_start_time', 'pk')
        elif f == 'tomorrow':
            ctx['title'] = "Tomorrow's Events"
            qx = qx.filter(location=location).filter(
                event_date=today + timedelta(days=1)
            ).order_by('event_date', 'event_start_time', 'pk')
        elif f == 'this-week':
            ctx['title'] = "This Week"
            start = today + relativedelta(weekday=MO(-1)) # greater than or equal to last Monday
            end = start + relativedelta(weekday=SU(+1)) # less than or equal to this Sunday
            qx = qx.filter(location=location).filter(
                event_date__gte=start,
                event_date__lte=end
            ).order_by('event_date', 'event_start_time', 'pk')
            ctx['start'] = start
            ctx['end'] = end
        elif f == 'this-weekend':
            ctx['title'] = "This Weekend"
            start = today + relativedelta(weekday=MO(-1)) + relativedelta(weekday=FR) # greater than or equal to this Friday
            end = start + relativedelta(weekday=SU(+1)) # less than or equal to this Sunday
            qx = qx.filter(location=location).filter(
                event_date__gte=start,
                event_date__lte=end
            ).order_by('event_date', 'event_start_time', 'pk')
            ctx['start'] = start
            ctx['end'] = end
        elif f == 'next-week':
            ctx['title'] = "Next Week"
            start = today + relativedelta(weekday=MO(-1), weeks=1) # greater than or equal to next Monday
            end = start + relativedelta(weekday=SU(+1)) # less than or equal to next Sunday
            qx = qx.filter(location=location).filter(
                event_date__gte=start,
                event_date__lte=end
            ).order_by('event_date', 'event_start_time', 'pk')
            ctx['start'] = start
            ctx['end'] = end
        elif f == 'next-weekend':
            ctx['title'] = "Next Weekend"
            start = today + relativedelta(weekday=MO(-1), weeks=1) + relativedelta(weekday=FR) # greater than or equal to next Friday
            end = start + relativedelta(weekday=SU(+1)) # less than or equal to next Sunday
            qx = qx.filter(location=location).filter(
                event_date__gte=start,
                event_date__lte=end
            ).order_by('event_date', 'event_start_time', 'pk')
            ctx['start'] = start
            ctx['end'] = end
    else: # no filter
        if not any_loc:
            qx = qx.filter(location__in=('destination', 'user-entered', location))
    q = request.GET.get('q', '')
    if q:
        if not f:
            ctx['title'] = 'Events matching "%s"' % q
        ctx['q'] = q
        qx = qx.filter(
            Q(title__icontains=q) | 
            Q(description__icontains=q) |
            Q(venue__name__icontains=q) |
            Q(hashtag__icontains=q)
        )
        if 'sxsw' in q.lower():
            if not request.mobile:
                ctx['rpp'] = 100
            ctx['limit'] = 30 # start with events starting in 30 minutes or more
        if request.GET.get('page', '1') == '1':
            prefix = request.mobile and '/mobile/search' or '/help/search'
            qflat = q.replace('#', '').replace('@', '').lower()
            for k,v in settings.EVENT_SEARCH_NORMALIZATION.iteritems():
                qflat = qflat.replace(k, v)
            try:
                flaturl = u"%s/%s/" % (prefix, qflat.replace(' ', '-'))
                fp = Site.objects.get_current().flatpage_set.get(url=flaturl.lower())
                ctx['flatcontent'] = markdown(fp.content)
            except FlatPage.DoesNotExist:
                # if this search phrase has multiple space-separated keywords, 
                # look for a flatpage for just the first word.
                if ' ' in qflat:
                    x = qflat.split(' ')
                    if x[0]:
                        try:
                            flaturl = u"%s/%s/" % (prefix, x[0])
                            fp = Site.objects.get_current().flatpage_set.get(url=flaturl.lower())
                            ctx['flatcontent'] = markdown(fp.content)
                        except FlatPage.DoesNotExist:
                            pass
    if sort_by and 'checkins' in sort_by:
        ctx['limit'] = 30
    limit = request.GET.get('limit', ctx.get('limit', None))
    if limit:
        minutes = int(limit)
        now = td and (datetime.now() - td) or datetime.now()
        threshold = now - timedelta(minutes=minutes)
        time_threshold = threshold.time()
        _log.debug("Excluding %s events started before %s (limit = %s minutes, td = %s)", today, time_threshold, limit, td)
        qx = qx.exclude(
            # event_start_time__isnull=False, # has start time
            event_date=today, # happened today
            event_start_time__lt=time_threshold # already happened
        )
    if sort_by and sort_by in ('date', 'tweets', 'interested', 'new', 'checkins', 'mf_checkins', 'fm_checkins'):
        if 'checkins' in sort_by:
            # only include events that have no start time or that have already started or that are starting in the next 1 hour
            now = td and (datetime.now() - td) or datetime.now()
            threshold = now + timedelta(minutes=settings.CHECKIN_THRESHOLD_MINUTES) # 80 mins from now
            time_threshold = threshold.time()
            qx = qx.filter(
                Q(event_start_time__isnull=True) |
                Q(event_start_time__lte=time_threshold)
            )
        if sort_by == 'date':
            qx = qx.order_by('event_date', 'event_start_time', 'pk')
        elif sort_by == 'tweets':
            qx = qx.order_by('-tweet_count', 'event_date', 'event_start_time', 'pk')
        elif sort_by == 'interested':
            qx = qx.order_by('-stats__num_attendees', 'event_date', 'event_start_time', 'pk')
        elif sort_by == 'new':
            qx = qx.order_by('-pk', 'event_date', 'event_start_time')
        elif sort_by == 'checkins':
            qx = qx.order_by('-venue__fsq_checkins', 'event_date', 'event_start_time', 'pk')
            ctx['title'] = "Checkins Today"
        elif request.user.is_authenticated():
            if sort_by == 'mf_checkins': # where the ladies are          
                qx = qx.filter(venue__fsq_checkins__gt=0, venue__fsq_f__gt=1).order_by('venue__fsq_mf', 'event_date', 'event_start_time', 'pk')
                ctx['title'] = "Checkins Today"
            elif sort_by == 'fm_checkins': # where the gents are
                qx = qx.filter(venue__fsq_checkins__gt=0, venue__fsq_m__gt=1).order_by('venue__fsq_fm', 'event_date', 'event_start_time', 'pk')
                ctx['title'] = "Checkins Today"
        ctx['sort_by'] = sort_by
    return list_events(request, location, queryset=qx, extra_context=ctx, hide_top3=True)