added change room + change room owner features

This commit is contained in:
Romain Beuque 2013-03-31 03:25:04 +02:00
parent 41d85ca856
commit 7842e4506a
6 changed files with 203 additions and 21 deletions

View File

@ -31,12 +31,16 @@ class EditController(AuthenticatedBaseController):
""" Controller fils wifi pour gérer le wifi """
wifi = None
""" Controller fils room pour gérer les chambres """
room = None
""" Controller fils de gestion des machines """
machine = None
def __init__(self, new_show):
self.show = new_show
self.wifi = WifiRestController(new_show)
self.machine = MachineController()
self.room = RoomController(new_show)
""" Affiche les détails éditables du membre et de la chambre """
@ -264,3 +268,132 @@ class WifiRestController(AuthenticatedRestController):
#end def
#end class
""" Controller de gestion des rooms """
class RoomController(AuthenticatedBaseController):
require_group = groups_enum.admin
""" Controller fils d'ajout de machine """
move = None
show = None
change_member = None
def __init__(self, show):
self.move = RoomMoveController()
self.show = show
self.change_member = RoomChangeMemberController()
""" Affiche les détails éditables de la chambre """
@expose("brie.templates.edit.room")
def index(self, residence, room_id):
residence_dn = Residences.get_dn_by_name(self.user, residence)
room = Room.get_by_uid(self.user, residence_dn, room_id)
if room is None:
raise Exception("no room")
member = None
if room.has("x-memberIn"):
member = Member.get_by_dn(self.user, room.get("x-memberIn").first())
members = Member.get_all(self.user, residence_dn)
return {
"residence" : residence,
"user" : self.user,
"room_ldap" : room,
"member_ldap" : member,
"members" : members
}
#se Exception("tait toi")
#end def
#end class
""" Controller REST de gestion des ajouts de machines. """
class RoomMoveController(AuthenticatedRestController):
require_group = groups_enum.admin
""" Gestion des requêtes post sur ce controller """
@expose()
def post(self, residence, member_uid, room_uid):
residence_dn = Residences.get_dn_by_name(self.user, residence)
# Récupération du membre et de la machine
# Note : on cherche la machine seulement sur le membre (member.dn)
member = Member.get_by_uid(self.user, residence_dn, member_uid)
room = Room.get_by_uid(self.user, residence_dn, room_uid)
# Si la machine existe effectivement, on la supprime
if room is not None:
if room.get("x-memberIn") is not None:
raise Exception("chambre de destination non vide")
#TODO passer sur une page d'erreur au lieu d'une exception
else:
old_room = Room.get_by_member_dn(self.user, residence_dn, member.dn)
memberIn_attribute = Room.memberIn_attr(str(member.dn))
self.user.ldap_bind.delete_attr(old_room.dn, memberIn_attribute)
self.user.ldap_bind.add_attr(room.dn, memberIn_attribute)
#end if
else:
old_room = Room.get_by_member_dn(self.user, residence_dn, member.dn)
memberIn_attribute = Room.memberIn_attr(str(member.dn))
self.user.ldap_bind.delete_attr(old_room.dn, memberIn_attribute)
#end if
#self.user.ldap_bind.delete_entry_subtree(machine.dn)
#taken_attribute = IpReservation.taken_attr(ip.get("x-taken").first())
#self.user.ldap_bind.delete_attr(ip.dn, taken_attribute)
#end if
# On redirige sur la page d'édition du membre
redirect("/edit/member/" + residence + "/" + member_uid)
#end def
#end def
""" Controller REST de gestion des ajouts de machines. """
class RoomChangeMemberController(AuthenticatedRestController):
require_group = groups_enum.admin
""" Gestion des requêtes post sur ce controller """
@expose()
def post(self, residence, member_uid, room_uid):
residence_dn = Residences.get_dn_by_name(self.user, residence)
# Récupération du membre et de la machine
# Note : on cherche la machine seulement sur le membre (member.dn)
member = Member.get_by_uid(self.user, residence_dn, member_uid)
room = Room.get_by_uid(self.user, residence_dn, room_uid)
if member is None and member_uid != "":
raise Exception("member not found")
#end if
if member is not None:
old_room_member = Room.get_by_member_dn(self.user, residence_dn, member.dn)
# Si la machine existe effectivement, on la supprime
if old_room_member is not None:
raise Exception("le nouveau membre possèdait déjà une chambre. conflit")
#end if
#end if
if room is None:
raise Exception("room inconnue")
if room.get("x-memberIn") is not None:
memberIn_attribute = Room.memberIn_attr(str(room.get("x-memberIn").first()))
self.user.ldap_bind.delete_attr(room.dn, memberIn_attribute)
#end if
if member is not None:
memberIn_attribute = Room.memberIn_attr(str(member.dn))
self.user.ldap_bind.add_attr(room.dn, memberIn_attribute)
#end if
# On redirige sur la page d'édition du membre
redirect("/edit/room/index/" + residence + "/" + room_uid)
#end def
#end def

