telegram_bots/aggregator/views.py

61 lines
2.2 KiB
Python
Raw Normal View History

2019-01-27 21:20:09 +00:00
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.views import View
from django.views.generic import ListView, UpdateView, CreateView
from django.views.generic.detail import SingleObjectMixin
from aggregator.forms import AggregationSourceForm, AggregationSourceEditForm
from aggregator.models import AggregationSource
from cabinet.utils import CabinetViewMixin
class AggregationSourceListView(CabinetViewMixin, ListView):
template_name = 'cabinet/aggregator/source_list.html'
title = 'Aggregation source list'
sidebar_section = 'aggregator'
def get_queryset(self):
return AggregationSource.objects.filter(owner=self.request.user)
class AggregationSourceCreateView(CabinetViewMixin, CreateView):
template_name = 'cabinet/aggregator/source_form.html'
title = 'Create aggregation source'
sidebar_section = 'aggregator'
form_class = AggregationSourceForm
def form_valid(self, form):
form.instance.owner = self.request.user
form.instance.chat_id = form.cleaned_data['chat_id']
form.save()
return redirect('cabinet:aggregator:edit', pk=form.instance.pk)
class AggregationSourceUpdateView(CabinetViewMixin, UpdateView):
template_name = 'cabinet/aggregator/source_form.html'
title = 'Configure aggregation source'
sidebar_section = 'aggregator'
form_class = AggregationSourceEditForm
context_object_name = 'source'
def get_queryset(self):
return AggregationSource.objects.filter(owner=self.request.user)
def form_valid(self, form):
if 'chat_id' in form.cleaned_data:
form.instance.chat_id = form.cleaned_data['chat_id']
form.save()
return redirect('cabinet:aggregator:edit', pk=form.instance.pk)
class AggregationSourceDeleteView(LoginRequiredMixin, SingleObjectMixin, View):
def get_queryset(self):
return AggregationSource.objects.filter(owner=self.request.user)
def post(self, request, *args, **kwargs):
source = self.get_object()
messages.success(self.request, 'Source "{}" was successfully deleted'.format(source.title))
source.delete()
return redirect('cabinet:aggregator:index')