Add templates management

This commit is contained in:
Gregory Soutade 2016-06-30 20:13:15 +02:00
parent d3327f13a2
commit b983d6d3f1
9 changed files with 280 additions and 18 deletions

View File

@ -31,6 +31,13 @@ class NoteForm(ModelForm):
model = Note model = Note
exclude = ('author', 'transformed_text', 'long_summary', 'short_summary', 'created_date', 'modified_date', 'category') 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): class UserForm(ModelForm):

View File

@ -34,6 +34,7 @@ from search import Search
class User(AbstractUser): class User(AbstractUser):
hidden_categories = models.TextField(blank=True) hidden_categories = models.TextField(blank=True)
home_notes_visibility = models.IntegerField(default=0, choices=[(0, 'private'), (1, 'registered'), (2, 'public')]) 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): def getPreference(self, name):
if name == 'hidden_categories': if name == 'hidden_categories':
@ -132,6 +133,15 @@ class Note(models.Model):
super(Note, self).save() 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) @receiver(post_save, sender=Note)
def post_save_note_signal(sender, **kwargs): def post_save_note_signal(sender, **kwargs):
s = Search() s = Search()

View File

@ -13,14 +13,33 @@
</head> </head>
<body onload="startup();"> <body onload="startup();">
<!-- Header --> <!-- 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 %} {% endif %}
<form action="/search" method="post">{% csrf_token %}<input name="text"/><input type="submit" value="Search"/></form></div> <form action="/search" method="post">{% csrf_token %}<input name="text"/><input type="submit" value="Search"/></form></div>
<!-- Left panel --> <!-- Left panel -->
<div id="left_panel"> <div id="left_panel">
<a id="home_icon" href="/" alt="Home"><img src="{{ STATIC_URL }}images/home.png"/></a><br/><br/> <a id="home_icon" href="/" alt="Home"><img src="{{ STATIC_URL }}images/home.png"/></a><br/><br/>
{% if authenticated %} {% 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 %} {% endif %}
{% block left %} {% endblock %} {% block left %} {% endblock %}
</div> </div>

View File

@ -8,6 +8,20 @@
<form action="/user/edit" method="post"> <form action="/user/edit" method="post">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ 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 ?')"/> <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> </form>
{% endblock %} {% endblock %}

View File

@ -29,7 +29,7 @@
<div class="form_add"> <div class="form_add">
<form action="/note/add" method="post">{% csrf_token %} <form action="/note/add" method="post">{% csrf_token %}
{{ note_form.as_p }} {{ 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"> <datalist id="all_categories">
{% for category in categories %} {% for category in categories %}
<option value="{{ category.name }}"></option> <option value="{{ category.name }}"></option>

View File

@ -0,0 +1,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 %}

View File

@ -0,0 +1,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 %}

View File

@ -24,7 +24,7 @@ urlpatterns = patterns('',
url(r'^index[/]?$', 'denote.views.index', name='index'), url(r'^index[/]?$', 'denote.views.index', name='index'),
url(r'^[/]?$', 'denote.views.index', name='index'), url(r'^[/]?$', 'denote.views.index', name='index'),
url(r'^disconnect?$', 'denote.views.disconnect', name='disconnect'), 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'^user/edit$','denote.views.edit_user', name='edit_user'),
url(r'^note/add$', 'denote.views.add_note', name='add_note'), url(r'^note/add$', 'denote.views.add_note', name='add_note'),
url(r'^note/(\d+)$', 'denote.views.note', name='note'), url(r'^note/(\d+)$', 'denote.views.note', name='note'),
@ -34,4 +34,8 @@ urlpatterns = patterns('',
url(r'^preferences$', 'denote.views.preferences', name='preferences'), url(r'^preferences$', 'denote.views.preferences', name='preferences'),
url(r'^search$', 'denote.views.search', name='search'), url(r'^search$', 'denote.views.search', name='search'),
url(r'^generate_search_index$', 'denote.views.generate_search_index', name='generate_search_index'), 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'),
) )

View File

@ -84,7 +84,7 @@ def new_user(request):
form = UserForm() form = UserForm()
c = {'login' : login_val, 'password' : password, 'form': form} c = {'login' : login_val, 'password' : password, 'form': form}
return render(request, 'add_user.html', c) return render(request, 'add_user.html', c)
@login_required @login_required
@ -94,13 +94,22 @@ def edit_user(request):
if request.method == 'POST': if request.method == 'POST':
if 'edit' in request.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':''}) form = UserForm(request.POST, instance=user, initial={'password':''})
if form.is_valid(): if form.is_valid():
form.save() form.save()
if request.POST['password'] != '': if request.POST['password'] != '':
user.set_password(request.POST['password']) user.set_password(request.POST['password'])
user.save() user.save()
edited = True edited = True
else: else:
if 'delete' in request.POST: if 'delete' in request.POST:
logout(request) logout(request)
@ -110,10 +119,39 @@ def edit_user(request):
login = 'login' in request.POST and request.POST['login'] or '' login = 'login' in request.POST and request.POST['login'] or ''
form = UserForm(instance=user, initial={'password':'', 'login':login}) 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 = {'authenticated' : True, 'user_to_edit' : user, 'form' : form, 'edited' : edited}
c['templates_by_name'] = templates_by_name
return render(request, 'edit_user.html', c) 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): def _prepare_note_context(user):
if not user.is_authenticated(): if not user.is_authenticated():
return { return {
@ -137,12 +175,21 @@ def _prepare_note_context(user):
categories = Category.objects.filter(author=user.id).order_by('name') categories = Category.objects.filter(author=user.id).order_by('name')
notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date') 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 = { context = {
'user': user, 'user': user,
'authenticated' : True, 'authenticated' : True,
'notes_by_category': notes_by_category, 'notes_by_category': notes_by_category,
'categories': categories, 'categories': categories,
'notes_without_category': notes_without_category, 'notes_without_category': notes_without_category,
'templates_by_name': templates_by_name,
} }
return context return context
@ -161,24 +208,32 @@ def user_home(request, user):
def add_note(request): def add_note(request):
user = request.user user = request.user
if request.method == 'POST': if 'add' in request.POST:
if 'add' in request.POST: note = Note(author=user, created_date=datetime.now())
note = Note(author=user, created_date=datetime.now()) note.category = manage_category(user, request.POST['category'])
note.category = manage_category(user, request.POST['category']) form = NoteForm(request.POST, instance=note)
form = NoteForm(request.POST, instance=note) if form.is_valid():
if form.is_valid(): form.save()
form.save() return HttpResponseRedirect('/note/%d' % (note.id))
return HttpResponseRedirect('/note/%d' % (note.id)) elif 'cancel' in request.POST:
else: return HttpResponseRedirect('/')
if 'cancel' in request.POST:
return HttpResponseRedirect('/')
else: 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) form = NoteForm(instance=note)
context = _prepare_note_context(user) context = _prepare_note_context(user)
context['note_form'] = form context['note_form'] = form
context['note'] = None context['note'] = None
if note.category:
context['category'] = note.category.name
return render(request, 'user_note.html', context) return render(request, 'user_note.html', context)
@login_required @login_required
@ -323,3 +378,65 @@ def generate_search_index(request):
s.generate_index(Note.objects.all()) s.generate_index(Note.objects.all())
return HttpResponseRedirect('/') 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)