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

Archive Download this file

Branches

Tags