summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2008-04-07 18:22:17 +0200
committerGuillaume Seguin <guillaume@segu.in>2008-04-07 18:22:17 +0200
commit388db88b90905adaee7baf5bfff959c8a17eef82 (patch)
treed6211b544898c279d47498185e257832f0d92e4e
parent48930f05619e4a73c1b5adeefbe08cf266462992 (diff)
downloadmarks-388db88b90905adaee7baf5bfff959c8a17eef82.tar.gz
marks-388db88b90905adaee7baf5bfff959c8a17eef82.tar.bz2
* Add view to import user data
-rw-r--r--main/forms.py3
-rw-r--r--main/views.py57
-rw-r--r--media/css/main.css30
-rw-r--r--templates/layout.html2
-rw-r--r--templates/main/home.html37
-rw-r--r--urls.py1
6 files changed, 125 insertions, 5 deletions
diff --git a/main/forms.py b/main/forms.py
index 14d9c1a..0ab9cca 100644
--- a/main/forms.py
+++ b/main/forms.py
@@ -24,6 +24,9 @@ def clean_name (self, key, error):
raise forms.ValidationError (error)
return self.cleaned_data["name"]
+class ImportForm (forms.Form):
+ file = forms.FileField (label = "Fichier", required = True)
+
class AddExaminationForm (forms.Form):
name = forms.CharField (label = "Nom", required = True)
diff --git a/main/views.py b/main/views.py
index 1320793..144bc8f 100644
--- a/main/views.py
+++ b/main/views.py
@@ -26,8 +26,10 @@ except ImportError:
from datetime import datetime
from marks.main.shared import redirect, render_page, render_template, \
- get_user_data, set_user_data, reset_user_data
-from marks.main.forms import AddExaminationForm, AddRatingsForm, AddMarksForm
+ get_user_data, set_user_data, \
+ reset_user_data, make_initial_user_data
+from marks.main.forms import ImportForm, AddExaminationForm, \
+ AddRatingsForm, AddMarksForm
def home_view (request):
'''Render the main page'''
@@ -70,6 +72,7 @@ def home_view (request):
add_ratings_form = AddRatingsForm (user_data)
add_marks_form = AddMarksForm (user_data)
data = {
+ "import_form": ImportForm (),
"add_examination_form": add_examination_form,
"add_ratings_form": add_ratings_form,
"add_marks_form": add_marks_form,
@@ -82,7 +85,7 @@ def home_view (request):
def reset_view (request):
'''Free user data'''
reset_user_data (request)
- return redirect (request, "/", "Session was successfully reset.")
+ return redirect (request, "/", "Session réinitialisée.")
def export_view (request):
'''Export user data'''
@@ -94,6 +97,54 @@ def export_view (request):
response.write (data)
return response
+def import_view (request):
+ '''Import user data'''
+ def fail ():
+ return redirect (request, "/",
+ "Impossible d'effectuer l'importation")
+ def is_string (str):
+ return type (str) in [type (""), type (u"")]
+ if not request.method == "POST":
+ return home_view (request)
+ form = ImportForm (request.POST, request.FILES)
+ if not form.is_valid ():
+ return fail ()
+ data = form.cleaned_data["file"].content
+ try:
+ data = pickle.loads (data)
+ except:
+ return fail ()
+ user_data = make_initial_user_data ()
+ if "exams" in data and type (data["exams"]) == type ([]):
+ for exam in data["exams"]:
+ if not is_string (exam):
+ continue
+ user_data["exams"].append (exam)
+ if "ratings" in data and type (data["ratings"]) == type ({}):
+ for ratings_set in data["ratings"]:
+ if not is_string (ratings_set):
+ continue
+ user_data["ratings"][ratings_set] = {}
+ for exam in user_data["exams"]:
+ if exam in data["ratings"][ratings_set]:
+ rating = data["ratings"][ratings_set][exam]
+ if type (rating) != type (1):
+ continue
+ user_data["ratings"][ratings_set][exam] = rating
+ if "marks" in data and type (data["marks"]) == type ({}):
+ for marks_set in data["marks"]:
+ if not is_string (marks_set):
+ continue
+ user_data["marks"][marks_set] = {}
+ for exam in user_data["exams"]:
+ if exam in data["marks"][marks_set]:
+ mark = data["marks"][marks_set][exam]
+ if type (mark) != type (1.0):
+ continue
+ user_data["marks"][marks_set][exam] = mark
+ set_user_data (request, user_data)
+ return home_view (request)
+
def get_rating (user_data, exam, name):
'''Helper function returning the stored rating if it exists, 0 otherwise'''
if exam in user_data["ratings"][name]:
diff --git a/media/css/main.css b/media/css/main.css
index 71e186b..6125f47 100644
--- a/media/css/main.css
+++ b/media/css/main.css
@@ -30,8 +30,10 @@ ul
padding-left: 1em;
list-style-type: square;
}
-input
+input, button
{
+ margin-left: 0.2em;
+ margin-right: 0.2em;
border: 1px solid #FFDEAD;
padding: 0.1em;
background-color: #B57241;
@@ -41,6 +43,32 @@ input[type="radio"], input.radio
{
vertical-align: middle;
}
+.fileform, .fileinput
+{
+ display: inline;
+}
+.fileinput
+{
+ position: relative;
+}
+.fakefile
+{
+ position: absolute;
+ margin: 0;
+ padding: 0;
+ top: -1px;
+ left: 0px;
+ z-index: 1;
+}
+.fileform #id_file
+{
+ position: relative;
+ text-align: right;
+ -moz-opacity: 0;
+ filter: alpha(opacity: 0);
+ opacity: 0;
+ z-index: 2;
+}
.spacer
{
clear: both;
diff --git a/templates/layout.html b/templates/layout.html
index 9a4e2b7..3a5f125 100644
--- a/templates/layout.html
+++ b/templates/layout.html
@@ -9,7 +9,7 @@
{% block head %}{% endblock %}
<title>{% block title %}Marks{% endblock %}</title>
</head>
- <body>
+ {% block body %}<body>{% endblock %}
<div id="page">
{% block page %}{% endblock %}
</div>
diff --git a/templates/main/home.html b/templates/main/home.html
index f476504..90b6107 100644
--- a/templates/main/home.html
+++ b/templates/main/home.html
@@ -1,10 +1,47 @@
{% extends "main/base.html" %}
{% load marks_tags %}
+{% block head %}
+<script type="text/javascript">
+var W3CDOM = (document.createElement && document.getElementsByTagName);
+
+function init () {
+ if (!W3CDOM) return;
+ var fakeFileUpload = document.createElement ('div');
+ fakeFileUpload.className = 'fakefile';
+ fakeFileUpload.appendChild (document.createElement('input'));
+ var button = document.createElement ('button');
+ button.innerHTML = "Parcourir";
+ fakeFileUpload.appendChild (button);
+ var x = document.getElementsByTagName ('input');
+ for (var i = 0; i < x.length; i++)
+ {
+ if (x[i].type != 'file') continue;
+ if (x[i].parentNode.className != 'fileinput') continue;
+ x[i].className = 'file hidden';
+ var clone = fakeFileUpload.cloneNode (true);
+ x[i].parentNode.appendChild (clone);
+ x[i].relatedElement = clone.getElementsByTagName ('input')[0];
+ x[i].onchange = x[i].onmouseout = function () {
+ this.relatedElement.value = this.value;
+ }
+ }
+}
+</script>
+{% endblock %}
+
+{% block body %}<body onload="javascript:init ();">{% endblock %}
+
{% block content %}
{% rounded_block_start 'fullblock' 'center' 'menu' %}{% end_rounded_block_start %}
<a href="{% url marks.main.views.reset_view %}">Réinitialiser</a>
| <a href="{% url marks.main.views.export_view %}">Exporter</a>
+| <form class="fileform" enctype="multipart/form-data" method="post" action="{% url marks.main.views.import_view %}">
+ <div class="fileinput">
+ {{ import_form.file }}
+ </div>
+ <input type="submit" value="Importer" />
+ </form>
{% rounded_block_end %}
{% rounded_block_start 'halfblock' %}Ajouter une épreuve{% end_rounded_block_start %}
<div class="blockcontent">
diff --git a/urls.py b/urls.py
index 3be3d92..7e5d5a5 100644
--- a/urls.py
+++ b/urls.py
@@ -23,6 +23,7 @@ urlpatterns = patterns('',
(r'^$', 'marks.main.views.home_view'),
(r'^reset$', 'marks.main.views.reset_view'),
(r'^export$', 'marks.main.views.export_view'),
+ (r'^import$', 'marks.main.views.import_view'),
(r'^view/exam/(.+)$', 'marks.main.views.exam_view'),
(r'^view/ratings/(.+)$', 'marks.main.views.ratings_view'),
(r'^view/marks/(.+)$', 'marks.main.views.marks_view'),