summaryrefslogtreecommitdiff
path: root/app/gtd/views.py
blob: 709766ea2baa219c6041a4f0188b5b3336774af8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from django.views.generic import UpdateView, DetailView, ListView, CreateView, DeleteView, RedirectView
from django.views.generic.base import TemplateView
from django.urls import reverse, reverse_lazy
from django.db.models import Q

from .models import (
    GTDNote,
    GTDProject,
    GTDOutcome,
    WiredNote,
    WiredPost,
    NoteType
)

from .forms import (
    GTDNoteCreateForm,
    GTDNoteEditForm,
    GTDProjectCreateForm,
    GTDProjectUpdateForm,
    WiredNoteEditForm,
    WiredPostUpdateForm
)


class GTDNoteCreateView(CreateView):
    model = GTDNote
    form_class = GTDNoteCreateForm
    template_name = "gtd/note_form.html"

    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):
        if self.object.project:
            return reverse('gtd:project-detail', kwargs={"pk": self.object.project.pk})
        else:
            return reverse('gtd:note-list', kwargs={"pk": self.object.project.pk})


class GTDNoteListView(ListView):
    model = GTDNote
    template_name = "gtd/note_list.html"

    def get_queryset(self):
        project = self.request.GET.get("project", False)
        if self.kwargs['note_type']:
            note_type = NoteType.string_to_int(self.kwargs['note_type'].title())
            if project:
                return GTDNote.objects.filter(note_type=note_type, project__title=project, status__in=[0,1])
            return GTDNote.objects.filter(note_type=note_type, status__in=[0,1])
        else:
            if project:
                return GTDNote.objects.filter(project__title=project, status__in=[0,1])
        return GTDNote.objects.filter(status__in=[0,1])

    def get_context_data(self, **kwargs):
        context = super(GTDNoteListView, self).get_context_data(**kwargs)
        context['note_types'] = NoteType
        context['projects'] = GTDProject.objects.all()
        context['project'] = self.request.GET.get("project", False)
        return context


class GTDNoteTODOView(ListView):
    model = GTDNote
    template_name = "gtd/note_list.html"

    def get_queryset(self):
        project = self.request.GET.get("project", False)
        # need to get wired notes and gtd notes and blend them by
        # date created maybe?
        if project:
            return GTDNote.objects.filter(status__in=[0, 1], note_type__in=[NoteType.REMINDER, NoteType.ACTION],project__title=project)
        return GTDNote.objects.filter(status__in=[0, 1], note_type__in=[NoteType.REMINDER, NoteType.ACTION])

    def get_context_data(self, **kwargs):
        context = super(GTDNoteTODOView, self).get_context_data(**kwargs)
        context['projects'] = GTDProject.objects.all()
        context['project'] = self.request.GET.get("project", False)
        return context


class GTDProjectCreateView(CreateView):
    model = GTDProject
    form_class = GTDProjectCreateForm
    template_name = "gtd/project_form.html"

    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)
        context['note_set'] = GTDNote.objects.filter(project=self.object).order_by('note_type')
        context['projects'] = GTDProject.objects.all()
        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
    fields = ['title', 'url', 'body_markdown', 'post']
    template_name = "gtd/wirednote_form.html"

    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_context_data(self, **kwargs):
        context = super(WiredNoteUpdateView, self).get_context_data(**kwargs)
        context['is_update'] = True
        return context

    def get_success_url(self):
        if self.object.post:
            return reverse('gtd:wiredpost-detail', kwargs={"pk": self.object.post.pk})
        else:
            return reverse('gtd:wirednote-list', kwargs={"pk": self.object.project.pk})


class WiredNoteListView(ListView):
    model = WiredNote
    template_name = "gtd/wirednote_list.html"

    def get_queryset(self):
        wiredpost = self.request.GET.get("post", False)
        if self.kwargs['status']:
            status_reverse = dict((v, k) for k, v in WiredNote.STATUS)
            status = status_reverse[self.kwargs['status'].title()]
            if wiredpost:
                return WiredNote.objects.filter(status=status, post__title=wiredpost)
            return WiredNote.objects.filter(status=status)
        else:
            if wiredpost:
                return WiredNote.objects.filter(post__title=wiredpost)
        return WiredNote.objects.all()

    def get_context_data(self, **kwargs):
        context = super(WiredNoteListView, self).get_context_data(**kwargs)
        context['note_statuses'] = WiredNote.STATUS
        context['posts'] = WiredPost.objects.all()
        context['post'] = self.request.GET.get("post", False)
        return context


class WiredNoteDeleteView(DeleteView):
    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 WiredPostUpdateOverdueView(RedirectView):
    permanent = False
    url = "/gtd/wiredposts"
    query_string = True

    def get_redirect_url(self, *args, **kwargs):
        for post in WiredPost.objects.all():
            post.set_needs_update()
        return super().get_redirect_url(*args, **kwargs)


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")
        context['posts'] = WiredPost.objects.all()
        return context


class WiredPostUpdateView(UpdateView):
    model = WiredPost
    form_class = WiredPostUpdateForm
    template_name = "gtd/wiredpost_form.html"

    def get_success_url(self):
        if 'add_new' in self.request.POST:
            return reverse('gtd:posts-create')
        else:
            return reverse('gtd:wiredpost-list')


class WiredGenerateDealCodeView(TemplateView):
    template_name = "gtd/deal_code.html"


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"




from datetime import datetime
from bs4 import BeautifulSoup
import requests

def scrape_date(url):
    return time

class UpdateDateView(RedirectView):
    permanent = False
    url = "/gtd/wiredposts"
    query_string = True

    def get_redirect_url(self, *args, **kwargs):
        for post in WiredPost.objects.all():
            if post.url:
                content = requests.get(post.url)
                soup = BeautifulSoup(content.text, 'html.parser')
                time = datetime.strptime(soup.time['datetime'][:-6], "%Y-%m-%dT%H:%M:%S").date()
                if time != post.date_last_pub:
                    post.date_last_pub = time
                    post.save()
        return super().get_redirect_url(*args, **kwargs)