forked from DGNum/gestioCOF
Création compte avec balance initiale
- Utile pour la migration des anciens comptes vers le nouveau système - Fix JS page historique
This commit is contained in:
parent
a78ad8c4ef
commit
f538f27843
7 changed files with 207 additions and 1 deletions
|
@ -54,6 +54,11 @@ class AccountForm(forms.ModelForm):
|
|||
'trigramme': forms.TextInput(attrs={'autocomplete': 'off'}),
|
||||
}
|
||||
|
||||
class AccountBalanceForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Account
|
||||
fields = ['balance']
|
||||
|
||||
class AccountTriForm(AccountForm):
|
||||
|
||||
def clean_trigramme(self):
|
||||
|
|
23
kfet/migrations/0045_auto_20160905_0705.py
Normal file
23
kfet/migrations/0045_auto_20160905_0705.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('kfet', '0044_auto_20160901_1614'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='globalpermissions',
|
||||
options={'managed': False, 'permissions': (('is_team', 'Is part of the team'), ('perform_deposit', 'Effectuer une charge'), ('perform_negative_operations', 'Enregistrer des commandes en n\xe9gatif'), ('override_frozen_protection', "Forcer le gel d'un compte"), ('cancel_old_operations', 'Annuler des commandes non r\xe9centes'), ('manage_perms', 'G\xe9rer les permissions K-F\xeat'), ('manage_addcosts', 'G\xe9rer les majorations'), ('perform_commented_operations', 'Enregistrer des commandes avec commentaires'), ('view_negs', 'Voir la liste des n\xe9gatifs'), ('order_to_inventory', "G\xe9n\xe9rer un inventaire \xe0 partir d'une commande"), ('edit_balance_account', "Modifier la balance d'un compte"), ('change_account_password', "Modifier le mot de passe d'une personne de l'\xe9quipe"), ('special_add_account', 'Cr\xe9er un compte avec une balance initiale'))},
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='operation',
|
||||
name='type',
|
||||
field=models.CharField(max_length=8, choices=[('purchase', 'Achat'), ('deposit', 'Charge'), ('withdraw', 'Retrait'), ('initial', 'Initial')]),
|
||||
),
|
||||
]
|
|
@ -508,11 +508,13 @@ class Operation(models.Model):
|
|||
PURCHASE = 'purchase'
|
||||
DEPOSIT = 'deposit'
|
||||
WITHDRAW = 'withdraw'
|
||||
INITIAL = 'initial'
|
||||
|
||||
TYPE_ORDER_CHOICES = (
|
||||
(PURCHASE, 'Achat'),
|
||||
(DEPOSIT, 'Charge'),
|
||||
(WITHDRAW, 'Retrait'),
|
||||
(INITIAL, 'Initial'),
|
||||
)
|
||||
|
||||
group = models.ForeignKey(
|
||||
|
@ -563,6 +565,7 @@ class GlobalPermissions(models.Model):
|
|||
('order_to_inventory', "Générer un inventaire à partir d'une commande"),
|
||||
('edit_balance_account', "Modifier la balance d'un compte"),
|
||||
('change_account_password', "Modifier le mot de passe d'une personne de l'équipe"),
|
||||
('special_add_account', "Créer un compte avec une balance initiale")
|
||||
)
|
||||
|
||||
class Settings(models.Model):
|
||||
|
|
96
kfet/templates/kfet/account_create_special.html
Normal file
96
kfet/templates/kfet/account_create_special.html
Normal file
|
@ -0,0 +1,96 @@
|
|||
{% extends "kfet/base.html" %}
|
||||
{% load staticfiles %}
|
||||
|
||||
{% block title %}Nouveau compte{% endblock %}
|
||||
|
||||
{% block extra_head %}
|
||||
<script src="{% static "autocomplete_light/autocomplete.js" %}" type="text/javascript"></script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content-header-title %}Création d'un compte{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
{% include 'kfet/base_messages.html' %}
|
||||
|
||||
<div class="row form-only">
|
||||
<div class="col-sm-12 col-md-8 col-md-offset-2">
|
||||
<div class="content-form">
|
||||
<form action="{% url "kfet.account.create_special" %}" method="post" class="account_create">
|
||||
{% csrf_token %}
|
||||
<div>
|
||||
{{ trigramme_form.trigramme.errors }}
|
||||
{{ trigramme_form.trigramme }}
|
||||
{{ balance_form }}
|
||||
</div>
|
||||
<div id="trigramme_valid"></div>
|
||||
<input type="text" name="q" id="search_autocomplete" spellcheck="false" placeholder="Chercher un utilisateur par nom, prénom ou identifiant clipper" class="form-control">
|
||||
<div style="position:relative;">
|
||||
<div id="search_results"></div>
|
||||
</div>
|
||||
<div class="form-horizontal">
|
||||
<div id="form-placeholder">
|
||||
{% include 'kfet/account_create_form.html' %}
|
||||
</div>
|
||||
{% if not perms.kfet.add_account %}
|
||||
{% include 'kfet/form_authentication_snippet.html' %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
// Affichage des résultats d'autocomplétion
|
||||
$('input#search_autocomplete').yourlabsAutocomplete({
|
||||
url: '{% url "kfet.account.create.autocomplete" %}',
|
||||
minimumCharacters: 0,
|
||||
id: 'search_autocomplete',
|
||||
choiceSelector: 'li:has(a)',
|
||||
container: $("#search_results"),
|
||||
box: $("#search_results"),
|
||||
});
|
||||
|
||||
// Chargement du formulaire adapté au choix sélectionné
|
||||
$('input#search_autocomplete').bind(
|
||||
'selectChoice',
|
||||
function(e, choice, autocomplete) {
|
||||
autocomplete.hide();
|
||||
link = choice.find('a:first');
|
||||
if (link.length && link.attr('href') != undefined) {
|
||||
$('#form-placeholder').html("").load(link.attr('href'));
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Vérification client de la validité
|
||||
// et de ladisponibilité du trigramme choisi
|
||||
$('#id_trigramme').on('input', function() {
|
||||
var trigramme = $('#id_trigramme').val().toUpperCase();
|
||||
var container = '#trigramme_valid';
|
||||
|
||||
var pattern = /^[^a-z]{3}$/;
|
||||
if (!(trigramme.match(pattern))) {
|
||||
$('#id_trigramme')
|
||||
.css('background', '#fff')
|
||||
.css('color', '#000');
|
||||
} else {
|
||||
$.ajax({
|
||||
dataType: "json",
|
||||
url: "{% url "kfet.account.is_validandfree.ajax" %}",
|
||||
data: { trigramme: trigramme },
|
||||
}).done(function(data) {
|
||||
if (data['is_free']) {
|
||||
$('#id_trigramme').css('background', '#009011');
|
||||
} else {
|
||||
$('#id_trigramme').css('background', '#C8102E');
|
||||
}
|
||||
$('#id_trigramme').css('color', '#fff');
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
|
@ -212,9 +212,11 @@ $(document).ready(function() {
|
|||
content += '<li>'+data['errors']['missing_perms'][i]+'</li>';
|
||||
content += '</ul>';
|
||||
}
|
||||
if ('negative' in data['errors'])
|
||||
if ('negative' in data['errors']) {
|
||||
for (var i=0; i<data['errors']['negative'].length; i++) {
|
||||
content += '<a class="btn btn-primary" href="/k-fet/accounts/'+data['errors']['negative'][i]+'/edit" target="_blank">Autorisation de négatif requise pour '+data['errors']['negative'][i]+'</a>';
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
|
|
|
@ -31,6 +31,8 @@ urlpatterns = [
|
|||
# Account - Create
|
||||
url(r'^accounts/new$', views.account_create,
|
||||
name = 'kfet.account.create'),
|
||||
url(r'^accounts/new_special$', views.account_create_special,
|
||||
name = 'kfet.account.create_special'),
|
||||
url(r'^accounts/new/user/(?P<username>.+)$', views.account_create_ajax,
|
||||
name = 'kfet.account.create.fromuser'),
|
||||
url(r'^accounts/new/clipper/(?P<login_clipper>.+)$', views.account_create_ajax,
|
||||
|
|
|
@ -83,6 +83,81 @@ def account_is_validandfree_ajax(request):
|
|||
|
||||
# Account - Create
|
||||
|
||||
@login_required
|
||||
@teamkfet_required
|
||||
def account_create_special(request):
|
||||
|
||||
# Enregistrement
|
||||
if request.method == "POST":
|
||||
trigramme_form = AccountTriForm(request.POST, initial={'balance':0})
|
||||
balance_form = AccountBalanceForm(request.POST)
|
||||
|
||||
# Peuplement des forms
|
||||
username = request.POST.get('username')
|
||||
login_clipper = request.POST.get('login_clipper')
|
||||
|
||||
forms = get_account_create_forms(
|
||||
request, username=username, login_clipper=login_clipper)
|
||||
|
||||
account_form = forms['account_form']
|
||||
cof_form = forms['cof_form']
|
||||
user_form = forms['user_form']
|
||||
|
||||
if all((user_form.is_valid(), cof_form.is_valid(),
|
||||
trigramme_form.is_valid(), account_form.is_valid(),
|
||||
balance_form.is_valid())):
|
||||
# Checking permission
|
||||
if not request.user.has_perm('kfet.special_add_account'):
|
||||
messages.error(request, 'Permission refusée')
|
||||
else:
|
||||
data = {}
|
||||
# Fill data for Account.save()
|
||||
put_cleaned_data_in_dict(data, user_form)
|
||||
put_cleaned_data_in_dict(data, cof_form)
|
||||
|
||||
try:
|
||||
account = trigramme_form.save(data = data)
|
||||
account_form = AccountNoTriForm(request.POST, instance=account)
|
||||
account_form.save()
|
||||
balance_form = AccountBalanceForm(request.POST, instance=account)
|
||||
balance_form.save()
|
||||
amount = balance_form.cleaned_data['balance']
|
||||
checkout = Checkout.objects.get(name='Initial')
|
||||
is_cof = account.is_cof
|
||||
opegroup = OperationGroup.objects.create(
|
||||
on_acc=account,
|
||||
checkout=checkout,
|
||||
amount = amount,
|
||||
is_cof = account.is_cof)
|
||||
ope = Operation.objects.create(
|
||||
group = opegroup,
|
||||
type = Operation.INITIAL,
|
||||
amount = amount,
|
||||
is_checkout = False)
|
||||
messages.success(request, 'Compte créé : %s' % account.trigramme)
|
||||
return redirect('kfet.account.create')
|
||||
except Account.UserHasAccount as e:
|
||||
messages.error(request, \
|
||||
"Cet utilisateur a déjà un compte K-Fêt : %s" % e.trigramme)
|
||||
else:
|
||||
initial = { 'trigramme': request.GET.get('trigramme', '') }
|
||||
trigramme_form = AccountTriForm(initial = initial)
|
||||
balance_form = AccountBalanceForm(initial = {'balance': 0})
|
||||
account_form = None
|
||||
cof_form = None
|
||||
user_form = None
|
||||
|
||||
return render(request, "kfet/account_create_special.html", {
|
||||
'trigramme_form': trigramme_form,
|
||||
'account_form': account_form,
|
||||
'cof_form': cof_form,
|
||||
'user_form': user_form,
|
||||
'balance_form': balance_form,
|
||||
})
|
||||
|
||||
|
||||
# Account - Create
|
||||
|
||||
@login_required
|
||||
@teamkfet_required
|
||||
def account_create(request):
|
||||
|
|
Loading…
Reference in a new issue