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
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 form = UserForm(request.POST, instance=user, initial={'password':''})
98 if form.is_valid():
99 form.save()
100 if request.POST['password'] != '':
101 user.set_password(request.POST['password'])
102 user.save()
103 edited = True
104 else:
105 if 'delete' in request.POST:
106 logout(request)
107 User.objects.filter(pk=user.id).delete()
108 return HttpResponseRedirect('/')
109 else:
110 login = 'login' in request.POST and request.POST['login'] or ''
111 form = UserForm(instance=user, initial={'password':'', 'login':login})
112
113 c = {'user_to_edit' : user, 'form' : form, 'edited' : edited}
114
115 return render(request, 'edit_user.html', c)
116
117def _prepare_note_context(user):
118 categories = Category.objects.filter(author=user.id).order_by('name')
119 notes_by_category = []
120 need_refresh = False
121 for category in categories:
122 meta_note = {}
123 meta_note['category'] = category.name
124 meta_note['category_id'] = category.id
125 meta_note['notes'] = Note.objects.filter(author=user,category=category).order_by('-modified_date')
126 if meta_note['notes']:
127 notes_by_category.append(meta_note)
128 else:
129 category.delete()
130 need_refresh = True
131 if need_refresh:
132 categories = Category.objects.filter(author=user.id).order_by('name')
133 notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date')
134
135 context = {
136 'user': user,
137 'notes_by_category': notes_by_category,
138 'categories': categories,
139 'notes_without_category': notes_without_category,
140 }
141
142 return context
143
144@login_required
145def user_home(request, user):
146 context = _prepare_note_context(user)
147
148 notes = Note.objects.filter(author=user.id).order_by('-modified_date')[:20]
149 context['notes'] = notes
150 context['note_form'] = NoteForm()
151
152 return render(request, 'user_index.html', context)
153
154@login_required
155def add_note(request):
156 user = request.user
157
158 if request.method == 'POST':
159 if 'add' in request.POST:
160 note = Note(author=user, created_date=datetime.now())
161 note.category = manage_category(user, request.POST['category'])
162 form = NoteForm(request.POST, instance=note)
163 if form.is_valid():
164 form.save()
165 return HttpResponseRedirect('/note/%d' % (note.id))
166 else:
167 if 'cancel' in request.POST:
168 return HttpResponseRedirect('/')
169 else:
170 form = NoteForm()
171
172 context = _prepare_note_context(user)
173 context['note_form'] = form
174 context['note'] = None
175 return render(request, 'user_note.html', context)
176
177@login_required
178def note(request, note_id):
179 user = request.user
180
181 note = Note.objects.get(pk=note_id, author=user)
182
183 if note is None:
184 raise Http404
185
186 form = NoteForm(instance=note)
187 if request.method == 'POST':
188 if 'edit' in request.POST:
189 note.category = manage_category(user, request.POST['category'])
190 form = NoteForm(request.POST, instance=note)
191 if form.is_valid():
192 form.save()
193 else:
194 if 'delete' in request.POST:
195 note.delete()
196 return HttpResponseRedirect('/')
197
198 context = _prepare_note_context(user)
199 context['note'] = note
200 context['note_form'] = form
201
202 return render(request, 'user_note.html', context)
203
204@login_required
205def edit_category(request, category_id):
206 user = request.user
207
208 category = Category.objects.get(pk=category_id, author=user)
209
210 if category is None:
211 raise Http404
212
213 if request.method == 'POST':
214 if not 'new_cat_name' in request.POST or \
215 not request.POST['new_cat_name']:
216 return HttpResponseRedirect('/')
217 category.name = request.POST['new_cat_name'].strip()
218 if len(category.name) > 50: category.name = category.name[:50]
219 category.author = user
220 try:
221 category.save()
222 except:
223 pass
224
225 return HttpResponseRedirect('/')
226
227@login_required
228def preferences(request):
229 print request.method
230 if request.method != 'POST':
231 raise Http404
232
233 print request.POST.items()
234
235 if 'get' in request.POST and 'name' in request.POST:
236 return request.user.getPreference(request.POST['name'])
237 elif 'set' in request.POST and 'name' in request.POST and \
238 'value' in request.POST:
239 return request.user.setPreference(request.POST['name'], request.POST['value'])
240 else:
241 raise Http404
242
243@login_required
244def search(request):
245 context = _prepare_note_context(request.user)
246
247 ref = request.META['HTTP_REFERER']
248
249 if 'text' in request.POST:
250 text = request.POST['text']
251 else:
252 return HttpResponseRedirect(ref)
253
254 s = Search()
255 note_list = s.search(text)
256
257 notes = Note.objects.filter(pk__in=note_list, author=request.user)
258 context['notes'] = notes
259 context['note_form'] = NoteForm()
260
261 return render(request, 'user_index.html', context)
262
263@login_required
264def generate_search_index(request):
265
266 if os.path.exists('_search.db'):
267 os.path.remove('_search.db')
268
269 s = Search()
270 s.generate_index(Note.objects.all())
271
272 return HttpResponseRedirect('/')

Archive Download this file

Branches

Tags