Ajout scheduler pour deconnexion des membres. Ajout de reactivation des machine à la cotisation par symétrie
This commit is contained in:
parent
c6a01ef56c
commit
8e550129c1
2
Brie/brie/config/credentials.py.default
Normal file
2
Brie/brie/config/credentials.py.default
Normal file
@ -0,0 +1,2 @@
|
||||
scheduler_user="<replace_me>"
|
||||
scheduler_pass="<replace_me>"
|
@ -406,6 +406,19 @@ class CotisationAddController(AuthenticatedRestController):
|
||||
|
||||
price_to_pay = CotisationComputes.price_to_pay(year_price, month_price, already_paid, len(available_months))
|
||||
|
||||
|
||||
# réactivation des machines du membre # FIXME
|
||||
if now.month in available_months:
|
||||
dhcps = Machine.get_dhcps(self.user, member.dn)
|
||||
|
||||
machine_membre_tag = "machine_membre" # FIXME move to config
|
||||
|
||||
for dhcp_item in dhcps:
|
||||
dhcp_item.uid.replace(dhcp_item.uid.first(), machine_membre_tag)
|
||||
self.user.ldap_bind.save(dhcp_item)
|
||||
#end for
|
||||
#end if
|
||||
|
||||
user_info = self.user.attrs.cn.first()
|
||||
return Cotisation.entry_attr(time, residence, current_year, self.user.attrs.dn, user_info, price_to_pay, available_months)
|
||||
#end def
|
||||
|
@ -10,6 +10,8 @@ from brie.lib.base import BaseController
|
||||
from brie.lib.aurore_helper import *
|
||||
from brie import model
|
||||
|
||||
import brie.controllers.scheduler as scheduler
|
||||
|
||||
#from brie.controllers.secure import SecureController
|
||||
import brie.controllers.auth as auth_handler
|
||||
from brie.controllers.auth import AuthRestController
|
||||
|
74
Brie/brie/controllers/scheduler.py
Normal file
74
Brie/brie/controllers/scheduler.py
Normal file
@ -0,0 +1,74 @@
|
||||
from apscheduler.scheduler import Scheduler
|
||||
|
||||
from brie.config import ldap_config
|
||||
from brie.lib.ldap_helper import *
|
||||
from brie.lib.aurore_helper import *
|
||||
from brie.model.ldap import *
|
||||
|
||||
import datetime
|
||||
import brie.config.credentials as credentials
|
||||
|
||||
|
||||
def admin_user():
|
||||
bind = Ldap.connect(credentials.scheduler_user, credentials.scheduler_pass)
|
||||
dn = "dc=aurore,dc=u-psud,dc=fr"
|
||||
|
||||
user = User(bind,None, dn)
|
||||
result = Member.get_by_dn(user, "uid=admin,ou=Administrators,ou=TopologyManagement,o=netscaperoot")
|
||||
user.attrs = result
|
||||
|
||||
return user
|
||||
#end def
|
||||
|
||||
sched = Scheduler()
|
||||
|
||||
|
||||
def disconnect_members_from_residence(admin_user, residence_dn):
|
||||
current_year = CotisationComputes.current_year()
|
||||
now = datetime.datetime.now()
|
||||
|
||||
members = Member.get_all(admin_user, residence_dn)
|
||||
|
||||
for member in members:
|
||||
|
||||
machines_tuples = Machine.get_machine_tuples_of_member(admin_user, member.dn)
|
||||
if machines_tuples != []:
|
||||
cotisations = Cotisation.cotisations_of_member(admin_user, member.dn, current_year)
|
||||
months_list, anniversary = CotisationComputes.ldap_items_to_months_list(cotisations)
|
||||
|
||||
if not (now.month in months_list
|
||||
or ((now.month - 1) in months_list and now.day <= (anniversary + 7))):
|
||||
dhcps = Machine.get_dhcps(admin_user, member.dn)
|
||||
|
||||
machine_membre_tag = "machine_membre" # FIXME move to config
|
||||
|
||||
for dhcp_item in dhcps:
|
||||
if dhcp_item.uid.first() == machine_membre_tag:
|
||||
dhcp_item.uid.replace(machine_membre_tag, machine_membre_tag + "_disabled")
|
||||
admin_user.ldap_bind.save(dhcp_item)
|
||||
#end if
|
||||
#end for
|
||||
#end if
|
||||
|
||||
#end if
|
||||
|
||||
#end for
|
||||
|
||||
#end def
|
||||
|
||||
@sched.interval_schedule(seconds=40)
|
||||
def disconnect_members_job():
|
||||
user = admin_user()
|
||||
|
||||
residences = Residences.get_residences(user)
|
||||
|
||||
|
||||
for residence in residences:
|
||||
print "Disconnect job on : " + residence.uniqueMember.first()
|
||||
disconnect_members_from_residence(
|
||||
user, residence.uniqueMember.first())
|
||||
#end for
|
||||
|
||||
# user.ldap_bind.disconnect()
|
||||
#end def
|
||||
|
@ -1,5 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import brie.controllers.scheduler as scheduler
|
||||
|
||||
"""The application's Globals object"""
|
||||
|
||||
__all__ = ['Globals']
|
||||
@ -15,4 +17,4 @@ class Globals(object):
|
||||
|
||||
def __init__(self):
|
||||
"""Do nothing, by default."""
|
||||
pass
|
||||
scheduler.sched.start()
|
||||
|
@ -95,4 +95,30 @@ class CotisationComputes:
|
||||
return months_price
|
||||
#end def
|
||||
|
||||
@staticmethod
|
||||
def ldap_items_to_months_list(ldap_cotisations):
|
||||
result = []
|
||||
|
||||
for cotisation in ldap_cotisations:
|
||||
anniversary_data = cotisation.get("x-time").first()
|
||||
anniversary_datetime = datetime.datetime.strptime(anniversary_data,
|
||||
"%Y-%m-%d %H:%M:%S.%f")
|
||||
for month in cotisation.get("x-validMonth").all():
|
||||
result.append((anniversary_datetime, int(month)))
|
||||
#end for
|
||||
#end for
|
||||
|
||||
first_anniversary_day = 0
|
||||
# tri par ordre d'inscription et pas ordre de mois
|
||||
result = sorted(result)
|
||||
|
||||
if result != []:
|
||||
# premier anniversaire
|
||||
first_anniversary_day = result[0][0].day
|
||||
#end if
|
||||
|
||||
months_without_anniversary = [item[1] for item in result]
|
||||
|
||||
return months_without_anniversary, first_anniversary_day
|
||||
#end def
|
||||
#end class
|
||||
|
@ -5,6 +5,42 @@ import ldap
|
||||
from brie.config import ldap_config
|
||||
|
||||
|
||||
class Groups(object):
|
||||
__groups = list()
|
||||
|
||||
def __init__(self, groups):
|
||||
self.__groups = groups
|
||||
#end def
|
||||
|
||||
def __getattr__(self, name):
|
||||
return name in self.__groups
|
||||
#end def
|
||||
|
||||
def list(self):
|
||||
return list(self.__groups)
|
||||
#end def
|
||||
|
||||
#end class
|
||||
|
||||
class User(object):
|
||||
ldap_bind = None
|
||||
attrs = None
|
||||
groups = None
|
||||
residence_dn = None
|
||||
|
||||
def __init__(self, ldap_bind, attrs, residence_dn = None):
|
||||
self.ldap_bind = ldap_bind
|
||||
self.attrs = attrs
|
||||
self.residence_dn = residence_dn
|
||||
|
||||
if attrs is not None:
|
||||
groups = Groupes.get_by_user_dn(self, residence_dn, self.attrs.dn)
|
||||
|
||||
self.groups = Groups(groups)
|
||||
#end if
|
||||
#end def
|
||||
#end class
|
||||
|
||||
""" Classe de manipulation de la base ldap """
|
||||
class Ldap(object):
|
||||
__connection = None
|
||||
|
@ -205,6 +205,11 @@ class Machine(object):
|
||||
return user_session.ldap_bind.search_first(machine_dn, "(objectClass=dlzAbstractRecord)")
|
||||
#end def
|
||||
|
||||
@staticmethod
|
||||
def get_dhcps(user_session, machine_dn):
|
||||
return user_session.ldap_bind.search(machine_dn, "(objectClass=dhcpHost)")
|
||||
#end def
|
||||
|
||||
@staticmethod
|
||||
def get_dhcp_by_mac(user_session, member_dn, mac):
|
||||
return user_session.ldap_bind.search_first(member_dn, "(dhcpHWAddress=ethernet "+mac+")")
|
||||
|
Loading…
Reference in New Issue
Block a user