Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_object(self):
page_slug = self.kwargs.get('slug')
page = Page.objects.get(slug=page_slug, region=self.get_region())
version_num = int(self.kwargs['version'])
return page.pagetagset.versions.as_of(version=version_num)
def get_protected_object(self):
return self.object.page
def get_success_url(self):
# Redirect back to the file info page.
return reverse('pages:tags-history',
args=[self.kwargs['region'], self.kwargs['slug']])
class AddSingleTagView(PermissionRequiredMixin, RegionMixin, CreateObjectMixin, FormView):
"""
A convenience view that's used with the "add a new page to tag list" button.
"""
form_class = SingleTagForm
permission = 'pages.change_page'
def form_valid(self, form):
tag_slug = form.cleaned_data['tag_slug']
if not self.object:
# Page doesn't exist yet, so let's redirect to page creation screen
page_name = form.cleaned_data['page_name']
p = Page(name=page_name, region=self.get_region())
url = reverse('pages:edit', kwargs={'slug': page_name, 'region': self.get_region()})
return HttpResponseRedirect('%s?tag=%s' % (url, tag_slug))
import random
from django.db import connection
from django.views.generic import ListView, TemplateView
from regions.views import RegionMixin
from maps.widgets import InfoMap
from pages.models import Page
from utils.views import MultipleTypesPaginatedView, JSONResponseMixin
class BaseExploreList(RegionMixin, MultipleTypesPaginatedView):
context_object_name = 'pages'
items_per_page = 100
def get_object_lists(self):
qs = Page.objects.all()
region = self.get_region()
if region:
qs = Page.objects.filter(region=region)
# Exclude meta stuff
qs = qs.exclude(slug__startswith='templates/')
qs = qs.exclude(slug='templates')
qs = qs.exclude(slug='front page')
return [qs]
from django.utils.translation import ugettext as _
from django.views.generic.edit import FormView
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib import messages
from django.http import HttpResponseRedirect
from pages.models import Page
from regions.views import RegionMixin
from utils.views import AuthenticationRequired
from forms import CommentForm
class AddCommentView(RegionMixin, AuthenticationRequired, FormView):
form_class = CommentForm
def form_valid(self, form):
comment = form.cleaned_data.get('content')
self.page = Page.objects.get(slug=self.kwargs.get('slug'), region=self.get_region())
user_url = 'Users/%s' % self.request.user.username
user_info = '<a href="%s">%s</a>' % (user_url, self.request.user)
new_page_content = u"""%(current_html)s
<hr><p>
<em>%(datetime)s</em> %(comment)s —%(user_info)s
</p>""" % {
'current_html': self.page.content,
'datetime': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
'comment': comment,
def get_context_data(self, *args, **kwargs):
context = super(PageTagSetUpdateView, self).get_context_data(*args, **kwargs)
context['region'] = self.get_region()
def get_form_kwargs(self):
kwargs = super(PageTagSetUpdateView, self).get_form_kwargs()
# We need to pass the `region` to the PageTagSetForm.
kwargs['region'] = self.get_region()
return kwargs
def get_protected_object(self):
return self.object.page
class PageTagSetVersions(PageNotFoundMixin, RegionMixin, VersionsList):
def get_queryset(self):
page_slug = self.kwargs.get('slug')
try:
self.page = get_object_or_404(Page,
slug=page_slug, region=self.get_region())
return self.page.pagetagset.versions.all()
except PageTagSet.DoesNotExist:
return PageTagSet.versions.none()
def get_context_data(self, **kwargs):
context = super(PageTagSetVersions, self).get_context_data(**kwargs)
context['page'] = self.page
return context
class PageTagSetVersionDetailView(RegionMixin, DetailView):
page = Page(slug=slugify(self.kwargs['slug']), region=region)
latest_page = page.versions.most_recent()
# Need to set the pk on the dummy page for correct MapData lookup.
page.pk = latest_page.id
page.name = latest_page.name
self.mapdata = MapData(page=page, region=region)
return self.mapdata.versions.all()
def get_context_data(self, **kwargs):
context = super(MapVersionsList, self).get_context_data(**kwargs)
context['mapdata'] = self.mapdata
return context
class MapCompareView(RegionMixin, diff.views.CompareView):
model = MapData
def get_object(self):
region = self.get_region()
# A dummy page object.
page = Page(slug=slugify(self.kwargs['slug']), region=region)
latest_page = page.versions.most_recent()
# Need to set the pk on the dummy page for correct MapData lookup.
page.pk = latest_page.id
page.name = latest_page.name
return MapData(page=page, region=region)
def get_context_data(self, **kwargs):
# Send this in directly because we've wrapped InfoMap.
context = super(MapCompareView, self).get_context_data(**kwargs)
return [(obj.geom, popup_html(obj)) for obj in self.object_list]
def get_map(self):
map_objects = self.get_map_objects()
options = copy.deepcopy(getattr(settings,
'OLWIDGET_DEFAULT_OPTIONS', {}))
options.update({
'dynamic': self.dynamic,
'zoomToDataExtent': self.zoom_to_data,
'permalink': self.permalink,
'cluster': True
})
return InfoMap(map_objects, options=options)
class MapFullRegionView(CacheMixin, RegionMixin, MapBaseListView):
template_name = 'maps/mapdata_list.html'
dynamic = True
zoom_to_data = False
filter_by_zoom = True
def get_queryset(self):
queryset = super(MapFullRegionView, self).get_queryset()
# XXX TODO TEMPORARY HACK
queryset = queryset.exclude(page__pagetagset__tags__slug='zipcode')
queryset = queryset.exclude(page__pagetagset__tags__slug='supervisorialdistrict')
if not filter_by_zoom:
return queryset
# We order by -length so that the geometries are in that
# order when rendered by OpenLayers -- this creates the
# correct stacking order.
return filter_by_zoom(queryset, 12).order_by('-length')
class RandomTourJSON(JSONResponseMixin, RandomExploreList):
region_required = False
def get_context_data(self, *args, **kwargs):
context = super(RandomTourJSON, self).get_context_data(*args, **kwargs)
pages = context['pages']
context = {} # Reset context
# All we want is the URL of the pages here
context['urls'] = [p.get_absolute_url() for p in pages]
context['page_type'] = 'random'
context['random_seed'] = self.random_seed
return context
class RandomTourView(RegionMixin, TemplateView):
region_required = False
template_name = 'explore/random_tour.html'
class AlphabeticalExploreList(BaseExploreList):
def get_template_names(self):
if self.request.is_ajax():
return ['explore/page.html']
return ['explore/alphabetical_index.html']
def get_object_lists(self):
obj_lists = super(AlphabeticalExploreList, self).get_object_lists()
qs = obj_lists[0]
qs = qs.defer('content').select_related('region').order_by('name')
return [qs]
def create_object(self):
pagename = clean_name(self.kwargs['original_slug'])
content = _('<p>What do you know about %s?</p>') % pagename
if 'template' in self.request.GET:
try:
p = Page.objects.get(
slug=self.request.GET['template'],
region=self.get_region())
content = p.content
except Page.DoesNotExist:
pass
return Page(name=url_to_name(self.kwargs['original_slug']),
content=content, region=self.get_region())
class PageDeleteView(PermissionRequiredMixin, RegionMixin, DeleteView):
model = Page
context_object_name = 'page'
permission = 'pages.delete_page'
def get_success_url(self):
# Redirect back to the page.
region = self.get_region()
return reverse('pages:show',
kwargs={'region': region.slug, 'slug': self.kwargs.get('original_slug')})
class PageRevertView(PermissionRequiredMixin, RegionMixin, RevertView):
model = Page
context_object_name = 'page'
permission = 'pages.delete_page'
kwargs={'region': self.get_region().slug, 'slug': self.kwargs.get('slug')})
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
options = map_options_for_region(self.get_region())
context['show_revision'] = True
context['map'] = InfoMap([(self.object.geom, self.object.page.name)], options=options)
context['date'] = self.object.version_info.date
return context
def get_protected_object(self):
return self.object.page
class MapVersionsList(RegionMixin, VersionsList):
def get_queryset(self):
region = self.get_region()
# A dummy page object.
page = Page(slug=slugify(self.kwargs['slug']), region=region)
latest_page = page.versions.most_recent()
# Need to set the pk on the dummy page for correct MapData lookup.
page.pk = latest_page.id
page.name = latest_page.name
self.mapdata = MapData(page=page, region=region)
return self.mapdata.versions.all()
def get_context_data(self, **kwargs):
context = super(MapVersionsList, self).get_context_data(**kwargs)
context['mapdata'] = self.mapdata
return context
return ('<div>' + _('Page renamed to "%s"') + '</div>') % escape(self.new_pagename)
def get_success_url(self):
region = self.get_region()
messages.add_message(self.request, messages.SUCCESS,
self.success_msg())
# Redirect back to the page.
return reverse('pages:show',
kwargs={'region': region.slug, 'slug': self.new_pagename})
class MoveRegionForm(forms.Form):
new_region = forms.CharField(max_length=255, label=ugettext_lazy("Move to region (short name)"))
class PageMoveRegionView(PermissionRequiredMixin, RegionMixin, FormView):
form_class = MoveRegionForm
template_name = 'pages/page_move_region.html'
permission = 'is_staff'
def get_object(self):
page_slug = slugify(self.kwargs['slug'])
self.page = Page.objects.get(slug=page_slug, region=self.get_region())
return self.page
def form_valid(self, form):
from regions.utils import move_to_region
self.page = self.get_object()
self.new_region_slug = form.cleaned_data['new_region']
self.new_region = Region.objects.get(slug__iexact=self.new_region_slug)