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.db.models import Q from django.urls import reverse from .models import Post from .forms import PostUpdateForm from notes.models import Note """ TODO: crawl the RSS feed and compare url to stored url if it matches update the pub_date. Then scrape the HTML. Then parse the scraped html for aff links and create products from them """ class PostListView(LoginRequiredMixin, ListView): model = Post template_name = 'posts/post_table.html' def get_queryset(self): return Post.objects.filter(user=self.request.user).order_by("-needs_update") class PostNewView(LoginRequiredMixin, ListView): template_name = 'posts/post_list.html' def get_queryset(self): return Post.objects.filter(user=self.request.user).filter(is_live=0) def get_context_data(self, **kwargs): context = super(PostNewView, self).get_context_data(**kwargs) context['reviews'] = Note.objects.filter(plan__in=[1,2], status__in=[0,1,2,3]) return context class PostTodoView(LoginRequiredMixin, ListView): template_name = 'posts/post_table.html' def get_queryset(self): qs = Post.objects.filter(user=self.request.user) qs = qs.filter(Q(needs_update=True) | Q(is_live=0)) unsorted_results = qs.all() return sorted(unsorted_results, key=lambda a: a.days_overdue(), reverse=True) def get_context_data(self, **kwargs): context = super(PostTodoView, self).get_context_data(**kwargs) context['reviews'] = Note.objects.filter(plan__in=[1,2,3]).exclude(status=4) return context class PostNotesView(LoginRequiredMixin, DetailView): model = Post class PostUpdateView(LoginRequiredMixin, UpdateView): model = Post form_class = PostUpdateForm def get_form_kwargs(self): kwargs = super(PostUpdateView, 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('posts:create') else: return reverse('posts:detail', kwargs={"pk": self.object.pk})