2012-11-20 03:47:19 -06:00
|
|
|
from django.views.generic import TemplateView
|
2012-12-11 13:04:03 -06:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.views.generic import DetailView
|
2012-12-16 08:10:00 -06:00
|
|
|
from django.db.models import Q
|
2013-02-02 09:12:40 -06:00
|
|
|
from django.contrib.gis.measure import D
|
2013-02-12 10:34:33 -06:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
2012-12-16 08:10:00 -06:00
|
|
|
|
|
|
|
from sesql.shortquery import shortquery
|
2012-11-20 03:47:19 -06:00
|
|
|
|
2012-12-22 13:32:11 -06:00
|
|
|
from leaflet_storage.models import Map
|
2013-02-02 09:12:40 -06:00
|
|
|
from leaflet_storage.forms import DEFAULT_CENTER
|
2012-11-20 03:47:19 -06:00
|
|
|
|
|
|
|
|
2013-02-12 10:34:33 -06:00
|
|
|
class PaginatorMixin(object):
|
|
|
|
per_page = 5
|
|
|
|
|
|
|
|
def paginate(self, qs):
|
|
|
|
paginator = Paginator(qs, self.per_page)
|
|
|
|
page = self.request.GET.get('p')
|
|
|
|
try:
|
|
|
|
qs = paginator.page(page)
|
|
|
|
except PageNotAnInteger:
|
|
|
|
# If page is not an integer, deliver first page.
|
|
|
|
qs = paginator.page(1)
|
|
|
|
except EmptyPage:
|
|
|
|
# If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
|
qs = paginator.page(paginator.num_pages)
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
|
|
|
class Home(TemplateView, PaginatorMixin):
|
2013-01-02 08:49:20 -06:00
|
|
|
template_name = "umap/home.html"
|
2012-12-22 13:32:11 -06:00
|
|
|
list_template_name = "leaflet_storage/map_list.html"
|
2012-11-20 03:47:19 -06:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2013-02-12 10:34:33 -06:00
|
|
|
qs = Map.objects.filter(center__distance_gt=(DEFAULT_CENTER, D(km=1)))
|
|
|
|
demo_map = None
|
|
|
|
if hasattr(settings, "UMAP_DEMO_PK"):
|
|
|
|
try:
|
|
|
|
demo_map = Map.objects.get(pk=settings.UMAP_DEMO_PK)
|
|
|
|
except Map.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
qs = qs.exclude(id=demo_map.pk)
|
|
|
|
maps = qs.order_by('-modified_at')[:50]
|
|
|
|
maps = self.paginate(maps)
|
|
|
|
|
2012-11-20 03:47:19 -06:00
|
|
|
return {
|
2012-12-12 06:13:14 -06:00
|
|
|
"maps": maps,
|
2013-05-01 17:54:40 -05:00
|
|
|
"demo_map": demo_map,
|
|
|
|
"DEMO_SITE": settings.UMAP_DEMO_SITE
|
2012-11-20 03:47:19 -06:00
|
|
|
}
|
|
|
|
|
2012-12-07 06:40:57 -06:00
|
|
|
def get_template_names(self):
|
|
|
|
"""
|
|
|
|
Dispatch template according to the kind of request: ajax or normal.
|
|
|
|
"""
|
|
|
|
if self.request.is_ajax():
|
|
|
|
return [self.list_template_name]
|
|
|
|
else:
|
|
|
|
return [self.template_name]
|
|
|
|
|
2012-11-20 03:47:19 -06:00
|
|
|
home = Home.as_view()
|
2012-12-11 13:04:03 -06:00
|
|
|
|
|
|
|
|
2013-02-12 10:34:33 -06:00
|
|
|
class About(Home):
|
|
|
|
|
|
|
|
template_name = "umap/about.html"
|
|
|
|
|
|
|
|
about = About.as_view()
|
|
|
|
|
|
|
|
|
|
|
|
class UserMaps(DetailView, PaginatorMixin):
|
2012-12-11 13:04:03 -06:00
|
|
|
model = User
|
|
|
|
slug_url_kwarg = 'username'
|
|
|
|
slug_field = 'username'
|
2012-12-22 13:32:11 -06:00
|
|
|
list_template_name = "leaflet_storage/map_list.html"
|
2012-12-11 13:04:03 -06:00
|
|
|
context_object_name = "current_user"
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2013-05-22 09:23:49 -05:00
|
|
|
maps = Map.objects.filter(Q(owner=self.object) | Q(editors=self.object)).distinct().order_by('-modified_at')[:30]
|
2013-02-12 10:34:33 -06:00
|
|
|
maps = self.paginate(maps)
|
2012-12-11 13:04:03 -06:00
|
|
|
kwargs.update({
|
|
|
|
"maps": maps
|
|
|
|
})
|
|
|
|
return super(UserMaps, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
def get_template_names(self):
|
|
|
|
"""
|
|
|
|
Dispatch template according to the kind of request: ajax or normal.
|
|
|
|
"""
|
|
|
|
if self.request.is_ajax():
|
|
|
|
return [self.list_template_name]
|
|
|
|
else:
|
|
|
|
return super(UserMaps, self).get_template_names()
|
|
|
|
|
|
|
|
user_maps = UserMaps.as_view()
|
2012-12-16 08:10:00 -06:00
|
|
|
|
|
|
|
|
2013-02-12 10:34:33 -06:00
|
|
|
class Search(TemplateView, PaginatorMixin):
|
2013-01-02 08:49:20 -06:00
|
|
|
template_name = "umap/search.html"
|
2012-12-22 13:32:11 -06:00
|
|
|
list_template_name = "leaflet_storage/map_list.html"
|
2012-12-16 08:10:00 -06:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2013-02-12 10:34:33 -06:00
|
|
|
q = self.request.GET.get('q')
|
|
|
|
maps = []
|
|
|
|
if q:
|
|
|
|
maps = shortquery(Q(fulltext__containswords=q))
|
|
|
|
maps = self.paginate(maps)
|
2012-12-16 08:10:00 -06:00
|
|
|
kwargs.update({
|
|
|
|
'maps': maps,
|
|
|
|
'q': q
|
2013-05-01 06:49:24 -05:00
|
|
|
})
|
2012-12-16 08:10:00 -06:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_template_names(self):
|
|
|
|
"""
|
|
|
|
Dispatch template according to the kind of request: ajax or normal.
|
|
|
|
"""
|
|
|
|
if self.request.is_ajax():
|
|
|
|
return [self.list_template_name]
|
|
|
|
else:
|
|
|
|
return super(Search, self).get_template_names()
|
|
|
|
|
|
|
|
search = Search.as_view()
|