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 | ␊ |
21 | import os␊ |
22 | from datetime import datetime␊ |
23 | ␊ |
24 | from django.http import HttpResponseRedirect, HttpResponse, Http404, HttpResponseForbidden␊ |
25 | from django.contrib.auth.decorators import login_required␊ |
26 | from django.contrib.auth import authenticate, login, logout␊ |
27 | from django.shortcuts import render␊ |
28 | ␊ |
29 | from denote.models import *␊ |
30 | from denote.forms import *␊ |
31 | from denote.search import *␊ |
32 | ␊ |
33 | def 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 | ␊ |
58 | def disconnect(request):␊ |
59 | user = request.user␊ |
60 | ␊ |
61 | if not user is None:␊ |
62 | logout(request)␊ |
63 | ␊ |
64 | return HttpResponseRedirect('/')␊ |
65 | ␊ |
66 | def 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␊ |
91 | def 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 | ␊ |
117 | def _prepare_note_context(user):␊ |
118 | if not user.is_authenticated():␊ |
119 | return {␊ |
120 | 'authenticated' : False,␊ |
121 | }␊ |
122 | ␊ |
123 | categories = Category.objects.filter(author=user.id).order_by('name')␊ |
124 | notes_by_category = []␊ |
125 | need_refresh = False␊ |
126 | for category in categories:␊ |
127 | meta_note = {}␊ |
128 | meta_note['category'] = category.name␊ |
129 | meta_note['category_id'] = category.id␊ |
130 | meta_note['notes'] = Note.objects.filter(author=user,category=category).order_by('-modified_date')␊ |
131 | if meta_note['notes']:␊ |
132 | notes_by_category.append(meta_note)␊ |
133 | else:␊ |
134 | category.delete()␊ |
135 | need_refresh = True␊ |
136 | if need_refresh:␊ |
137 | categories = Category.objects.filter(author=user.id).order_by('name')␊ |
138 | notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date')␊ |
139 | ␊ |
140 | context = {␊ |
141 | 'user': user,␊ |
142 | 'authenticated' : True,␊ |
143 | 'notes_by_category': notes_by_category,␊ |
144 | 'categories': categories,␊ |
145 | 'notes_without_category': notes_without_category,␊ |
146 | }␊ |
147 | ␊ |
148 | return context␊ |
149 | ␊ |
150 | @login_required␊ |
151 | def user_home(request, user):␊ |
152 | context = _prepare_note_context(user)␊ |
153 | ␊ |
154 | notes = Note.objects.filter(author=user.id).order_by('-modified_date')[:20]␊ |
155 | context['notes'] = notes␊ |
156 | context['note_form'] = NoteForm()␊ |
157 | ␊ |
158 | return render(request, 'user_index.html', context)␊ |
159 | ␊ |
160 | @login_required␊ |
161 | def add_note(request):␊ |
162 | user = request.user␊ |
163 | ␊ |
164 | if request.method == 'POST':␊ |
165 | if 'add' in request.POST:␊ |
166 | note = Note(author=user, created_date=datetime.now())␊ |
167 | note.category = manage_category(user, request.POST['category'])␊ |
168 | form = NoteForm(request.POST, instance=note)␊ |
169 | if form.is_valid():␊ |
170 | form.save()␊ |
171 | return HttpResponseRedirect('/note/%d' % (note.id))␊ |
172 | else:␊ |
173 | if 'cancel' in request.POST:␊ |
174 | return HttpResponseRedirect('/')␊ |
175 | else:␊ |
176 | note = Note(visibility=user.home_notes_visibility)␊ |
177 | form = NoteForm(instance=note)␊ |
178 | ␊ |
179 | context = _prepare_note_context(user)␊ |
180 | context['note_form'] = form␊ |
181 | context['note'] = None␊ |
182 | return render(request, 'user_note.html', context)␊ |
183 | ␊ |
184 | @login_required␊ |
185 | def note(request, note_id):␊ |
186 | user = request.user␊ |
187 | ␊ |
188 | note = Note.objects.get(pk=note_id, author=user)␊ |
189 | ␊ |
190 | if note is None:␊ |
191 | raise Http404␊ |
192 | ␊ |
193 | form = NoteForm(instance=note)␊ |
194 | if request.method == 'POST':␊ |
195 | if 'edit' in request.POST:␊ |
196 | note.category = manage_category(user, request.POST['category'])␊ |
197 | form = NoteForm(request.POST, instance=note)␊ |
198 | if form.is_valid():␊ |
199 | form.save()␊ |
200 | else:␊ |
201 | if 'delete' in request.POST:␊ |
202 | note.delete()␊ |
203 | return HttpResponseRedirect('/')␊ |
204 | ␊ |
205 | context = _prepare_note_context(user)␊ |
206 | context['note'] = note␊ |
207 | context['note_form'] = form␊ |
208 | ␊ |
209 | return render(request, 'user_note.html', context)␊ |
210 | ␊ |
211 | def public_note(request, user_id, note_id):␊ |
212 | user = request.user␊ |
213 | ␊ |
214 | try:␊ |
215 | note = Note.objects.get(pk=note_id, author=user_id)␊ |
216 | except:␊ |
217 | raise Http404␊ |
218 | ␊ |
219 | if note is None:␊ |
220 | raise Http404␊ |
221 | ␊ |
222 | if not user or not user.is_authenticated():␊ |
223 | if note.visibility != Note.PUBLIC:␊ |
224 | return HttpResponseForbidden()␊ |
225 | else:␊ |
226 | if note.visibility == Note.PRIVATE and\␊ |
227 | user_id != user.id:␊ |
228 | return HttpResponseForbidden()␊ |
229 | ␊ |
230 | if user.is_authenticated():␊ |
231 | public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.REGISTERED).order_by('-modified_date')␊ |
232 | else:␊ |
233 | public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.PUBLIC).order_by('-modified_date')␊ |
234 | ␊ |
235 | context = _prepare_note_context(user)␊ |
236 | context['note'] = note␊ |
237 | context['public_notes'] = public_notes␊ |
238 | ␊ |
239 | return render(request, 'public_note.html', context)␊ |
240 | ␊ |
241 | def public_notes(request):␊ |
242 | user = request.user␊ |
243 | ␊ |
244 | if user.is_authenticated():␊ |
245 | public_notes = Note.objects.filter(visibility__gte=Note.REGISTERED).order_by('-modified_date')␊ |
246 | else:␊ |
247 | public_notes = Note.objects.filter(visibility__gte=Note.PUBLIC).order_by('-modified_date')␊ |
248 | ␊ |
249 | context = _prepare_note_context(user)␊ |
250 | context['notes'] = public_notes[:50]␊ |
251 | context['public_notes'] = public_notes[:50]␊ |
252 | ␊ |
253 | return render(request, 'public_notes.html', context)␊ |
254 | ␊ |
255 | @login_required␊ |
256 | def edit_category(request, category_id):␊ |
257 | user = request.user␊ |
258 | ␊ |
259 | category = Category.objects.get(pk=category_id, author=user)␊ |
260 | ␊ |
261 | if category is None:␊ |
262 | raise Http404␊ |
263 | ␊ |
264 | if request.method == 'POST':␊ |
265 | if not 'new_cat_name' in request.POST or \␊ |
266 | not request.POST['new_cat_name']:␊ |
267 | return HttpResponseRedirect('/')␊ |
268 | category.name = request.POST['new_cat_name'].strip()␊ |
269 | if len(category.name) > 50: category.name = category.name[:50]␊ |
270 | category.author = user␊ |
271 | try:␊ |
272 | category.save()␊ |
273 | except:␊ |
274 | pass␊ |
275 | ␊ |
276 | return HttpResponseRedirect('/')␊ |
277 | ␊ |
278 | @login_required␊ |
279 | def preferences(request):␊ |
280 | if request.method != 'POST':␊ |
281 | raise Http404␊ |
282 | ␊ |
283 | if 'get' in request.POST and 'name' in request.POST:␊ |
284 | return request.user.getPreference(request.POST['name'])␊ |
285 | elif 'set' in request.POST and 'name' in request.POST and \␊ |
286 | 'value' in request.POST:␊ |
287 | return request.user.setPreference(request.POST['name'], request.POST['value'])␊ |
288 | else:␊ |
289 | raise Http404␊ |
290 | ␊ |
291 | def search(request):␊ |
292 | context = _prepare_note_context(request.user)␊ |
293 | ␊ |
294 | ref = request.META['HTTP_REFERER']␊ |
295 | ␊ |
296 | if 'text' in request.POST:␊ |
297 | text = request.POST['text']␊ |
298 | else:␊ |
299 | return HttpResponseRedirect(ref)␊ |
300 | ␊ |
301 | s = Search()␊ |
302 | note_list = s.search(text)␊ |
303 | ␊ |
304 | if request.user.is_authenticated():␊ |
305 | notes = Note.objects.filter(pk__in=note_list, author=request.user)␊ |
306 | ␊ |
307 | context['notes'] = notes␊ |
308 | context['note_form'] = NoteForm()␊ |
309 | ␊ |
310 | return render(request, 'user_index.html', context)␊ |
311 | else:␊ |
312 | notes = Note.objects.filter(pk__in=note_list, visibility__gte=Note.PUBLIC)␊ |
313 | context['notes'] = notes␊ |
314 | return render(request, 'public_notes.html', context)␊ |
315 | ␊ |
316 | @login_required␊ |
317 | def generate_search_index(request):␊ |
318 | ␊ |
319 | if os.path.exists('_search.db'):␊ |
320 | os.path.remove('_search.db')␊ |
321 | ␊ |
322 | s = Search()␊ |
323 | s.generate_index(Note.objects.all())␊ |
324 | ␊ |
325 | return HttpResponseRedirect('/')␊ |