Created
June 30, 2011 11:35
-
-
Save atroche/1056052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import (request, redirect, render_template, abort, url_for, | |
current_app) | |
from mongoengine.queryset import DoesNotExist | |
from streetlife import current_user as user | |
from streetlife.frontend.conversation.forms import BucketMessageForm | |
from streetlife.frontend.home.models import FollowedMessages | |
from streetlife.helpers.views import login_required, BaseView | |
def get_category(args): | |
""" | |
Return the first valid category from the request args, if there isn't any | |
valid category return None. | |
""" | |
for arg in args: | |
if arg in ['deals', 'events', 'classified']: | |
return arg | |
return None | |
class HomeView(BaseView): | |
methods = ['GET', 'POST'] | |
def get(self, *args, **kwargs): | |
return self.render_template(self.template_name) | |
def post(self, *args, **kwargs): | |
form = self.context['form'] | |
if form.validate(): | |
form.save() | |
return redirect(self.redirect_url) | |
return self.render_template(self.template_name) | |
def default_context(self, *args, **kwargs): | |
context = kwargs.get('context', {}) | |
query = kwargs.get('query', {}) | |
page = kwargs.get('page', 1) | |
category = get_category(request.args) | |
query['category'] = category | |
active_bucket = context['active_bucket'] | |
buckets = [active_bucket] if active_bucket else user.buckets | |
base_context = { | |
'form': BucketMessageForm(request.form, buckets=buckets), | |
'category': get_category(request.args), | |
'messages': user.get_messages(**query).paginate(page, | |
current_app.config['PER_PAGE']), | |
'new_message': kwargs.get('new_message', False) | |
} | |
context.update(base_context) | |
return context | |
class Index(HomeView): | |
template_name = "home/index.html" | |
def default_context(self, *args, **kwargs): | |
self.redirect_url = url_for('home.index') | |
points_list = [point_list for point_list | |
in [user_bucket.locations_points | |
for user_bucket in user.buckets]] | |
map_points = [point for sublist in points_list for point in sublist] | |
search_scope = "in News Feed" | |
context = {'active_bucket': None, | |
'active_location': None, | |
'map_points': map_points, | |
'search_scope': search_scope} | |
return super(Index, self).default_context(self, context=context, | |
*args, **kwargs) | |
class Bucket(HomeView): | |
template_name = "home/bucket.html" | |
def default_context(self, *args, **kwargs): | |
try: | |
bucket = user.get_bucket(kwargs.get('bucket_slug')) | |
except DoesNotExist: | |
abort(404) | |
search_scope = "within " + bucket.name | |
self.redirect_url = url_for("home.bucket", | |
bucket_slug=bucket.slug) | |
map_points = bucket.locations_points | |
context = {'active_bucket': bucket, | |
'active_location': None, | |
'map_points': map_points, | |
'search_scope': search_scope} | |
return super(Bucket, self).default_context(self, context=context, | |
*args, **kwargs) | |
class BucketLocation(HomeView): | |
template_name = "home/bucket_location.html" | |
def default_context(self, *args, **kwargs): | |
bucket_slug = kwargs.get("bucket_slug", "") | |
location_slug = kwargs.get("location_slug", "") | |
try: | |
bucket, location = user.get_bucket_location(bucket_slug, | |
location_slug) | |
except DoesNotExist: | |
abort(404) | |
search_scope = "in " + location.name | |
self.redirect_url = url_for("home.bucket_location", | |
bucket_slug=bucket.slug, | |
location_slug=location.slug) | |
map_points = [location.point] | |
context = {'active_bucket': bucket, | |
'active_location': location, | |
'map_points': map_points, | |
'search_scope': search_scope} | |
return super(BucketLocation, self).default_context(self, | |
context=context, *args, **kwargs) | |
class Followed(BaseView): | |
template_name = "home/followed.html" | |
def get(self, *args, **kwargs): | |
return self.render_template(self.template_name) | |
def default_context(self, *args, **kwargs): | |
page = kwargs.get('page', 1) | |
try: | |
followed_messages_object = FollowedMessages.objects.get( | |
user=user.id) | |
messages = FollowedMessages.objects().paginate_field('messages', | |
followed_messages_object.id, page, | |
current_app.config['PER_PAGE']) | |
except DoesNotExist: | |
messages = [] | |
return {'messages': messages, 'active_bucket': 'followed'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment