# -*- 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 . """ import os from datetime import datetime from django.http import HttpResponseRedirect, HttpResponse, Http404 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 * from denote.search import * 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() } 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} 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: 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 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}) c = {'user_to_edit' : user, 'form' : form, 'edited' : edited} return render(request, 'edit_user.html', c) def _prepare_note_context(user): 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') context = { 'user': user, 'notes_by_category': notes_by_category, 'categories': categories, 'notes_without_category': notes_without_category, } 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 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('/') else: form = NoteForm() context = _prepare_note_context(user) context['note_form'] = form context['note'] = None 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) @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): print request.method if request.method != 'POST': raise Http404 print request.POST.items() 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 @login_required 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) notes = Note.objects.filter(pk__in=note_list, author=request.user) context['notes'] = notes context['note_form'] = NoteForm() return render(request, 'user_index.html', context) @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('/')