View File

@ -40,6 +40,14 @@ class Member(object):
class Room(object):
@staticmethod
def memberIn_attr(member_dn):
return {
"x-memberIn" : str(member_dn)
}
#end def
@staticmethod
def get_by_name(user_session, residence_dn, name):
return user_session.ldap_bind.search_first(ldap_config.room_base_dn + residence_dn, "(&(objectClass=pacaterieRoom)(cn=" + name + "))")

View File

@ -15,7 +15,7 @@
</div>
<div py:choose="room_ldap">
<span class="section_name" py:when="None">Pas de chambre associée</span>
<div py:otherwise="">${room_view(room_ldap,rooms)}</div>
<div py:otherwise="">${room_view(room_ldap, rooms, member_ldap)}</div>
</div>
</body>
</html>

View File

@ -84,19 +84,20 @@
</div>
</py:def>
<py:def function="room_view(room, rooms, member_in = None)">
<py:def function="room_view(room, rooms, member_in)">
<div class="section">
<span class="section_name show_section_name">CHAMBRE ${room.cn.first()}</span>
<div>
<div py:if="member_in is not None">
<!-- <div py:if="member_in is not None">
<span class="item_name">Membre</span>
<span>
<a href="/show/member/${residence}/${member_in.uid.first()}">${member_in.cn.first()}</a>
</span>
</div>
<div>
</div>-->
<div py:if="rooms is not None">
<form method="post" action="/edit/room/move">
<span class="item_name">Changer de chambre</span>
<select name="rooms">
<select name="room_uid">
<option value="">&lt;vide&gt;</option>
<py:for each="target_room in rooms">
<py:choose test="target_room.cn.first() == room.cn.first()">
@ -105,9 +106,12 @@
</py:choose>
</py:for>
</select>
<input type="hidden" name="residence" value="${residence}"/>
<input type="hidden" name="member_uid" value="${member_in.uid.first()}"/>
<span>
<a href="#" class="button">Sauvegarder le changement de chambre</a>
<input type="submit" class="button" value="Envoyer!" />
</span>
</form>
</div>
<div>
<span class="item_name"></span>
@ -118,5 +122,42 @@
</div>
</div>
</py:def>
<py:def function="room_edit_member(room, members, member_in)">
<div class="section">
<span class="section_name show_section_name">CHAMBRE ${room.cn.first()}</span>
<div>
<div py:if="member_in is not None">
<span class="item_name">Membre</span>
<span>
<a href="/show/member/${residence}/${member_in.uid.first()}">${member_in.cn.first()}</a>
</span>
</div>
<div>
<form method="post" action="/edit/room/change_member">
<span class="item_name">Changer de propriétaire</span>
<select name="member_uid">
<option value="">&lt;vide&gt;</option>
<py:for each="target_member in members">
<py:if test="member_in is None">
<option value="${target_member.uid.first()}">${target_member.cn.first()}</option>
</py:if>
<py:if test="member_in is not None">
<py:choose test="target_member.cn.first() == member_in.cn.first()">
<option py:when="True" selected="selected" value="${target_member.uid.first()}">${target_member.cn.first()}</option>
<option py:when="False" value="${target_member.uid.first()}">${target_member.cn.first()}</option>
</py:choose>
</py:if>
</py:for>
</select>
<input type="hidden" name="residence" value="${residence}"/>
<input type="hidden" name="room_uid" value="${room.uid.first()}"/>
<span>
<input type="submit" class="button" value="Envoyer!" />
</span>
</form>
</div>
</div>
</div>
</py:def>
</html>

View File

@ -9,10 +9,10 @@
</head>
<body>
${edit_banner()}
${back_show("room", None, room_ldap.uid.first())}
${back_show("room", room_ldap.uid.first())}
<div py:choose="room_ldap">
<span class="section_name" py:when="None">Pas de chambre associée</span>
<div py:otherwise="">${room_view(room_ldap, member_ldap)}</div>
<div py:otherwise="">${room_edit_member(room_ldap, members, member_ldap)}</div>
</div>
</body>
</html>

View File

@ -90,7 +90,7 @@
<py:def function="room_view(room, member_in = None)">
<div class="section">
<span class="section_name show_section_name">CHAMBRE ${room.cn.first()}</span>
<a class="link_button some_margin" href="/edit/room/${residence}/${room.uid.first()}/">éditer</a>
<a class="link_button some_margin" href="/edit/room/index/${residence}/${room.uid.first()}/">éditer</a>
<div>
<div py:if="member_in is not None">
<span class="item_name">Membre</span>