Skip to content
This repository has been archived by the owner on Jan 6, 2019. It is now read-only.

Permet la création d'assos #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ProfileForm(PasswordCheckMixin, forms.Form):
passwd = passwd_field(required=False)
passwd_confirm = passwd_confirm_field(required=False)

class ProfilePosixForm(ProfileForm):
shell = forms.CharField(max_length=100, label="Shell")

class RequestAccountForm(forms.ModelForm):
uid = uid_field()
Expand Down Expand Up @@ -81,3 +83,7 @@ class ProcessAccountForm(PasswordCheckMixin, forms.Form):
class ProcessPasswdForm(PasswordCheckMixin, forms.Form):
passwd = passwd_field()
passwd_confirm = passwd_confirm_field()

class NewOrgForm(forms.Form):
name = forms.CharField(max_length=100, label="Nom de l'association en minuscules")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a name, this is the ID of the new organization so it should be called "id" or something like that. Furthermore, I think the label should read "Identififant de l'organisation" with a placeholder like "org-identifier" to hint the admin at the correct format. We could even use a field validator to ensure the format of the ID is correct (only [a-z-] characters). If you need help with that, don't hesitate to ask me.

complete_name = forms.CharField(max_length=100, label="Nom complet de l'association")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

complete_name -> name

1 change: 1 addition & 0 deletions app/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
url(r'^logout/$', 'logout'),
url(r'^passwd/$', 'passwd'),
url(r'^admin/$', 'admin'),
url(r'^new_org/$', 'new_org'),
url(r'^org/(?P<uid>[A-Za-z0-9-_]+)/$', 'org'),
url(r'^org/(?P<uid>[A-Za-z0-9-_]+)/add/$', 'org_add'),
url(r'^org/(?P<uid>[A-Za-z0-9-_]+)/promote/(?P<user_uid>[a-z-.]+)/$', 'org_promote'),
Expand Down
49 changes: 44 additions & 5 deletions app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from django.utils import timezone
from django.contrib import messages

from .forms import (LoginForm, ProfileForm, RequestAccountForm, RequestPasswdForm,
ProcessAccountForm, ProcessPasswdForm)
from .forms import (LoginForm, ProfileForm, ProfilePosixForm, RequestAccountForm, RequestPasswdForm,
ProcessAccountForm, ProcessPasswdForm, NewOrgForm)
from .models import Request

from webldap import settings
Expand Down Expand Up @@ -126,20 +126,40 @@ def profile(request, l):
@connect_ldap
def profile_edit(request, l):
me = l.get_entry(request.session['ldap_binddn'])
posix = 'posixAccount' in me.objectClass
if request.method != 'POST':
f = ProfileForm(label_suffix='', initial={'email': one(me.mail),
if not posix:
f = ProfileForm(label_suffix='', initial={'email': one(me.mail),
'name': me.displayName,
'nick': one(me.cn)})
else:
f = ProfilePosixForm(label_suffix='', initial={'email': one(me.mail),
'name': me.displayName,
'nick': one(me.cn),
'shell': me.loginShell})
ctx = {'form': f, 'name': me.displayName, 'nick': one(me.cn), 'email': one(me.mail)}

return form(ctx, 'main/edit.html', request)

f = ProfileForm(request.POST)
if not posix:
f = ProfileForm(request.POST)
else:
f = ProfilePosixForm(request.POST)
ctx = {'form': f, 'name': me.displayName, 'nick': one(me.cn), 'email': one(me.mail)}

if not f.is_valid():
return form(ctx, 'main/edit.html', request)

if posix:
shell = f.cleaned_data['shell']
if shell != me.loginShell:
try:
me.loginShell = shell
me.save()
except ldapom.error.LDAPError:
messages.error(request, 'Shell invalide ?')
return form(ctx, 'main/edit.html', request)

name = f.cleaned_data['name']
nick = f.cleaned_data['nick']

Expand Down Expand Up @@ -183,6 +203,26 @@ def profile_edit(request, l):

return form(ctx, 'main/edit.html', request)

@connect_ldap
def new_org(request, l):
if not request.session['is_admin']:
messages.error(request, 'Vous n\'êtes pas admin')
return HttpResponseRedirect('/org/{}'.format(uid))

if request.method == 'POST':
req = NewOrgForm(request.POST)
if req.is_valid():
me = l.get_entry(request.session['ldap_binddn'])
new_org = l.get_entry('o={},ou=associations,{}'.format(req.cleaned_data['name'], settings.LDAP_BASE))
new_org.objectClass='groupOfUniqueNames'
new_org.cn = req.cleaned_data['complete_name']
new_org.uniqueMember.add(me.dn)
new_org.save()
messages.success(request, 'Association créée')
return HttpResponseRedirect('/')

f = NewOrgForm()
return form({'form': f}, 'main/new_org.html', request)

@connect_ldap
def org(request, l, uid):
Expand Down Expand Up @@ -250,7 +290,6 @@ def org_relegate(request, l, uid, user_uid):
messages.success(request, '{} n\'est plus gérant'.format(user.displayName))
return HttpResponseRedirect('/org/{}'.format(uid))


@connect_ldap
def org_add(request, l, uid):
org = l.get_entry('o={},ou=associations,{}'.format(uid, settings.LDAP_BASE))
Expand Down
1 change: 1 addition & 0 deletions app/templates/main/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<h1>Administration</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<h2>Associations</h2>
<a href="/new_org">Ajouter une association</a>
<ul>
{% for org in orgs %}
<li>
Expand Down
18 changes: 18 additions & 0 deletions app/templates/main/new_org.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "main/base.html" %}
{% block title %}Ajouter une organisation{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should read "Ajouter une association" (same below in the <h1> title)

{% block content %}
<h1>Ajouter une organisation</h1>
{% if error_msg %}<p><strong>{{ error_msg }}</strong></p>{% endif %}
<form action="." method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
<tr>
<td></td>
<td>
<input type="submit" value="Créer" />
</td>
</tr>
</table>
</form>
{% endblock %}