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
21from datetime import datetime
22
23from django.http import HttpResponseRedirect, HttpResponse, Http404
24from django.contrib.auth.decorators import login_required
25from django.contrib.auth import authenticate, login, logout
26from django.shortcuts import render
27
28from denote.models import *
29from denote.forms import *
30
31def index(request):
32 if request.user.is_authenticated():
33 return user_home(request, request.user)
34
35 login_failed = False
36 if 'login' in request.POST:
37 user = authenticate(username=request.POST['login'], password=request.POST['password'])
38 if user is None:
39 login_failed = True
40 else:
41 login(request, user)
42 if 'next' in request.GET:
43 return HttpResponseRedirect(request.GET['next'])
44 elif 'next' in request.POST:
45 return HttpResponseRedirect(request.POST['next'])
46 else:
47 return user_home(request, request.user)
48
49 c = {
50 'login_failed' : login_failed,
51 'nb_people_registered' : User.objects.all().count()
52 }
53
54 return render(request, 'login.html', c)
55
56def disconnect(request):
57 user = request.user
58
59 if not user is None:
60 logout(request)
61
62 return HttpResponseRedirect('/')
63
64def new_user(request):
65 login_val = 'login' in request.POST and request.POST['login'] or ''
66 password = 'password' in request.POST and request.POST['password'] or ''
67
68 if request.method == 'POST':
69 if 'add' in request.POST:
70 form = UserForm(request.POST)
71 if form.is_valid():
72 form = form.save()
73 user = User.objects.get(pk=form.id)
74 user.set_password(request.POST['password'])
75 user.save()
76 user = authenticate(username=user.username, password=request.POST['password'])
77 login(request, user)
78 return user_home(request, user)
79 else:
80 return HttpResponseRedirect('/')
81 else:
82 form = UserForm()
83
84 c = {'login' : login_val, 'password' : password, 'form': form}
85
86 return render(request, 'add_user.html', c)
87
88@login_required
89def edit_user(request):
90 user = request.user
91 edited = False
92
93 if request.method == 'POST':
94 if 'edit' in request.POST:
95 form = UserForm(request.POST, instance=user, initial={'password':''})
96 if form.is_valid():
97 form.save()
98 if request.POST['password'] != '':
99 user.set_password(request.POST['password'])
100 user.save()
101 edited = True
102 else:
103 if 'delete' in request.POST:
104 logout(request)
105 User.objects.filter(pk=user.id).delete()
106 return HttpResponseRedirect('/')
107 else:
108 login = 'login' in request.POST and request.POST['login'] or ''
109 form = UserForm(instance=user, initial={'password':'', 'login':login})
110
111 c = {'user_to_edit' : user, 'form' : form, 'edited' : edited}
112
113 return render(request, 'edit_user.html', c)
114
115def _prepare_note_context(user):
116 categories = Category.objects.filter(author=user.id).order_by('name')
117 notes_by_category = []
118 need_refresh = False
119 for category in categories:
120 meta_note = {}
121 meta_note['category'] = category.name
122 meta_note['category_id'] = category.id
123 meta_note['notes'] = Note.objects.filter(author=user,category=category).order_by('-modified_date')
124 if meta_note['notes']:
125 notes_by_category.append(meta_note)
126 else:
127 category.delete()
128 need_refresh = True
129 if need_refresh:
130 categories = Category.objects.filter(author=user.id).order_by('name')
131 notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date')
132
133 context = {
134 'user': user,
135 'notes_by_category': notes_by_category,
136 'categories': categories,
137 'notes_without_category': notes_without_category,
138 }
139
140 return context
141
142@login_required
143def user_home(request, user):
144 context = _prepare_note_context(user)
145
146 notes = Note.objects.filter(author=user.id).order_by('-modified_date')[:20]
147 context['notes'] = notes
148 context['note_form'] = NoteForm()
149
150 return render(request, 'user_index.html', context)
151
152@login_required
153def add_note(request):
154 user = request.user
155
156 if request.method == 'POST':
157 if 'add' in request.POST:
158 note = Note(author=user, created_date=datetime.now())
159 note.category = manage_category(user, request.POST['category'])
160 form = NoteForm(request.POST, instance=note)
161 if form.is_valid():
162 form.save()
163 return HttpResponseRedirect('/note/%d' % (note.id))
164 else:
165 if 'cancel' in request.POST:
166 return HttpResponseRedirect('/')
167 else:
168 form = NoteForm()
169
170 context = _prepare_note_context(user)
171 context['note_form'] = form
172 context['note'] = None
173 return render(request, 'user_note.html', context)
174
175@login_required
176def note(request, note_id):
177 user = request.user
178
179 note = Note.objects.get(pk=note_id, author=user)
180
181 if note is None:
182 raise Http404
183
184 form = NoteForm(instance=note)
185 if request.method == 'POST':
186 if 'edit' in request.POST:
187 note.category = manage_category(user, request.POST['category'])
188 form = NoteForm(request.POST, instance=note)
189 if form.is_valid():
190 form.save()
191 else:
192 if 'delete' in request.POST:
193 note.delete()
194 return HttpResponseRedirect('/')
195
196 context = _prepare_note_context(user)
197 context['note'] = note
198 context['note_form'] = form
199
200 return render(request, 'user_note.html', context)
201
202@login_required
203def edit_category(request, category_id):
204 user = request.user
205
206 category = Category.objects.get(pk=category_id, author=user)
207
208 if category is None:
209 raise Http404
210
211 if request.method == 'POST':
212 if not 'new_cat_name' in request.POST or \
213 not request.POST['new_cat_name']:
214 return HttpResponseRedirect('/')
215 category.name = request.POST['new_cat_name'].strip()
216 if len(category.name) > 50: category.name = category.name[:50]
217 category.author = user
218 try:
219 category.save()
220 except:
221 pass
222
223 return HttpResponseRedirect('/')
224
225@login_required
226def preferences(request):
227 print request.method
228 if request.method != 'POST':
229 raise Http404
230
231 print request.POST.items()
232
233 if 'get' in request.POST and 'name' in request.POST:
234 return request.user.getPreference(request.POST['name'])
235 elif 'set' in request.POST and 'name' in request.POST and \
236 'value' in request.POST:
237 return request.user.setPreference(request.POST['name'], request.POST['value'])
238 else:
239 raise Http404
240

Archive Download this file

Branches

Tags