Commit e8c9d8e9 authored by Vincent Le gallic's avatar Vincent Le gallic

Implémentation des retraits

parent 15ef8e9d
......@@ -343,6 +343,7 @@ class InscriptionForm(forms.Form):
return out
class PhotoForm(forms.Form):
"""Formulaire d'envoi de photo"""
photo = forms.FileField(label="Fichier photo")
def clean(self):
"""On n'autorise pas les photos trop grosses."""
......@@ -353,8 +354,9 @@ class PhotoForm(forms.Form):
raise forms.ValidationError(u"Photo trop volumineuse (%s octets), maximum %s" % (photo.size, settings.MAX_PHOTO_SIZE))
return out
class CreditForm(forms.Form):
destinataire = forms.IntegerField(widget=forms.HiddenInput)
class CreditRetraitForm(forms.Form):
"""Formulaire pour effectuer un crédit ou un retrait."""
idbde = forms.IntegerField(widget=forms.HiddenInput)
montant = FrenchFloatField(label="Montant")
type = forms.ChoiceField(label="Type de paiement", choices=[("especes", "Espèces"), ("cheque", "Chèque"), ("virement", "Virement bancaire")])
nom = forms.CharField(label="Nom", required=False)
......
......@@ -43,7 +43,7 @@ urlpatterns = patterns('note.views',
url(ur'^/get_boutons/*$', 'get_boutons'),
url(ur'^/get_photo/(?P<idbde>[^/]*)/*$', 'get_photo'),
url(ur'^/do_conso/*$', 'do_conso'),
url(ur'^/do_credit/*$', 'do_credit'),
url(ur'^/do_(?P<action>credit|retrait)/*$', 'do_credit'),
# easter egg
url(ur'^/(?:teapot|th(?:é|e)|coffee|caf(?:é|e))/*$', 'teapot'),
)
......@@ -608,7 +608,9 @@ def consos(request):
else:
_add_error(request, out["errmsg"])
# Le formulaire de Crédit
variables["credit_form"] = CreditForm(prefix="credit_form", label_suffix=" :")
variables["credit_form"] = CreditRetraitForm(prefix="credit_form", label_suffix=" :")
# Le formulaire de Retrait
variables["retrait_form"] = CreditRetraitForm(prefix="retrait_form", label_suffix=" :")
variables.update(variables_standard)
return render_to_response('note/consos.html', variables, context_instance=RequestContext(request))
else:
......@@ -617,26 +619,28 @@ def consos(request):
return response
@csrf_exempt
def do_credit(request):
def do_credit(request, action):
types = {"especes": "Espèces", "cheque": "Chèque", "virement": "Virement bancaire"}
actions_write = {"credit": "crédit", "retrait": "retrait"}
actions_socket = {"credit": "crediter", "retrait": "retirer"}
# On appelle la fonction standard
success, sock_ou_response, variables_standard = standard_page(request, socket=True)
if success:
sock = sock_ou_response
# on n'a pas besoin du prefix parce que le JS a filé les paramètre sans.
credit_form = CreditForm(request.POST, label_suffix=" :")
if credit_form.is_valid():
data = credit_form.cleaned_data
form = CreditRetraitForm(request.POST, label_suffix=" :")
if form.is_valid():
data = form.cleaned_data
if data["type"] != "especes" and "" in [data["nom"], data["prenom"], data["banque"]]:
types = {"especes": "Espèces", "cheque": "Chèque", "virement": "Virement bancaire"}
return HttpResponse("""Pour un crédit par %s, les champs Nom, Prénom et Banque doivent être spécifiés.""" % types[data["type"]])
to_send = [data["destinataire"], data["montant"], data["type"],
return HttpResponse("""Pour un %s par %s, les champs Nom, Prénom et Banque doivent être spécifiés.""" % (actions_write[action], types[data["type"]]))
to_send = [data["idbde"], data["montant"], data["type"],
{"nom": data["nom"], "prenom": data["prenom"], "banque": data["banque"]}]
sock.write("crediter %s" % json.dumps(to_send))
sock.write("%s %s" % (actions_socket[action], json.dumps(to_send)))
out = full_read(sock)
return HttpResponse(json.dumps(out))
else:
errmsg = "Ce crédit est invalide :\n"
for (k,v) in credit_form.errors.items():
errmsg = "Ce %s est invalide :\n" % (actions_write[action])
for (k,v) in form.errors.items():
errmsg += "%s : %s\n" % (k,"".join([str(i) for i in v]))
return HttpResponse(errmsg)
else:
......
......@@ -87,15 +87,19 @@ function put_note(itself, affiche, idbde, nom, prenom, secondstack) {
display_stack_used[affiche]["nb"] += 1;
}
refreshStack(secondstack);
/* On remplit aussi les champs du formulaire de crédit */
var pseudo_destinataire = document.getElementById("credit_pseudo_destinataire");
pseudo_destinataire.replaceChild(itself.cloneNode(true), pseudo_destinataire.firstChild);
var nom_field = document.getElementById("id_credit_form-nom");
nom_field.value = nom;
var prenom_field = document.getElementById("id_credit_form-prenom");
prenom_field.value = prenom;
var destinataire_field = document.getElementById("id_credit_form-destinataire");
destinataire_field.value = idbde;
/* On remplit aussi les champs des formulaires de crédit et de retrait */
var target, pseudo_field, nom_field, prenom_field, idbde_field;
for (var i_target in [0, 1]) {
target = ["credit", "retrait"][i_target];
pseudo_field = document.getElementById(target + "_pseudo");
pseudo_field.replaceChild(itself.cloneNode(true), pseudo_field.firstChild);
nom_field = document.getElementById("id_" + target + "_form-nom");
nom_field.value = nom;
prenom_field = document.getElementById("id_" + target + "_form-prenom");
prenom_field.value = prenom;
idbde_field = document.getElementById("id_" + target + "_form-idbde");
idbde_field.value = idbde;
}
}
/* fonction appelée quand on clique sur une note déjà dans le stack */
......@@ -216,16 +220,17 @@ function refreshStackButton() {
/* Fonction de nettoyage des fomulaires */
function cleanForms() {
/* On vide les forms */
var ids = ["id_credit_form-destinataire", "id_credit_form-montant", "id_credit_form-type", "id_credit_form-nom",
"id_credit_form-prenom", "id_credit_form-banque"];
var ids = ["idbde", "montant", "type", "nom", "prenom", "banque"];
var id, field;
for (var i_id = 0, c = ids.length; i_id < ids.length; i_id++) {
id = ids[i_id];
field = document.getElementById(id);
field.value = null;
for (var target in [0, 1]) {
for (var i_id = 0, c = ids.length; i_id < ids.length; i_id++) {
id = "id_" + ["credit", "retrait"][target] + "_form-" + ids[i_id];
field = document.getElementById(id);
field.value = null;
}
}
/* On vide aussi les zones de texte */
var span_ids = ["credit_pseudo_destinataire"];
var span_ids = ["credit_pseudo", "retrait_pseudo"];
var emptyspan = document.createElement("span");
var span;
for (var i_id = 0, c = span_ids.length; i_id < span_ids.length; i_id++) {
......@@ -397,21 +402,27 @@ function displayAccount(objetnote, note, idbde, solde) {
xhr.send(null);
}
/*** Crédits ***/
/* Fonction qui va chercher le contenu des champs du formulaire de Crédit et envoie le crédit */
function crediter() {
var destinataire_field = document.getElementById("id_credit_form-destinataire");
var montant_field = document.getElementById("id_credit_form-montant");
var type_field = document.getElementById("id_credit_form-type");
var nom_field = document.getElementById("id_credit_form-nom");
var prenom_field = document.getElementById("id_credit_form-prenom");
var banque_field = document.getElementById("id_credit_form-banque");
/*** Crédits et Retraits ***/
/* Fonction qui va chercher le contenu des champs du formulaire et envoie le crédit/retrait */
function crediter_ou_retirer(credit) {
if (credit) {
var target = 'credit';
}
else {
var target = 'retrait';
}
var idbde_field = document.getElementById("id_" + target + "_form-idbde");
var montant_field = document.getElementById("id_" + target + "_form-montant");
var type_field = document.getElementById("id_" + target + "_form-type");
var nom_field = document.getElementById("id_" + target + "_form-nom");
var prenom_field = document.getElementById("id_" + target + "_form-prenom");
var banque_field = document.getElementById("id_" + target + "_form-banque");
/* On envoie la requête grâce à jQuery */
jQuery.ajax({
url: "/note/do_credit/",
url: "/note/do_" + target + "/",
type: "POST",
data: ({destinataire: destinataire_field.value,
data: ({idbde: idbde_field.value,
montant: montant_field.value,
type: type_field.value,
nom: nom_field.value,
......
......@@ -56,14 +56,18 @@
<p>Transfert...</p>
</div>
<div class="tab-pane" id="credit">
Créditer <span id="credit_pseudo_destinataire"><span></span></span> :
<form onsubmit="crediter(); return false;"> <!-- pas d'action, on fait du JS -->
<form onsubmit="crediter_ou_retirer(true); return false;"> {# true = crédit #}
<legend>Créditer <span id="credit_pseudo"><span></span></span> :</legend>
{{ credit_form.as_p }}
<input type="submit" class="btn btn-primary" value="Créditer"/>
</form>
</div>
<div class="tab-pane" id="retrait">
<p>Donne-moi du cash...</p>
<form onsubmit="crediter_ou_retirer(false); return false;"> {# false = retrait #}
<legend>Retirer de l'argent à <span id="retrait_pseudo"><span></span></span> :</legend>
{{ retrait_form.as_p }}
<input type="submit" class="btn btn-primary" value="Retirer"/>
</form>
</div>
{% for categorie, listeboutons in boutons %}
<div class="tab-pane" id="{{categorie.lower}}">
......
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