diff --git a/denote/forms.py b/denote/forms.py
index db5bf4f..0599085 100644
--- a/denote/forms.py
+++ b/denote/forms.py
@@ -31,6 +31,13 @@ class NoteForm(ModelForm):
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):
diff --git a/denote/models.py b/denote/models.py
index e68b17c..965b123 100644
--- a/denote/models.py
+++ b/denote/models.py
@@ -34,6 +34,7 @@ from search import Search
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':
@@ -132,6 +133,15 @@ class Note(models.Model):
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()
diff --git a/denote/templates/base.html b/denote/templates/base.html
index a593f20..9dca2b7 100644
--- a/denote/templates/base.html
+++ b/denote/templates/base.html
@@ -13,14 +13,33 @@
- Public notes {% if authenticated %}
Settings Disconnect
+

{% if authenticated %}
-
Add a note
+
{% endif %}
{% block left %} {% endblock %}
diff --git a/denote/templates/edit_user.html b/denote/templates/edit_user.html
index 6caffb5..7716bf4 100644
--- a/denote/templates/edit_user.html
+++ b/denote/templates/edit_user.html
@@ -8,6 +8,20 @@
{% endblock %}
diff --git a/denote/templates/user_note.html b/denote/templates/user_note.html
index 9a0d0c2..a45726d 100644
--- a/denote/templates/user_note.html
+++ b/denote/templates/user_note.html
@@ -29,7 +29,7 @@
+{% endblock %}
diff --git a/denote/templates/user_template_index.html b/denote/templates/user_template_index.html
new file mode 100644
index 0000000..14a18c8
--- /dev/null
+++ b/denote/templates/user_template_index.html
@@ -0,0 +1,14 @@
+{% extends "base.html" %}
+
+{% block content %}
+{% for template in templates %}
+
+{% endfor %}
+{% if templates|length == 0 %}
+
Any template
+{% endif %}
+
+
+{% endblock %}
diff --git a/denote/urls.py b/denote/urls.py
index 6ec851c..b30a00d 100644
--- a/denote/urls.py
+++ b/denote/urls.py
@@ -24,7 +24,7 @@ urlpatterns = patterns('',
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'),
@@ -34,4 +34,8 @@ urlpatterns = patterns('',
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'),
)
diff --git a/denote/views.py b/denote/views.py
index ef526db..9e0e141 100644
--- a/denote/views.py
+++ b/denote/views.py
@@ -84,7 +84,7 @@ def new_user(request):
form = UserForm()
c = {'login' : login_val, 'password' : password, 'form': form}
-
+
return render(request, 'add_user.html', c)
@login_required
@@ -94,13 +94,22 @@ def edit_user(request):
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)
@@ -110,10 +119,39 @@ def edit_user(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 {
@@ -137,12 +175,21 @@ def _prepare_note_context(user):
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
@@ -161,24 +208,32 @@ def user_home(request, user):
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
@@ -323,3 +378,65 @@ def generate_search_index(request):
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)