You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from django.db.models.loading import get_model
  2. from django.template.defaultfilters import slugify
  3. from django.http import HttpResponseRedirect
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.core.exceptions import ValidationError
  6. from django.core.urlresolvers import reverse
  7. from django.views import generic
  8. from django.views.generic import ListView
  9. from django.contrib import messages
  10. from oscar.core.validators import URLDoesNotExistValidator
  11. from oscar.apps.dashboard.pages import forms
  12. FlatPage = get_model('flatpages', 'FlatPage')
  13. Site = get_model('sites', 'Site')
  14. class PageListView(ListView):
  15. """
  16. View for listing all existing flatpages.
  17. """
  18. template_name = 'dashboard/pages/index.html'
  19. current_view = 'dashboard:pages-index'
  20. model = FlatPage
  21. form_class = forms.PageSearchForm
  22. paginate_by = 25
  23. desc_template = u'%(main_filter)s %(title_filter)s'
  24. def get_queryset(self):
  25. """
  26. Get queryset of all flatpages to be displayed. If a
  27. search term is specified in the search form, it will be used
  28. to filter the queryset.
  29. """
  30. self.desc_ctx = {
  31. 'main_filter': _('All pages'),
  32. 'title_filter': '',
  33. }
  34. queryset = self.model.objects.all().order_by('title')
  35. self.form = self.form_class(self.request.GET)
  36. if not self.form.is_valid():
  37. return queryset
  38. data = self.form.cleaned_data
  39. if data['title']:
  40. queryset = queryset.filter(title__icontains=data['title'])
  41. self.desc_ctx['title_filter'] = _(" with title containing '%s'") % data['title']
  42. return queryset
  43. def get_context_data(self, **kwargs):
  44. """
  45. Get context data with *form* and *queryset_description* data
  46. added to it.
  47. """
  48. context = super(PageListView, self).get_context_data(**kwargs)
  49. context['form'] = self.form
  50. context['queryset_description'] = self.desc_template % self.desc_ctx
  51. return context
  52. class PageCreateView(generic.CreateView):
  53. """
  54. View for creating a flatpage from dashboard.
  55. """
  56. template_name = 'dashboard/pages/update.html'
  57. model = FlatPage
  58. form_class = forms.PageUpdateForm
  59. context_object_name = 'page'
  60. def get_context_data(self, **kwargs):
  61. """
  62. Get context data with additional *title* object.
  63. """
  64. ctx = super(PageCreateView, self).get_context_data(**kwargs)
  65. ctx['title'] = _('Create New Page')
  66. return ctx
  67. def form_valid(self, form):
  68. """
  69. Store new flatpage from form data. Checks wether a site
  70. is specified for the flatpage or sets the current site by
  71. default. Additionally, if URL is left blank, a slugified
  72. version of the title will be used as URL after checking
  73. if it is valid.
  74. """
  75. # if no URL is specified, generate from title
  76. page = form.save(commit=False)
  77. if not page.url:
  78. page.url = '/%s/' % slugify(page.title)
  79. try:
  80. URLDoesNotExistValidator()(page.url)
  81. # use current site as default for new page
  82. page.save()
  83. page.sites.add(Site.objects.get_current())
  84. return HttpResponseRedirect(self.get_success_url(page))
  85. except ValidationError:
  86. pass
  87. ctx = self.get_context_data()
  88. ctx['form'] = form
  89. return self.render_to_response(ctx)
  90. def get_success_url(self, page):
  91. messages.success(self.request, _("Created new page '%s'") % page.title)
  92. return reverse('dashboard:page-list')
  93. class PageUpdateView(generic.UpdateView):
  94. """
  95. View for updating flatpages from the dashboard.
  96. """
  97. template_name = 'dashboard/pages/update.html'
  98. model = FlatPage
  99. form_class = forms.PageUpdateForm
  100. context_object_name = 'page'
  101. def get_context_data(self, **kwargs):
  102. """
  103. Get context data with additional *title* and *page* objects attached.
  104. """
  105. ctx = super(PageUpdateView, self).get_context_data(**kwargs)
  106. ctx['title'] = _('Update Page')
  107. return ctx
  108. def form_valid(self, form):
  109. """
  110. Store updated flatpage from form data. Checks wether a site
  111. is specified for the flatpage or sets the current site by
  112. default.
  113. """
  114. page = form.save(commit=False)
  115. if not page.sites.count():
  116. page.sites.add(Site.objects.get_current())
  117. page.save()
  118. return HttpResponseRedirect(self.get_success_url())
  119. def get_success_url(self):
  120. """
  121. Get URL to redirect to when updating page was successful.
  122. """
  123. messages.success(self.request, _("Updated page '%s'") % self.object.title)
  124. return reverse('dashboard:page-list')
  125. class PageDeleteView(generic.DeleteView):
  126. """
  127. View for deleting flatpages from the dashboard. It performs an
  128. 'are you sure?' check before actually deleting the page.
  129. """
  130. template_name = 'dashboard/pages/delete.html'
  131. model = FlatPage
  132. def get_success_url(self):
  133. """
  134. Get URL to redirect to when deleting page is succesful.
  135. """
  136. messages.success(self.request, _("Deleted page '%s'") % self.object.title)
  137. return reverse('dashboard:page-list')