Commit c0d35764 authored by Daniel STAN's avatar Daniel STAN

pageperso: factorise calcul du login

parent 8541c5e3
......@@ -4,6 +4,7 @@ des infos sur sa page perso."""
from django import forms
from models import PagePerso
from django.utils.translation import ugettext_lazy as _
class PagePersoForm(forms.ModelForm):
"""ModelForm proposant un formulaire basé sur la classe
......@@ -29,10 +30,10 @@ class SqlPasswordForm(forms.Form):
def clean_password(self):
if len(self.cleaned_data["password"]) < 8:
raise forms.ValidationError("Le mot de passe doit faire au moins 8 caractères.")
raise forms.ValidationError(_(u"Le mot de passe doit faire au moins 8 caractères."))
return self.cleaned_data["password"]
def clean(self):
if "password" in self.cleaned_data:
if self.cleaned_data["password"] != self.cleaned_data.get("password_confirmation"):
raise forms.ValidationError("Les mots de passe ne sont pas identiques.")
raise forms.ValidationError(_(u"Les mots de passe ne sont pas identiques."))
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-02-22 17:47+0100\n"
"POT-Creation-Date: 2016-02-24 18:49+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -17,6 +17,14 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: forms.py:33
msgid "Le mot de passe doit faire au moins 8 caractères."
msgstr "The password must be contain at least 8 characters."
#: forms.py:39
msgid "Les mots de passe ne sont pas identiques."
msgstr "Passwords mismatch."
#: templates/pageperso/affichage.html:3 templates/pageperso/createdb.html:3
#: templates/pageperso/deletedb.html:3
msgid "Ma page Perso"
......@@ -124,11 +132,10 @@ msgstr "Really destroy the database? All data in will be irremediably lost"
msgid "supprimer"
msgstr "Delete"
#: views.py:43 views.py:58
#: views.py:49 views.py:64
msgid "Modifier l'affichage de ma page perso"
msgstr "Modify your personnal webpage display"
#: views.py:46 views.py:63
#: views.py:52 views.py:69
msgid "Référencer ma page perso"
msgstr "Index my personnal web page"
......@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-02-22 17:47+0100\n"
"POT-Creation-Date: 2016-02-24 18:49+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -18,6 +18,14 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: forms.py:33
msgid "Le mot de passe doit faire au moins 8 caractères."
msgstr ""
#: forms.py:39
msgid "Les mots de passe ne sont pas identiques."
msgstr ""
#: templates/pageperso/affichage.html:3 templates/pageperso/createdb.html:3
#: templates/pageperso/deletedb.html:3
msgid "Ma page Perso"
......@@ -113,10 +121,10 @@ msgstr ""
msgid "supprimer"
msgstr ""
#: views.py:43 views.py:58
#: views.py:49 views.py:64
msgid "Modifier l'affichage de ma page perso"
msgstr ""
#: views.py:46 views.py:63
#: views.py:52 views.py:69
msgid "Référencer ma page perso"
msgstr ""
......@@ -23,11 +23,17 @@ def test_backticks(name):
raise ValueError("%r should not contain '`'")
return name
def get_login(request, user):
"""Renvoie le login présumé de la page perso. Et vérifie que l'utilisateur
courant a bien le droit de faire des modifs dessus"""
return request.user.username
# Vue d'affichage du formulaire d'indexation de la pageperso
@login_required
@require_http_methods(["GET", "POST"])
def pageperso(request):
result = PagePerso.objects.filter(login=str(request.user))
def pageperso(request, user=None):
login = get_login(request, user)
result = PagePerso.objects.filter(login=login)
if request.method == "POST":
if result.exists():
form = PagePersoForm(request.POST, instance=result[0])
......@@ -38,7 +44,7 @@ def pageperso(request):
form.save()
else:
new_page = form.save(commit=False)
new_page.login = str(request.user)
new_page.login = login
new_page.save()
labelperso = _(u"Modifier l'affichage de ma page perso")
deref = True
......@@ -67,7 +73,7 @@ def pageperso(request):
with cursor() as cur:
if cur.execute(
"SELECT User From mysql.user WHERE User = %s",
(request.user.username,)
(login, )
):
# one user found, clean cursor
cur.fetchone()
......@@ -77,33 +83,35 @@ def pageperso(request):
"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA "
"WHERE SCHEMA_NAME = %s"
),
(request.user.username,)
(login, )
):
cur.execute(
(
"CREATE DATABASE `%s` CHARACTER SET utf8 COLLATE utf8_unicode_ci;"
) % test_backticks(request.user.username)
) % test_backticks(login)
)
cur.execute(
(
"GRANT ALL PRIVILEGES ON `%s`.* TO %%s@'localhost'"
) % test_backticks(request.user.username),
(request.user.username,)
) % test_backticks(login),
(login, )
)
else:
mysql_error = True
return render(request, "pageperso/affichage.html", locals())
@login_required
def deref(request):
result = PagePerso.objects.filter(login=str(request.user))
def deref(request, user=None):
login = get_login(request, user)
result = PagePerso.objects.filter(login=login)
result.delete()
return redirect("/pageperso/")
@login_required
@require_http_methods(["GET", "POST"])
def create_database(request):
def create_database(request, user=None):
login = get_login(request, user)
if cursor is None:
return redirect("pageperso:pageperso")
if request.method == "POST":
......@@ -114,13 +122,13 @@ def create_database(request):
(
"CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET utf8 "
"COLLATE utf8_unicode_ci;"
) % test_backticks(request.user.username)
) % test_backticks(login)
)
cur.execute(
(
"GRANT ALL PRIVILEGES ON `%s`.* TO %%s@'localhost' IDENTIFIED BY %%s"
) % test_backticks(request.user.username),
(request.user.username, form.cleaned_data["password"])
) % test_backticks(login),
(login, form.cleaned_data["password"])
)
return redirect("pageperso:pageperso")
elif request.method == "GET":
......@@ -130,7 +138,8 @@ def create_database(request):
@login_required
@require_http_methods(["GET", "POST"])
def reset_db_password(request):
def reset_db_password(request, user=None):
login = get_login(request, user)
if cursor is None:
return redirect("pageperso:pageperso")
if request.method == "POST":
......@@ -139,12 +148,12 @@ def reset_db_password(request):
with cursor() as cur:
if cur.execute(
"SELECT User From mysql.user WHERE User = %s",
(request.user.username,)
(login, )
):
cur.fetchone()
cur.execute(
"SET PASSWORD FOR %s@'localhost' = PASSWORD(%s);",
(request.user.username, form.cleaned_data["password"],)
(login, form.cleaned_data["password"],)
)
return redirect("pageperso:pageperso")
elif request.method == "GET":
......@@ -154,13 +163,14 @@ def reset_db_password(request):
@login_required
@require_http_methods(["GET", "POST"])
def delete_database(request):
def delete_database(request, user=None):
login = get_login(request, user)
if cursor is None:
return redirect("pageperso:pageperso")
if request.method == "POST":
if request.POST.get("confirm") == "yes":
with cursor() as cur:
cur.execute("DROP DATABASE IF EXISTS `%s`" % test_backticks(request.user.username))
cur.execute("DROP USER %s@'localhost'", (request.user.username,))
cur.execute("DROP DATABASE IF EXISTS `%s`" % test_backticks(login))
cur.execute("DROP USER %s@'localhost'", (login, ))
return redirect("pageperso:pageperso")
return render(request, "pageperso/deletedb.html")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment