from django.views.generic import UpdateView, DetailView, ListView from django.views.generic.edit import CreateView, DeleteView, UpdateView from django.contrib.auth.mixins import LoginRequiredMixin from django.urls import reverse, reverse_lazy from django.db.models import Q from .models import GTDNote, GTDProject, GTDOutcome, WiredNote, WiredPost from .forms import GTDNoteCreateForm, GTDNoteEditForm, GTDProjectCreateForm, GTDProjectUpdateForm, WiredNoteCreateForm, WiredNoteEditForm, WiredPostUpdateForm class GTDNoteCreateView(CreateView): model = GTDNote form_class = GTDNoteCreateForm template_name = "gtd/note_form.html" def get_form_kwargs(self): kwargs = super(GTDNoteCreateView, self).get_form_kwargs() return kwargs def get_success_url(self): if 'add_new' in self.request.POST: return reverse('gtd:note-create') else: if self.object.project: return reverse('gtd:project-detail', kwargs={"pk": self.object.project.pk}) else: return reverse('gtd:note-create') class GTDNoteUpdateView(UpdateView): model = GTDNote form_class = GTDNoteEditForm template_name = "gtd/note_form.html" def get_context_data(self, **kwargs): context = super(GTDNoteUpdateView, self).get_context_data(**kwargs) context['is_update'] = True return context def get_success_url(self): return reverse('gtd:project-detail', kwargs={"pk": self.object.project.pk}) class GTDNoteListView(ListView): model = GTDNote template_name = "gtd/note_list.html" def get_queryset(self): if self.kwargs['note_type']: print(self.kwargs['note_type']) note_type_reverse = dict((v, k) for k, v in GTDNote.NOTE_TYPE) note_type = note_type_reverse[self.kwargs['note_type'].title()] return GTDNote.objects.filter(note_type=note_type) return GTDNote.objects.all() def get_context_data(self, **kwargs): context = super(GTDNoteListView, self).get_context_data(**kwargs) context['note_types'] = GTDNote.NOTE_TYPE return context class GTDNoteTODOView(ListView): model = GTDNote template_name = "gtd/note_list.html" def get_queryset(self): return GTDNote.objects.filter(status=1) class GTDProjectCreateView(CreateView): model = GTDProject form_class = GTDProjectCreateForm template_name = "gtd/project_form.html" def get_form_kwargs(self): kwargs = super(GTDProjectCreateView, self).get_form_kwargs() return kwargs def get_success_url(self): if 'add_new' in self.request.POST: return reverse('gtd:project-create') else: return reverse('gtd:project-detail', kwargs={"pk": self.object.pk}) class GTDProjectUpdateView(UpdateView): model = GTDProject form_class = GTDProjectUpdateForm template_name = "gtd/project_form.html" def get_context_data(self, **kwargs): context = super(GTDProjectUpdateView, self).get_context_data(**kwargs) context['is_update'] = True return context class GTDProjectDetailView(DetailView): model = GTDProject template_name = "gtd/project_detail.html" def get_context_data(self, **kwargs): context = super(GTDProjectDetailView, self).get_context_data(**kwargs) return context class GTDProjectListView(ListView): model = GTDProject template_name = "gtd/project_list.html" def get_queryset(self, *args, **kwargs): if self.kwargs['project_type']: project_type_reverse = dict((v, k) for k, v in GTDProject.PROJECT_TYPE) project_type = project_type_reverse[self.kwargs['project_type']] return GTDProject.objects.filter(project_type=project_type) return GTDProject.objects.all() def get_context_data(self, **kwargs): context = super(GTDProjectListView, self).get_context_data(**kwargs) context['project_types'] = GTDProject.PROJECT_TYPE return context class GTDProjectDeleteView(DeleteView): # specify the model you want to use model = GTDProject success_url ="/gtd/projects" template_name = "gtd/confirm_delete.html" class WiredNoteCreateView(CreateView): model = WiredNote form_class = WiredNoteCreateForm template_name = "gtd/wirednote_form.html" def get_form_kwargs(self): kwargs = super(WiredNoteCreateView, self).get_form_kwargs() kwargs.update({'user': self.request.user}) return kwargs def get_success_url(self): if 'add_new' in self.request.POST: return reverse('gtd:wirednotes-create') else: if self.object.post: return reverse('gtd:wiredpost-detail', kwargs={"pk": self.object.post.pk}) else: return reverse('gtd:wirednotes-list') class WiredNoteUpdateView(UpdateView): model = WiredNote form_class = WiredNoteEditForm template_name = "gtd/wirednote_form.html" def get_form_kwargs(self): kwargs = super(WiredNoteUpdateView, self).get_form_kwargs() return kwargs def get_context_data(self, **kwargs): context = super(WiredNoteUpdateView, self).get_context_data(**kwargs) context['is_update'] = True return context def get_success_url(self): return reverse('gtd:wiredpost-detail', kwargs={"pk": self.object.post.pk}) class WiredNoteListView(ListView): model = WiredNote template_name = "gtd/wirednote_list.html" def get_queryset(self): if self.kwargs['status']: status_reverse = dict((v, k) for k, v in WiredNote.STATUS) status = status_reverse[self.kwargs['status'].title()] return WiredNote.objects.filter(status=status) return WiredNote.objects.all() def get_context_data(self, **kwargs): context = super(WiredNoteListView, self).get_context_data(**kwargs) context['note_statuses'] = WiredNote.STATUS return context class WiredNoteDeleteView(DeleteView): # specify the model you want to use model = WiredNote success_url ="/gtd/wirednote" template_name = "gtd/confirm_delete.html" class WiredPostListView(ListView): model = WiredPost template_name = 'gtd/wiredpost_table.html' def get_queryset(self): return WiredPost.objects.all().order_by("-needs_update") class WiredPostNewView(ListView): template_name = 'gtd/wiredpost_list.html' def get_queryset(self): return WiredPost.objects.filter(is_live=0) def get_context_data(self, **kwargs): context = super(WiredPostNewView, self).get_context_data(**kwargs) context['reviews'] = WiredNote.objects.filter(plan__in=[1,2], status__in=[0,1,2,3]) return context class WiredPostCreateView(CreateView): model = WiredPost fields = ['title', 'post_status', 'url', 'template_type', 'update_frequency', 'edit_url', 'date_last_pub'] template_name = "gtd/wiredpost_form.html" class WiredPostTodoView(ListView): template_name = 'gtd/wiredpost_table.html' def get_queryset(self): qs = WiredPost.objects.filter(Q(needs_update=True) | Q(is_live=0)).exclude(post_status=1) unsorted_results = qs.all() return sorted(unsorted_results, key=lambda a: a.days_overdue(), reverse=True) def get_context_data(self, **kwargs): context = super(WiredPostTodoView, self).get_context_data(**kwargs) context['reviews'] = WiredNote.objects.filter(plan__in=[1,2,3]).exclude(status__in=[4,5]) return context class WiredPostNotesView(DetailView): model = WiredPost template_name = "gtd/wiredpost_detail.html" def get_context_data(self, **kwargs): context = super(WiredPostNotesView, self).get_context_data(**kwargs) context['notes'] = self.get_object().wirednote_set.all().order_by("status") return context class WiredPostUpdateView(UpdateView): model = WiredPost form_class = WiredPostUpdateForm template_name = "gtd/wiredpost_form.html" def get_form_kwargs(self): kwargs = super(WiredPostUpdateView, self).get_form_kwargs() return kwargs def get_success_url(self): if 'add_new' in self.request.POST: return reverse('gtd:posts-create') else: return reverse('gtd:posts-detail', kwargs={"pk": self.object.pk}) class GTDNoteDeleteView(DeleteView): # specify the model you want to use model = GTDNote success_url ="/gtd/notes" template_name = "gtd/confirm_delete.html" class GTDOutcomeCreateView(CreateView): model = GTDOutcome template_name = "gtd/outcome_form.html" fields = ['title', 'body_markdown', 'date_goal', 'date_ended'] success_url = reverse_lazy("gtd:outcome-list") class GTDOutcomeUpdateView(UpdateView): model = GTDOutcome template_name = "gtd/outcome_form.html" fields = ['title', 'body_markdown', 'date_goal', 'date_ended'] success_url = reverse_lazy("gtd:outcome-list") def get_context_data(self, **kwargs): context = super(GTDOutcomeUpdateView, self).get_context_data(**kwargs) context['is_update'] = True return context class GTDOutcomeListView(ListView): model = GTDOutcome template_name = "gtd/outcome_list.html" class GTDOutcomeDeleteView(DeleteView): # specify the model you want to use model = GTDOutcome success_url ="/gtd/outcomes" template_name = "gtd/confirm_delete.html"