Dénote

Dénote Commit Details

Date:2016-06-30 20:13:15 (2 years 3 months ago)
Author:Grégory Soutadé
Branch:master
Commit:b983d6d3f1874d9e0d52c20ef263e42b790422d8
Parents: d3327f13a23a7d6582e89f4bd29627e9992761c1
Message:Add templates management

Changes:
Cdenote/templates/user_note.html → denote/templates/user_template.html
Adenote/templates/user_template_index.html (full)
Mdenote/forms.py (1 diff)
Mdenote/models.py (2 diffs)
Mdenote/templates/base.html (1 diff)
Mdenote/templates/edit_user.html (1 diff)
Mdenote/templates/user_note.html (1 diff)
Mdenote/urls.py (2 diffs)
Mdenote/views.py (6 diffs)

File differences

denote/forms.py
3131
3232
3333
34
35
36
37
38
39
40
3441
3542
3643
model = Note
exclude = ('author', 'transformed_text', 'long_summary', 'short_summary', 'created_date', 'modified_date', 'category')
class TemplateForm(ModelForm):
text = forms.CharField(widget=forms.Textarea(attrs={'rows':'20', 'cols':'150'}), required=False)
title = forms.CharField(widget=forms.Textarea(attrs={'rows':'1', 'cols':'100'}), required=False)
class Meta:
model = Template
exclude = ('author', 'category')
class UserForm(ModelForm):
denote/models.py
3434
3535
3636
37
3738
3839
3940
......
132133
133134
134135
136
137
138
139
140
141
142
143
144
135145
136146
137147
class User(AbstractUser):
hidden_categories = models.TextField(blank=True)
home_notes_visibility = models.IntegerField(default=0, choices=[(0, 'private'), (1, 'registered'), (2, 'public')])
default_template = models.ForeignKey('Template', null=True, on_delete=models.SET_NULL)
def getPreference(self, name):
if name == 'hidden_categories':
super(Note, self).save()
class Template(models.Model):
name = models.CharField(max_length=30, blank=False, unique=True)
author = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
title = models.CharField(max_length=100, blank=True)
text = models.TextField(blank=True)
visibility = models.IntegerField(default=Note.PRIVATE, choices=Note.VISIBILITY)
category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL)
@receiver(post_save, sender=Note)
def post_save_note_signal(sender, **kwargs):
s = Search()
denote/templates/base.html
1313
1414
1515
16
16
1717
1818
1919
2020
2121
2222
23
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2443
2544
2645
</head>
<body onload="startup();">
<!-- Header -->
<div class="settings"><a href="/public_notes">Public notes</a> {% if authenticated %}<a href="/user/edit">Settings</a> <a href="/disconnect">Disconnect</a><br/>
<div class="settings"><a href="/public_notes">Public notes</a> {% if authenticated %}<a href="/templates">Templates</a> <a href="/user/edit">Settings</a> <a href="/disconnect">Disconnect</a><br/>
{% endif %}
<form action="/search" method="post">{% csrf_token %}<input name="text"/><input type="submit" value="Search"/></form></div>
<!-- Left panel -->
<div id="left_panel">
<a id="home_icon" href="/" alt="Home"><img src="{{ STATIC_URL }}images/home.png"/></a><br/><br/>
{% if authenticated %}
<a href="/note/add">Add a note</a>
<form id="form_note_add" action="/note/add" method="POST">
{% csrf_token %}
<a href="#" onclick="document.getElementById('form_note_add').submit();">Add a note</a>
{% if templates_by_name|length != 0 %}
<br/>
<select name="template">
<option value="-1">None</option>
{% for template in templates_by_name %}
{% if template.id == user.default_template.id %}
<option value="{{ template.id }}" selected="1">{{ template.name }}</option>
{% else %}
<option value="{{ template.id }}">{{ template.name }}</option>
{% endif %}
{% endfor %}
</select>
<br/>
{% else %}
<input type="hidden" name="template" value="-1"/>
{% endif %}
</form>
{% endif %}
{% block left %} {% endblock %}
</div>
denote/templates/edit_user.html
88
99
1010
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1125
1226
1327
<form action="/user/edit" method="post">
{% csrf_token %}
{{ form.as_p }}
<p>
<label for="id_default_template">Default template:</label>
<select id="id_default_template" name="default_template">
<option value="-1">None</option>
{% for template in templates_by_name %}
{% if template.id == user.default_template.id %}
<option value="{{ template.id }}" selected="1">{{ template.name }}</option>
{% else %}
<option value="{{ template.id }}">{{ template.name }}</option>
{% endif %}
{% endfor %}
</select>
</p>
<input type="submit" name="edit" value="Edit" /><input type="submit" name="cancel" value="Cancel" /><input type="submit" name="delete" value="Delete" onclick="return confirm('Do you really want to delete your account ?')"/>
</form>
{% endblock %}
denote/templates/user_note.html
2929
3030
3131
32
32
3333
3434
3535
<div class="form_add">
<form action="/note/add" method="post">{% csrf_token %}
{{ note_form.as_p }}
Category <input name="category" list=all_categories>
Category <input name="category" list=all_categories {% if category != None %} value="{{ category }}" {% endif %}>
<datalist id="all_categories">
{% for category in categories %}
<option value="{{ category.name }}"></option>
denote/templates/user_template.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{% extends "base.html" %}
{% block content %}
<div class="template">
{% if template != None %}
<form action="/template/{{ template.id }}" method="post">{% csrf_token %}
{% else %}
<form action="/template/add" method="post">{% csrf_token %}
{% endif %}
{{ template_form.as_p }}
Category <input name="category" list="all_categories" {% if template.category != None %} value="{{ template.category.name }}" {% endif %}>
<datalist id="all_categories">
{% for category in categories %}
<option value="{{ category.name }}"></option>
{% endfor %}
</datalist><br/>
{% if template != None %}
<input type="submit" name="edit" value="Edit" />
<input type="submit" name="delete" value="Delete" onclick="return confirm('Do you really want to delete this template ?')"/>
{% else %}
<input type="submit" name="add" value="Add"/>
{% endif %}
<input type="submit" name="cancel" value="Cancel"/>
</form>
<br/>
<b>Markdown syntax</b><br /><br />
<table>
<tr>
<td class="markdown_help">
<pre style="display:inline">_italic_</pre> <span style="font-style:italic">italic</span><br/>
<pre style="display:inline">**bold**</pre> <span style="font-weight:bold">bold</span><br/>
<pre style="display:inline">~~line through~~</pre> <span style="text-decoration:line-through">line through</span><br/>
<pre style="display:inline">~underline~</pre> <span style="text-decoration:underline">underline</span><br/>
<pre style="display:inline">>Citation</pre><br/>
<pre>
* Unordered list
* Second element
</pre>
<ul>
<li>Unordered list
<li>Second element
</ul>
<pre>
1. Ordered list
1. Second element
</pre>
<ol>
<li>Ordered list
<li>Second element
</ol>
<pre style="display:inline">![Picture](https://bits.wikimedia.org/images/wikimedia-button.png)</pre><img src="https://bits.wikimedia.org/images/wikimedia-button.png" alt="Picture"/><br/>
<pre style="display:inline">#[Inline Picture](https://bits.wikimedia.org/images/wikimedia-button.png)</pre><img src="https://bits.wikimedia.org/images/wikimedia-button.png" alt="Picture"/><br/>
<pre style="display:inline">[Link](http://www.wikipedia.org)</pre> <a href="http://www.wikipedia.org">Link</a><br/><br/>
<pre>
Code : 4 whitespaces ahead OR
```language
Code
```
</pre>
</td>
<td>
<pre># Title # or
Title
=====</pre>
<h1>Title</h1>
<pre>## Sub title ## or
Sub title
---------</pre>
<h2>Sub title</h2>
<pre>### Sub sub title ###</pre>
<h3>Sub sub title</h3>
</td>
</tr>
</table>
</div>
</div>
{% endblock %}
denote/templates/user_template_index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{% extends "base.html" %}
{% block content %}
{% for template in templates %}
<div class="template">
<div class="title"><a href="/template/{{ template.id}}" oncontextmenu="return DoEdit('{{ template.id }}');">{{ template.name }}</a></div>
</div>
{% endfor %}
{% if templates|length == 0 %}
<b>Any template</b>
{% endif %}
<div class="settings"><a href="/template/add">Add a Template</a><br/></div>
{% endblock %}
denote/urls.py
2424
2525
2626
27
27
2828
2929
3030
......
3434
3535
3636
37
38
39
40
3741
url(r'^index[/]?$', 'denote.views.index', name='index'),
url(r'^[/]?$', 'denote.views.index', name='index'),
url(r'^disconnect?$', 'denote.views.disconnect', name='disconnect'),
url(r'^user/add$','denote.views.new_user', name='add_user'),
url(r'^user/add$','denote.views.new_user', name='new_user'),
url(r'^user/edit$','denote.views.edit_user', name='edit_user'),
url(r'^note/add$', 'denote.views.add_note', name='add_note'),
url(r'^note/(\d+)$', 'denote.views.note', name='note'),
url(r'^preferences$', 'denote.views.preferences', name='preferences'),
url(r'^search$', 'denote.views.search', name='search'),
url(r'^generate_search_index$', 'denote.views.generate_search_index', name='generate_search_index'),
url(r'^template/add$','denote.views.add_template', name='add_template'),
url(r'^template/(\d+)$','denote.views.template', name='template'),
url(r'^templates$','denote.views.templates', name='templates'),
)
denote/views.py
8484
8585
8686
87
87
8888
8989
9090
......
9494
9595
9696
97
98
99
100
101
102
103
104
105
97106
98107
99108
100109
101110
102111
103
112
104113
105114
106115
......
110119
111120
112121
122
123
124
125
126
127
128
129
113130
131
114132
115133
116134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
117155
118156
119157
......
137175
138176
139177
178
179
180
181
182
183
184
185
140186
141187
142188
143189
144190
145191
192
146193
147194
148195
......
161208
162209
163210
164
165
166
167
168
169
170
171
172
173
174
211
212
213
214
215
216
217
218
219
175220
176
221
222
223
224
225
226
227
177228
178229
179230
180231
181232
233
234
235
236
182237
183238
184239
......
323378
324379
325380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
form = UserForm()
c = {'login' : login_val, 'password' : password, 'form': form}
return render(request, 'add_user.html', c)
@login_required
if request.method == 'POST':
if 'edit' in request.POST:
template_id = request.POST['default_template']
if template_id == '-1':
template = None
else:
try:
template= Template.objects.get(author=user.id, id=template_id)
except:
template = None
user.default_template = template
form = UserForm(request.POST, instance=user, initial={'password':''})
if form.is_valid():
form.save()
if request.POST['password'] != '':
user.set_password(request.POST['password'])
user.save()
edited = True
edited = True
else:
if 'delete' in request.POST:
logout(request)
login = 'login' in request.POST and request.POST['login'] or ''
form = UserForm(instance=user, initial={'password':'', 'login':login})
templates = Template.objects.filter(author=user.id).order_by('name')
templates_by_name = []
for template in templates:
t = {}
t['name'] = template.name
t['id'] = template.id
templates_by_name.append(t)
c = {'authenticated' : True, 'user_to_edit' : user, 'form' : form, 'edited' : edited}
c['templates_by_name'] = templates_by_name
return render(request, 'edit_user.html', c)
def _prepare_template_context(user):
categories = Category.objects.filter(author=user.id).order_by('name')
templates = Template.objects.filter(author=user.id).order_by('name')
templates_by_name = []
for template in templates:
t = {}
t['name'] = template.name
t['id'] = template.id
templates_by_name.append(t)
context = {
'user': user,
'authenticated' : True,
'categories': categories,
'templates_by_name': templates_by_name,
}
return context
def _prepare_note_context(user):
if not user.is_authenticated():
return {
categories = Category.objects.filter(author=user.id).order_by('name')
notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date')
templates = Template.objects.filter(author=user.id).order_by('name')
templates_by_name = []
for template in templates:
t = {}
t['name'] = template.name
t['id'] = template.id
templates_by_name.append(t)
context = {
'user': user,
'authenticated' : True,
'notes_by_category': notes_by_category,
'categories': categories,
'notes_without_category': notes_without_category,
'templates_by_name': templates_by_name,
}
return context
def add_note(request):
user = request.user
if request.method == 'POST':
if 'add' in request.POST:
note = Note(author=user, created_date=datetime.now())
note.category = manage_category(user, request.POST['category'])
form = NoteForm(request.POST, instance=note)
if form.is_valid():
form.save()
return HttpResponseRedirect('/note/%d' % (note.id))
else:
if 'cancel' in request.POST:
return HttpResponseRedirect('/')
if 'add' in request.POST:
note = Note(author=user, created_date=datetime.now())
note.category = manage_category(user, request.POST['category'])
form = NoteForm(request.POST, instance=note)
if form.is_valid():
form.save()
return HttpResponseRedirect('/note/%d' % (note.id))
elif 'cancel' in request.POST:
return HttpResponseRedirect('/')
else:
note = Note(visibility=user.home_notes_visibility)
note = None
template_id = request.POST['template']
if template_id != '-1':
note = Template.objects.get(id=template_id, author=user.id)
if not note:
note = Note(visibility=user.home_notes_visibility)
form = NoteForm(instance=note)
context = _prepare_note_context(user)
context['note_form'] = form
context['note'] = None
if note.category:
context['category'] = note.category.name
return render(request, 'user_note.html', context)
@login_required
s.generate_index(Note.objects.all())
return HttpResponseRedirect('/')
@login_required
def add_template(request):
user = request.user
if request.method == 'POST':
if 'add' in request.POST:
template = Template(author=user)
template.category = manage_category(user, request.POST['category'])
form = TemplateForm(request.POST, instance=template)
if form.is_valid():
form.save()
return HttpResponseRedirect('/templates')
else:
if 'cancel' in request.POST:
return HttpResponseRedirect('/templates')
else:
template = Template(visibility=user.home_notes_visibility)
form = TemplateForm(instance=template)
context = _prepare_template_context(user)
context['template_form'] = form
context['template'] = None
return render(request, 'user_template.html', context)
@login_required
def template(request, template_id):
user = request.user
template = Template.objects.get(pk=template_id, author=user)
if template is None:
raise Http404
form = TemplateForm(instance=template)
if request.method == 'POST':
if 'edit' in request.POST:
template.category = manage_category(user, request.POST['category'])
form = TemplateForm(request.POST, instance=template)
if form.is_valid():
form.save()
else:
if 'delete' in request.POST:
template.delete()
return HttpResponseRedirect('/templates')
context = _prepare_template_context(user)
context['template'] = template
context['template_form'] = form
return render(request, 'user_template.html', context)
@login_required
def templates(request):
user = request.user
context = _prepare_template_context(user)
templates = Template.objects.filter(author=user.id).order_by('name')
context['templates'] = templates
context['template_form'] = TemplateForm()
return render(request, 'user_template_index.html', context)

Archive Download the corresponding diff file

Branches

Tags