From 3eb2ac8583e11a1ad2e7099d097fcaaccfae6d61 Mon Sep 17 00:00:00 2001 From: Romain Beuque Date: Thu, 13 Mar 2014 02:10:50 +0100 Subject: [PATCH] performance sur rooms et members + ajout de get_childs --- Brie/brie/controllers/members.py | 23 ++++++-- Brie/brie/controllers/rooms.py | 30 ++++++---- Brie/brie/controllers/show.py | 2 + Brie/brie/lib/aurore_helper.py | 99 +++++++++++++++++++++++++------- Brie/brie/lib/ldap_helper.py | 53 ++++++++++++++++- Brie/brie/model/ldap.py | 12 ++++ 6 files changed, 180 insertions(+), 39 deletions(-) diff --git a/Brie/brie/controllers/members.py b/Brie/brie/controllers/members.py index d9196ba..a5199b5 100644 --- a/Brie/brie/controllers/members.py +++ b/Brie/brie/controllers/members.py @@ -36,11 +36,24 @@ class MembersController(AuthenticatedBaseController): members = Member.get_all(self.user, residence_dn) members = MembersController.sort_name(members) + + rooms = Room.get_rooms(self.user, residence_dn) + rooms_dict = dict() + for room in rooms: + if room.get("x-memberIn").first is not None: + rooms_dict[room.get("x-memberIn").first()] = room + #end if + #end for - members_rooms = [ - (member, Room.get_by_member_dn(self.user, residence_dn, member.dn)) - for member in members - ] + #raise Exception("ee") + members_rooms = [] + for member in members: + if member.dn in rooms_dict: + members_rooms.append((member, rooms_dict[member.dn])) + else: + members_rooms.append((member, None)) + #end if + #end for # machines = Machine.get_machine_tuples_of_member(self.user, member.dn) @@ -133,4 +146,4 @@ class MembersController(AuthenticatedBaseController): redirect("/show/member/" + residence_name + "/" + member.uid.first()) #end def -#end class \ No newline at end of file +#end class diff --git a/Brie/brie/controllers/rooms.py b/Brie/brie/controllers/rooms.py index 8e7d386..2907aa3 100644 --- a/Brie/brie/controllers/rooms.py +++ b/Brie/brie/controllers/rooms.py @@ -45,26 +45,34 @@ class RoomsController(AuthenticatedBaseController): status = dict() areas = dict() - print "here 1" - residence_dn = Residences.get_dn_by_name(self.user, residence_name) if residence_dn is None: raise Exception("unknown residence") #end if - print "here 1 bis" - - stats = CotisationComputes.members_status_from_residence(self.user, residence_dn) - print "here 2" - + current_year = CotisationComputes.current_year() + cotisations_year = Cotisation.get_all_cotisation_by_year(self.user, residence_dn, current_year) + + stats = CotisationComputes.members_status_from_list_cotisations(self.user, residence_dn, cotisations_year) + members = dict() + members_entries = Member.get_all(self.user, residence_dn) + members_entries_dict = dict() + for member in members_entries: + members_entries_dict[member.dn] = member + #end for for label in stats: - for member in stats[label]: - members[member.dn] = label + liste = [] + for member_dn in stats[label]: + members[member_dn] = label + liste.append(member_dn) #end for member + for item in liste: + stats[label].remove(item) + stats[label].append(members_entries_dict[item]) + #end for item liste #end for stats - - print "here 3" + stats['number_of_rooms'] = Room.get_number_of_rooms(self.user, residence_dn) stats['empty_rooms'] = [] diff --git a/Brie/brie/controllers/show.py b/Brie/brie/controllers/show.py index 4edb7d9..f19c5c9 100644 --- a/Brie/brie/controllers/show.py +++ b/Brie/brie/controllers/show.py @@ -29,6 +29,8 @@ class ShowController(AuthenticatedBaseController): residence_dn = Residences.get_dn_by_name(self.user, residence) member = Member.get_by_uid(self.user, residence_dn, uid) +# truc = self.user.ldap_bind.get_childs("uid=romain.beuque,ou=2012,ou=membres,dc=emilie,dc=u-psud,dc=fr") +# raise Exception("") if member is None: return self.error_no_entry() diff --git a/Brie/brie/lib/aurore_helper.py b/Brie/brie/lib/aurore_helper.py index 6fadf7d..2947780 100644 --- a/Brie/brie/lib/aurore_helper.py +++ b/Brie/brie/lib/aurore_helper.py @@ -157,10 +157,12 @@ class CotisationComputes: @staticmethod # old = SDF or no cotisation this year - def is_old_member(member, user_session, residence_dn): - current_year = CotisationComputes.current_year() - cotisations = Cotisation.cotisations_of_member(user_session, member.dn, current_year) - return Room.get_by_member_dn(user_session, residence_dn, member.dn) == None or cotisations == [] + def is_old_member(member_dn, user_session, residence_dn, cotisations = None): + if cotisations is None: + current_year = CotisationComputes.current_year() + cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year) + #end if + return Room.get_by_member_dn(user_session, residence_dn, member_dn) == None or cotisations == [] #end def @staticmethod @@ -174,29 +176,35 @@ class CotisationComputes: @staticmethod # 7 days grace period - def is_cotisation_paid(member, user_session, residence_dn): - if CotisationComputes.is_old_member(member, user_session, residence_dn): + def is_cotisation_paid(member_dn, user_session, residence_dn, cotisations = None): + if CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations): return False - current_year = CotisationComputes.current_year() + if cotisations is None: + current_year = CotisationComputes.current_year() + #end if now = datetime.datetime.now() - cotisations = Cotisation.cotisations_of_member(user_session, member.dn, current_year) + if cotisations is None: + cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year) + #end if anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations) delta = (now - anniversary) - if member.dn == "uid=emir.kort,ou=2013,ou=membres,dc=ile,dc=u-psud,dc=fr": - print "member :" + member.dn + "delta :" + str(delta) + "now :" + str(now) + "anniversary :" + str(anniversary) return delta.days <= 7 #end def @staticmethod # less than a month late but more than a week - def is_cotisation_late(member, user_session, residence_dn): - if CotisationComputes.is_old_member(member, user_session, residence_dn): + def is_cotisation_late(member_dn, user_session, residence_dn, cotisations = None): + if CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations): return False - current_year = CotisationComputes.current_year() + if cotisations is None: + current_year = CotisationComputes.current_year() + #end if now = datetime.datetime.now() - cotisations = Cotisation.cotisations_of_member(user_session, member.dn, current_year) + if cotisations is None: + cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year) + #end if anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations) delta = (now - anniversary) #print("[DEBUG] cotisation en retard pour l'utilisateur "+ member.dn +" now="+ str(now) +" anniversary="+ str(anniversary) +" delta="+ str(delta)) @@ -205,13 +213,17 @@ class CotisationComputes: @staticmethod # more than a month late - def is_no_cotisation(member, user_session, residence_dn): - if CotisationComputes.is_old_member(member, user_session, residence_dn): + def is_no_cotisation(member_dn, user_session, residence_dn, cotisations = None): + if CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations): return False - current_year = CotisationComputes.current_year() + if cotisations is None: + current_year = CotisationComputes.current_year() + #end if now = datetime.datetime.now() - cotisations = Cotisation.cotisations_of_member(user_session, member.dn, current_year) + if cotisations is None: + cotisations = Cotisation.cotisations_of_member(user_session, member_dn, current_year) + #end if anniversary = CotisationComputes.anniversary_from_ldap_items(cotisations) delta = (now - anniversary) return delta.days > 30 @@ -226,14 +238,16 @@ class CotisationComputes: cotisation_late_members = [] no_cotisation_members = [] for member in members: - if CotisationComputes.is_old_member(member, user_session, residence_dn): + current_year = CotisationComputes.current_year() + cotisations = Cotisation.cotisations_of_member(user_session, member.dn, current_year) + if CotisationComputes.is_old_member(member.dn, user_session, residence_dn, cotisations): old_members.append(member) - elif CotisationComputes.is_cotisation_paid(member, user_session, residence_dn): + elif CotisationComputes.is_cotisation_paid(member.dn, user_session, residence_dn, cotisations): cotisation_paid_members.append(member) - elif CotisationComputes.is_cotisation_late(member, user_session, residence_dn): + elif CotisationComputes.is_cotisation_late(member.dn, user_session, residence_dn, cotisations): cotisation_late_members.append(member) #print("[DEBUG] cotisation en retard pour l'utilisateur "+ member.dn) - elif CotisationComputes.is_no_cotisation(member, user_session, residence_dn): + elif CotisationComputes.is_no_cotisation(member.dn, user_session, residence_dn, cotisations): no_cotisation_members.append(member) else: print "DEBUG : member with weird status !" @@ -243,4 +257,45 @@ class CotisationComputes: return dict(old_members=old_members, cotisation_paid_members=cotisation_paid_members, cotisation_late_members=cotisation_late_members, no_cotisation_members=no_cotisation_members) #end def + @staticmethod + def members_status_from_list_cotisations(user_session, residence_dn, cotisations): + members_dict = dict() + for cotisation in cotisations: + cotisation_dn = cotisation.dn.split(",") + member_dn = "" + for i in range(3, len(cotisation_dn)): + if i != 3: + member_dn += "," + #end if + member_dn += cotisation_dn[i] + #end for + if not member_dn in members_dict: + members_dict[member_dn] = [cotisation] + else: + members_dict[member_dn].append(cotisation) + #end if + #end for + + old_members = [] + cotisation_paid_members = [] + cotisation_late_members = [] + no_cotisation_members = [] + for member_dn, cotisations in members_dict.iteritems(): + if CotisationComputes.is_old_member(member_dn, user_session, residence_dn, cotisations): + old_members.append(member_dn) + elif CotisationComputes.is_cotisation_paid(member_dn, user_session, residence_dn, cotisations): + cotisation_paid_members.append(member_dn) + elif CotisationComputes.is_cotisation_late(member_dn, user_session, residence_dn, cotisations): + cotisation_late_members.append(member_dn) + #print("[DEBUG] cotisation en retard pour l'utilisateur "+ member.dn) + elif CotisationComputes.is_no_cotisation(member_dn, user_session, residence_dn, cotisations): + no_cotisation_members.append(member_dn) + else: + print "DEBUG : member with weird status !" + #end if + + #end for + return dict(old_members=old_members, cotisation_paid_members=cotisation_paid_members, cotisation_late_members=cotisation_late_members, no_cotisation_members=no_cotisation_members) + #end def + #end class diff --git a/Brie/brie/lib/ldap_helper.py b/Brie/brie/lib/ldap_helper.py index c8b590e..f0dac9f 100644 --- a/Brie/brie/lib/ldap_helper.py +++ b/Brie/brie/lib/ldap_helper.py @@ -108,6 +108,33 @@ class Ldap(object): return ldap_results #end def + def get_childs(self, dn, filter = "(objectClass=*)"): + results = self.search(dn, filter) + tree = [None, dict()] + + for result in results: + if result.dn == dn: + tree[0] = result + else: + result_dn = result.dn.replace(dn, "").split(",") + tree_c = tree + result_dn.reverse() + for dn_split in result_dn: + if dn_split != "": + if not dn_split in tree_c[1]: + tree_c[1][dn_split] = [None, dict()] + tree_c = tree_c[1][dn_split] + else: + tree_c = tree_c[1][dn_split] + #end if + #end if + #end for + tree_c[0] = result + #end if + #end for + return LdapEntryTree(tree[0], tree[1]) + #end def + """ Recherche le premier resultat sur la base appel la methode "search" en interne """ @@ -499,4 +526,28 @@ class LdapAttribute(object): #end def #end class - + +class LdapEntryTree(LdapEntry): + childs = None + val = None + + def __init__(self, val, childs): + self.__dict__ = val.__dict__ + if len(childs) > 0: + self.childs = dict() + for key,child in childs.iteritems(): + key = key.split("=")[1] + self.childs[key] = LdapEntryTree(child[0], child[1]) + self.__dict__[key] = self.childs[key] + #end for + #end if + #end def + + def __getattr__(self, name): + attr = LdapAttribute(name, []) + self.__dict__[name] = attr + + return attr + #end def + +#end class diff --git a/Brie/brie/model/ldap.py b/Brie/brie/model/ldap.py index 16d7fc9..dbf1335 100644 --- a/Brie/brie/model/ldap.py +++ b/Brie/brie/model/ldap.py @@ -419,6 +419,18 @@ class Cotisation: return user_session.ldap_bind.search_first(dn, "(uid=" + name + ")") #end def + @staticmethod + def get_all_cotisation_by_year(user_session, residence_dn, year): + dn = ldap_config.username_base_dn + residence_dn + return user_session.ldap_bind.search(dn, "(&(objectClass=auroreCotisation)(x-year=" + str(year) + "))") + #end def + + @staticmethod + def get_all_cotisation(user_session, residence_dn): + dn = ldap_config.username_base_dn + residence_dn + return user_session.ldap_bind.search(dn, "(objectClass=auroreCotisation)") + #end def + @staticmethod def get_all_payment_by_year(user_session, residence_dn, year): dn = ldap_config.username_base_dn + residence_dn