Dénote

Dénote Git Source Tree

Root/denote/views.py

1# -*- coding: utf-8 -*-
2"""
3 Copyright 2015 Grégory Soutadé
4
5 This file is part of Dénote.
6
7 Dénote is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Dénote is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Dénote. If not, see <http://www.gnu.org/licenses/>.
19"""
20
21import os
22from datetime import datetime
23
24from django.http import HttpResponseRedirect, HttpResponse, Http404, HttpResponseForbidden
25from django.contrib.auth.decorators import login_required
26from django.contrib.auth import authenticate, login, logout
27from django.shortcuts import render
28
29from denote.models import *
30from denote.forms import *
31from denote.search import *
32
33def index(request):
34 if request.user.is_authenticated():
35 return user_home(request, request.user)
36
37 login_failed = False
38 if 'login' in request.POST:
39 user = authenticate(username=request.POST['login'], password=request.POST['password'])
40 if user is None:
41 login_failed = True
42 else:
43 login(request, user)
44 if 'next' in request.GET:
45 return HttpResponseRedirect(request.GET['next'])
46 elif 'next' in request.POST:
47 return HttpResponseRedirect(request.POST['next'])
48 else:
49 return user_home(request, request.user)
50
51 c = {
52 'login_failed' : login_failed,
53 'nb_people_registered' : User.objects.all().count()
54 }
55
56 return render(request, 'login.html', c)
57
58def disconnect(request):
59 user = request.user
60
61 if not user is None:
62 logout(request)
63
64 return HttpResponseRedirect('/')
65
66def new_user(request):
67 login_val = 'login' in request.POST and request.POST['login'] or ''
68 password = 'password' in request.POST and request.POST['password'] or ''
69
70 if request.method == 'POST':
71 if 'add' in request.POST:
72 form = UserForm(request.POST)
73 if form.is_valid():
74 form = form.save()
75 user = User.objects.get(pk=form.id)
76 user.set_password(request.POST['password'])
77 user.save()
78 user = authenticate(username=user.username, password=request.POST['password'])
79 login(request, user)
80 return user_home(request, user)
81 else:
82 return HttpResponseRedirect('/')
83 else:
84 form = UserForm()
85
86 c = {'login' : login_val, 'password' : password, 'form': form}
87
88 return render(request, 'add_user.html', c)
89
90@login_required
91def edit_user(request):
92 user = request.user
93 edited = False
94
95 if request.method == 'POST':
96 if 'edit' in request.POST:
97 template_id = request.POST['default_template']
98 if template_id == '-1':
99 template = None
100 else:
101 try:
102 template= Template.objects.get(author=user.id, id=template_id)
103 except:
104 template = None
105 user.default_template = template
106 form = UserForm(request.POST, instance=user, initial={'password':''})
107 if form.is_valid():
108 form.save()
109 if request.POST['password'] != '':
110 user.set_password(request.POST['password'])
111 user.save()
112 edited = True
113 else:
114 if 'delete' in request.POST:
115 logout(request)
116 User.objects.filter(pk=user.id).delete()
117 return HttpResponseRedirect('/')
118 else:
119 login = 'login' in request.POST and request.POST['login'] or ''
120 form = UserForm(instance=user, initial={'password':'', 'login':login})
121
122 templates = Template.objects.filter(author=user.id).order_by('name')
123 templates_by_name = []
124 for template in templates:
125 t = {}
126 t['name'] = template.name
127 t['id'] = template.id
128 templates_by_name.append(t)
129
130 c = {'authenticated' : True, 'user_to_edit' : user, 'form' : form, 'edited' : edited}
131 c['templates_by_name'] = templates_by_name
132
133 return render(request, 'edit_user.html', c)
134
135def _prepare_template_context(user):
136 categories = Category.objects.filter(author=user.id).order_by('name')
137
138 templates = Template.objects.filter(author=user.id).order_by('name')
139 templates_by_name = []
140 for template in templates:
141 t = {}
142 t['name'] = template.name
143 t['id'] = template.id
144 templates_by_name.append(t)
145
146 context = {
147 'user': user,
148 'authenticated' : True,
149 'categories': categories,
150 'templates_by_name': templates_by_name,
151 }
152
153 return context
154
155def _prepare_note_context(user):
156 if not user.is_authenticated():
157 return {
158 'authenticated' : False,
159 }
160
161 categories = Category.objects.filter(author=user.id).order_by('name')
162 notes_by_category = []
163 need_refresh = False
164 for category in categories:
165 meta_note = {}
166 meta_note['category'] = category.name
167 meta_note['category_id'] = category.id
168 meta_note['notes'] = Note.objects.filter(author=user,category=category).order_by('-modified_date')
169 if meta_note['notes']:
170 notes_by_category.append(meta_note)
171 else:
172 category.delete()
173 need_refresh = True
174 if need_refresh:
175 categories = Category.objects.filter(author=user.id).order_by('name')
176 notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date')
177
178 templates = Template.objects.filter(author=user.id).order_by('name')
179 templates_by_name = []
180 for template in templates:
181 t = {}
182 t['name'] = template.name
183 t['id'] = template.id
184 templates_by_name.append(t)
185
186 context = {
187 'user': user,
188 'authenticated' : True,
189 'notes_by_category': notes_by_category,
190 'categories': categories,
191 'notes_without_category': notes_without_category,
192 'templates_by_name': templates_by_name,
193 }
194
195 return context
196
197@login_required
198def user_home(request, user):
199 context = _prepare_note_context(user)
200
201 notes = Note.objects.filter(author=user.id).order_by('-modified_date')[:20]
202 context['notes'] = notes
203 context['note_form'] = NoteForm()
204
205 return render(request, 'user_index.html', context)
206
207@login_required
208def add_note(request):
209 user = request.user
210
211 if 'add' in request.POST:
212 note = Note(author=user, created_date=datetime.now())
213 note.category = manage_category(user, request.POST['category'])
214 form = NoteForm(request.POST, instance=note)
215 if form.is_valid():
216 form.save()
217 return HttpResponseRedirect('/note/%d' % (note.id))
218 elif 'cancel' in request.POST:
219 return HttpResponseRedirect('/')
220 else:
221 note = None
222 template_id = request.POST['template']
223 if template_id != '-1':
224 note = Template.objects.get(id=template_id, author=user.id)
225 if not note:
226 note = Note(visibility=user.home_notes_visibility)
227
228 form = NoteForm(instance=note)
229
230 context = _prepare_note_context(user)
231 context['note_form'] = form
232 context['note'] = None
233
234 if note.category:
235 context['category'] = note.category.name
236
237 return render(request, 'user_note.html', context)
238
239@login_required
240def note(request, note_id):
241 user = request.user
242
243 note = Note.objects.get(pk=note_id, author=user)
244
245 if note is None:
246 raise Http404
247
248 form = NoteForm(instance=note)
249 if request.method == 'POST':
250 if 'edit' in request.POST:
251 note.category = manage_category(user, request.POST['category'])
252 form = NoteForm(request.POST, instance=note)
253 if form.is_valid():
254 form.save()
255 else:
256 if 'delete' in request.POST:
257 note.delete()
258 return HttpResponseRedirect('/')
259
260 context = _prepare_note_context(user)
261 context['note'] = note
262 context['note_form'] = form
263
264 return render(request, 'user_note.html', context)
265
266def public_note(request, user_id, note_id):
267 user = request.user
268
269 try:
270 note = Note.objects.get(pk=note_id, author=user_id)
271 except:
272 raise Http404
273
274 if note is None:
275 raise Http404
276
277 if not user or not user.is_authenticated():
278 if note.visibility != Note.PUBLIC:
279 return HttpResponseForbidden()
280 else:
281 if note.visibility == Note.PRIVATE and\
282 user_id != user.id:
283 return HttpResponseForbidden()
284
285 if user.is_authenticated():
286 public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.REGISTERED).order_by('-modified_date')
287 else:
288 public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.PUBLIC).order_by('-modified_date')
289
290 context = _prepare_note_context(user)
291 context['note'] = note
292 context['public_notes'] = public_notes
293
294 return render(request, 'public_note.html', context)
295
296def public_notes(request):
297 user = request.user
298
299 if user.is_authenticated():
300 public_notes = Note.objects.filter(visibility__gte=Note.REGISTERED).order_by('-modified_date')
301 else:
302 public_notes = Note.objects.filter(visibility__gte=Note.PUBLIC).order_by('-modified_date')
303
304 context = _prepare_note_context(user)
305 context['notes'] = public_notes[:50]
306 context['public_notes'] = public_notes[:50]
307
308 return render(request, 'public_notes.html', context)
309
310@login_required
311def edit_category(request, category_id):
312 user = request.user
313
314 category = Category.objects.get(pk=category_id, author=user)
315
316 if category is None:
317 raise Http404
318
319 if request.method == 'POST':
320 if not 'new_cat_name' in request.POST or \
321 not request.POST['new_cat_name']:
322 return HttpResponseRedirect('/')
323 category.name = request.POST['new_cat_name'].strip()
324 if len(category.name) > 50: category.name = category.name[:50]
325 category.author = user
326 try:
327 category.save()
328 except:
329 pass
330
331 return HttpResponseRedirect('/')
332
333@login_required
334def preferences(request):
335 if request.method != 'POST':
336 raise Http404
337
338 if 'get' in request.POST and 'name' in request.POST:
339 return request.user.getPreference(request.POST['name'])
340 elif 'set' in request.POST and 'name' in request.POST and \
341 'value' in request.POST:
342 return request.user.setPreference(request.POST['name'], request.POST['value'])
343 else:
344 raise Http404
345
346def search(request):
347 context = _prepare_note_context(request.user)
348
349 ref = request.META['HTTP_REFERER']
350
351 if 'text' in request.POST:
352 text = request.POST['text']
353 else:
354 return HttpResponseRedirect(ref)
355
356 s = Search()
357 note_list = s.search(text)
358
359 if request.user.is_authenticated():
360 notes = Note.objects.filter(pk__in=note_list, author=request.user)
361
362 context['notes'] = notes
363 context['note_form'] = NoteForm()
364
365 return render(request, 'user_index.html', context)
366 else:
367 notes = Note.objects.filter(pk__in=note_list, visibility__gte=Note.PUBLIC)
368 context['notes'] = notes
369 return render(request, 'public_notes.html', context)
370
371@login_required
372def generate_search_index(request):
373
374 if os.path.exists('_search.db'):
375 os.path.remove('_search.db')
376
377 s = Search()
378 s.generate_index(Note.objects.all())
379
380 return HttpResponseRedirect('/')
381
382@login_required
383def add_template(request):
384 user = request.user
385
386 if request.method == 'POST':
387 if 'add' in request.POST:
388 template = Template(author=user)
389 template.category = manage_category(user, request.POST['category'])
390 form = TemplateForm(request.POST, instance=template)
391 if form.is_valid():
392 form.save()
393 return HttpResponseRedirect('/templates')
394 else:
395 if 'cancel' in request.POST:
396 return HttpResponseRedirect('/templates')
397 else:
398 template = Template(visibility=user.home_notes_visibility)
399 form = TemplateForm(instance=template)
400
401 context = _prepare_template_context(user)
402 context['template_form'] = form
403 context['template'] = None
404 return render(request, 'user_template.html', context)
405
406@login_required
407def template(request, template_id):
408 user = request.user
409
410 template = Template.objects.get(pk=template_id, author=user)
411
412 if template is None:
413 raise Http404
414
415 form = TemplateForm(instance=template)
416 if request.method == 'POST':
417 if 'edit' in request.POST:
418 template.category = manage_category(user, request.POST['category'])
419 form = TemplateForm(request.POST, instance=template)
420 if form.is_valid():
421 form.save()
422 else:
423 if 'delete' in request.POST:
424 template.delete()
425 return HttpResponseRedirect('/templates')
426
427 context = _prepare_template_context(user)
428 context['template'] = template
429 context['template_form'] = form
430
431 return render(request, 'user_template.html', context)
432
433@login_required
434def templates(request):
435 user = request.user
436 context = _prepare_template_context(user)
437
438 templates = Template.objects.filter(author=user.id).order_by('name')
439 context['templates'] = templates
440 context['template_form'] = TemplateForm()
441
442 return render(request, 'user_template_index.html', context)

Archive Download this file

Branches

Tags