Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

views.py 5.1KB

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