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)
def process_request(self, request): if request.user.is_authenticated(): request.user_profile = get_or_none(UserProfile.objects.active(), user=request.user.pk) if request.user_profile: pending_friendship_requests = PendingFriendship.objects.filter( invitee_profile__pk=request.user_profile.pk ).count() request.pending_friendship_requests = pending_friendship_requests else: logout(request) return HttpResponseRedirect(reverse("home")) else: request.user_profile = None request.pending_friendship_requests = 0
def twitter_profile_url(user): """Return Twitter profile URL for the given user""" from twitter.models import TwitterProfile sfx = key_suffix(u"twitter_profile", user.pk) key = short_key(u"twitter_profile_url:%s" % sfx) value = cache.cache.get(key, None) if value is None: p = get_or_none(TwitterProfile.objects.active(), user_profile__user__pk=user.pk) if p: value = u"http://twitter.com/%s" % p.screen_name else: value = user.get_profile().get_absolute_url() cache.cache.set(key, value) return value
def twitter_profile_url(user): """Return Twitter profile URL for the given user""" from twitter.models import TwitterProfile sfx = key_suffix(u'twitter_profile', user.pk) key = short_key(u'twitter_profile_url:%s' % sfx) value = cache.cache.get(key, None) if value is None: p = get_or_none(TwitterProfile.objects.active(), user_profile__user__pk=user.pk) if p: value = u'http://twitter.com/%s' % p.screen_name else: value = user.get_profile().get_absolute_url() cache.cache.set(key, value) return value
def get_tiny_url(url): tiny_link = None d = u'' params = u'' try: key = shorten_key(u'tiny-%s' % url) tiny_link = cache.get(key, None) if tiny_link is None: tiny_link = get_or_none(KeyValue.objects, key=url, category='tinyurl') if tiny_link: tiny_link = tiny_link.value if tiny_link is None: tiny_url_api = getattr(settings, 'TINY_URL_API', 'http://tinyurl.com/api-create.php') if "bit.ly" in tiny_url_api: params = { 'longUrl': url, 'login': settings.BITLY_API_LOGIN, 'apiKey': settings.BITLY_API_KEY, 'version': getattr(settings, 'BITLY_API_VERSION', '2.0.1'), 'format': 'json', } else: params = {'url': url} data = urllib.urlencode(params) tiny_link = urllib2.urlopen(tiny_url_api, data).read().strip() if "bit.ly" in tiny_url_api: d = json.loads(tiny_link) if not d: return url tiny_link = d['results'][url]['shortUrl'] if len(tiny_link) > 25: return url KeyValue.objects.get_or_create(key=url, category='tinyurl', defaults=dict(value=tiny_link)) cache.set(key, tiny_link, 3600 * 12) return tiny_link except Exception, e: _log.exception(e) _log.warning( "Failed to get short URL for %s\nwith error: %s\nd=%s\nparams=%s", url, e, d, params) return tiny_link or url
def get_tiny_url(url): tiny_link = None d = u"" params = u"" try: key = shorten_key(u"tiny-%s" % url) tiny_link = cache.get(key, None) if tiny_link is None: tiny_link = get_or_none(KeyValue.objects, key=url, category="tinyurl") if tiny_link: tiny_link = tiny_link.value if tiny_link is None: tiny_url_api = getattr(settings, "TINY_URL_API", "http://tinyurl.com/api-create.php") if "bit.ly" in tiny_url_api: params = { "longUrl": url, "login": settings.BITLY_API_LOGIN, "apiKey": settings.BITLY_API_KEY, "version": getattr(settings, "BITLY_API_VERSION", "2.0.1"), "format": "json", } else: params = {"url": url} data = urllib.urlencode(params) tiny_link = urllib2.urlopen(tiny_url_api, data).read().strip() if "bit.ly" in tiny_url_api: d = json.loads(tiny_link) if not d: return url tiny_link = d["results"][url]["shortUrl"] if len(tiny_link) > 25: return url KeyValue.objects.get_or_create(key=url, category="tinyurl", defaults=dict(value=tiny_link)) cache.set(key, tiny_link, 3600 * 12) return tiny_link except Exception, e: _log.exception(e) _log.warning("Failed to get short URL for %s\nwith error: %s\nd=%s\nparams=%s", url, e, d, params) return tiny_link or url
def calendar(request, limit=10, template='event/tags/event_calendar.html'): """Render event calendar for the logged in user""" from event.templatetags.eventtags import gcal_url try: ctx = {} if not request.user.is_authenticated: if request.is_ajax(): raise Http404 return HttpResponseRedirect("%s?next=%s" % (reverse("login"), reverse("list_events"))) ctx['next'] = request.META.get('HTTP_REFERER', reverse("list_events")) event_id = request.REQUEST.get("event_id", ""); # If there's an event_id, we are on the event detail page event = event_id and get_or_none(Event.objects.active(), pk=event_id) or None events = Event.objects.active().filter( attendee__attendee_profile=request.user_profile ).distinct().order_by('event_date', 'event_start_time', 'pk') if settings.CALENDAR_FILTER_BY_LOCATION: events = events.filter(location__in=('destination', 'user-entered', request.location)) ctx['calendar_count'] = events.count() if limit: events = events[:int(limit)] ctx['events'] = events if event: ctx['next'] = event.get_absolute_url() ctx['event'] = event ctx['gcal_url'] = gcal_url(event) if request.user_profile and request.user_profile.attendee_set.filter(event=event).count(): ctx['show_remove_event'] = True else: ctx['show_add_event'] = True ctx['is_owner'] = request.user.is_authenticated() and \ ((event.creator and request.user.id == event.creator.user.id) or \ (request.user.id == event.artist.user_profile.user.id)) ctx['is_admin'] = request.user.has_perm('event.can_manage_events') return render_view(request, template, ctx) except Exception, e: _log.exception(e) raise