Denote/denote/views.py

443 lines
14 KiB
Python
Raw Normal View History

2015-09-24 18:31:03 +02:00
# -*- coding: utf-8 -*-
"""
Copyright 2015 Grégory Soutadé
This file is part of Dénote.
Dénote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dénote is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dénote. If not, see <http://www.gnu.org/licenses/>.
"""
2016-01-17 18:48:14 +01:00
import os
2015-09-24 18:31:03 +02:00
from datetime import datetime
2016-06-14 11:09:11 +02:00
from django.http import HttpResponseRedirect, HttpResponse, Http404, HttpResponseForbidden
2015-09-24 18:31:03 +02:00
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render
from denote.models import *
from denote.forms import *
2016-01-17 18:48:14 +01:00
from denote.search import *
2015-09-24 18:31:03 +02:00
def index(request):
if request.user.is_authenticated():
return user_home(request, request.user)
login_failed = False
if 'login' in request.POST:
user = authenticate(username=request.POST['login'], password=request.POST['password'])
if user is None:
login_failed = True
else:
login(request, user)
if 'next' in request.GET:
return HttpResponseRedirect(request.GET['next'])
elif 'next' in request.POST:
return HttpResponseRedirect(request.POST['next'])
else:
return user_home(request, request.user)
c = {
'login_failed' : login_failed,
'nb_people_registered' : User.objects.all().count()
}
2015-09-24 18:31:03 +02:00
return render(request, 'login.html', c)
def disconnect(request):
user = request.user
if not user is None:
logout(request)
return HttpResponseRedirect('/')
def new_user(request):
login_val = 'login' in request.POST and request.POST['login'] or ''
password = 'password' in request.POST and request.POST['password'] or ''
if request.method == 'POST':
if 'add' in request.POST:
form = UserForm(request.POST)
if form.is_valid():
form = form.save()
user = User.objects.get(pk=form.id)
user.set_password(request.POST['password'])
user.save()
user = authenticate(username=user.username, password=request.POST['password'])
login(request, user)
return user_home(request, user)
else:
return HttpResponseRedirect('/')
else:
form = UserForm()
c = {'login' : login_val, 'password' : password, 'form': form}
2016-06-30 20:13:15 +02:00
2015-09-24 18:31:03 +02:00
return render(request, 'add_user.html', c)
@login_required
def edit_user(request):
user = request.user
edited = False
if request.method == 'POST':
if 'edit' in request.POST:
2016-06-30 20:13:15 +02:00
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
2015-09-24 18:31:03 +02:00
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()
2016-06-30 20:13:15 +02:00
edited = True
2015-09-24 18:31:03 +02:00
else:
if 'delete' in request.POST:
logout(request)
User.objects.filter(pk=user.id).delete()
return HttpResponseRedirect('/')
else:
login = 'login' in request.POST and request.POST['login'] or ''
form = UserForm(instance=user, initial={'password':'', 'login':login})
2016-06-30 20:13:15 +02:00
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}
2016-06-30 20:13:15 +02:00
c['templates_by_name'] = templates_by_name
2015-09-24 18:31:03 +02:00
return render(request, 'edit_user.html', c)
2016-06-30 20:13:15 +02:00
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
2015-09-24 18:31:03 +02:00
def _prepare_note_context(user):
2016-06-14 11:09:11 +02:00
if not user.is_authenticated():
return {
'authenticated' : False,
}
2015-09-24 18:31:03 +02:00
categories = Category.objects.filter(author=user.id).order_by('name')
notes_by_category = []
need_refresh = False
for category in categories:
meta_note = {}
meta_note['category'] = category.name
meta_note['category_id'] = category.id
meta_note['notes'] = Note.objects.filter(author=user,category=category).order_by('-modified_date')
if meta_note['notes']:
notes_by_category.append(meta_note)
else:
category.delete()
need_refresh = True
if need_refresh:
categories = Category.objects.filter(author=user.id).order_by('name')
notes_without_category = Note.objects.filter(author=user,category=None).order_by('-modified_date')
2016-06-30 20:13:15 +02:00
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)
2015-09-24 18:31:03 +02:00
context = {
'user': user,
2016-06-14 11:09:11 +02:00
'authenticated' : True,
2015-09-24 18:31:03 +02:00
'notes_by_category': notes_by_category,
'categories': categories,
'notes_without_category': notes_without_category,
2016-06-30 20:13:15 +02:00
'templates_by_name': templates_by_name,
2015-09-24 18:31:03 +02:00
}
return context
@login_required
def user_home(request, user):
context = _prepare_note_context(user)
notes = Note.objects.filter(author=user.id).order_by('-modified_date')[:20]
context['notes'] = notes
context['note_form'] = NoteForm()
return render(request, 'user_index.html', context)
@login_required
def add_note(request):
user = request.user
2016-06-30 20:13:15 +02:00
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('/')
2015-09-24 18:31:03 +02:00
else:
2016-06-30 20:13:15 +02:00
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)
2016-06-14 11:09:11 +02:00
form = NoteForm(instance=note)
2015-09-24 18:31:03 +02:00
context = _prepare_note_context(user)
context['note_form'] = form
context['note'] = None
2016-06-30 20:13:15 +02:00
if note.category:
context['category'] = note.category.name
2015-09-24 18:31:03 +02:00
return render(request, 'user_note.html', context)
@login_required
def note(request, note_id):
user = request.user
note = Note.objects.get(pk=note_id, author=user)
if note is None:
raise Http404
form = NoteForm(instance=note)
if request.method == 'POST':
if 'edit' in request.POST:
note.category = manage_category(user, request.POST['category'])
form = NoteForm(request.POST, instance=note)
if form.is_valid():
form.save()
else:
if 'delete' in request.POST:
note.delete()
return HttpResponseRedirect('/')
context = _prepare_note_context(user)
context['note'] = note
context['note_form'] = form
return render(request, 'user_note.html', context)
2016-06-14 11:09:11 +02:00
def public_note(request, user_id, note_id):
user = request.user
try:
note = Note.objects.get(pk=note_id, author=user_id)
except:
raise Http404
if note is None:
raise Http404
if not user or not user.is_authenticated():
if note.visibility != Note.PUBLIC:
return HttpResponseForbidden()
else:
if note.visibility == Note.PRIVATE and\
user_id != user.id:
return HttpResponseForbidden()
if user.is_authenticated():
public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.REGISTERED).order_by('-modified_date')
else:
public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.PUBLIC).order_by('-modified_date')
context = _prepare_note_context(user)
context['note'] = note
context['public_notes'] = public_notes
return render(request, 'public_note.html', context)
2016-06-14 11:28:37 +02:00
def public_notes(request):
user = request.user
if user.is_authenticated():
public_notes = Note.objects.filter(visibility__gte=Note.REGISTERED).order_by('-modified_date')
else:
public_notes = Note.objects.filter(visibility__gte=Note.PUBLIC).order_by('-modified_date')
context = _prepare_note_context(user)
context['notes'] = public_notes[:50]
context['public_notes'] = public_notes[:50]
return render(request, 'public_notes.html', context)
2015-09-24 18:31:03 +02:00
@login_required
def edit_category(request, category_id):
user = request.user
category = Category.objects.get(pk=category_id, author=user)
if category is None:
raise Http404
if request.method == 'POST':
if not 'new_cat_name' in request.POST or \
not request.POST['new_cat_name']:
return HttpResponseRedirect('/')
category.name = request.POST['new_cat_name'].strip()
if len(category.name) > 50: category.name = category.name[:50]
category.author = user
try:
category.save()
except:
pass
return HttpResponseRedirect('/')
@login_required
def preferences(request):
if request.method != 'POST':
raise Http404
if 'get' in request.POST and 'name' in request.POST:
return request.user.getPreference(request.POST['name'])
elif 'set' in request.POST and 'name' in request.POST and \
'value' in request.POST:
return request.user.setPreference(request.POST['name'], request.POST['value'])
else:
raise Http404
2016-01-17 18:48:14 +01:00
def search(request):
context = _prepare_note_context(request.user)
ref = request.META['HTTP_REFERER']
if 'text' in request.POST:
text = request.POST['text']
else:
return HttpResponseRedirect(ref)
s = Search()
note_list = s.search(text)
2016-06-14 11:09:11 +02:00
if request.user.is_authenticated():
notes = Note.objects.filter(pk__in=note_list, author=request.user)
2016-01-17 18:48:14 +01:00
2016-06-14 11:09:11 +02:00
context['notes'] = notes
context['note_form'] = NoteForm()
return render(request, 'user_index.html', context)
else:
notes = Note.objects.filter(pk__in=note_list, visibility__gte=Note.PUBLIC)
context['notes'] = notes
return render(request, 'public_notes.html', context)
2016-01-17 18:48:14 +01:00
@login_required
def generate_search_index(request):
if os.path.exists('_search.db'):
os.path.remove('_search.db')
s = Search()
s.generate_index(Note.objects.all())
return HttpResponseRedirect('/')
2016-06-30 20:13:15 +02:00
@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)