Commit 47d81479 by Yvon Kerdoncuff

code cleaning : get rid of quick-and-dirty circular dependency fix by moving…

code cleaning : get rid of quick-and-dirty circular dependency fix by moving some code in separated file
parent 42f1607b
...@@ -8,6 +8,7 @@ from outils.common import CouchDB ...@@ -8,6 +8,7 @@ from outils.common import CouchDB
from outils.common import Verification from outils.common import Verification
from products.models import OFF from products.models import OFF
from envelops.models import CagetteEnvelops from envelops.models import CagetteEnvelops
import shifts.fonctions
import sys import sys
import pytz import pytz
...@@ -952,7 +953,7 @@ class CagetteMember(models.Model): ...@@ -952,7 +953,7 @@ class CagetteMember(models.Model):
fields = fields + ['shift_type', 'makeups_to_do', 'display_ftop_points', 'display_std_points', 'shift_type'] fields = fields + ['shift_type', 'makeups_to_do', 'display_ftop_points', 'display_std_points', 'shift_type']
cond.append(['shift_type', '=', 'standard']) cond.append(['shift_type', '=', 'standard'])
res = api.search_read('res.partner', cond, fields) res = api.search_read('res.partner', cond, fields)
CagetteMembers.add_makeups_to_come_to_member_data(res) CagetteMembers.add_makeups_to_come_to_member_data(api, res)
return res return res
elif search_type == "shift_template_data": elif search_type == "shift_template_data":
fields = CagetteMember.m_short_default_fields fields = CagetteMember.m_short_default_fields
...@@ -1393,8 +1394,7 @@ class CagetteMembers(models.Model): ...@@ -1393,8 +1394,7 @@ class CagetteMembers(models.Model):
# 1 : fetching members with no makeups to do but with some makeups to come # 1 : fetching members with no makeups to do but with some makeups to come
# 2 : providing makeups to come to all members # 2 : providing makeups to come to all members
cs = CagetteShift() makeups_to_come_per_partner = shifts.fonctions.get_partners_with_makeups_to_come(api)
makeups_to_come_per_partner = cs.get_partners_with_makeups_to_come()
# 1 : fetching members with no makeups to do but with some makeups to come # 1 : fetching members with no makeups to do but with some makeups to come
cond = [['makeups_to_do', '=', 0], ['id', 'in', list(makeups_to_come_per_partner.keys())]] cond = [['makeups_to_do', '=', 0], ['id', 'in', list(makeups_to_come_per_partner.keys())]]
...@@ -1414,11 +1414,10 @@ class CagetteMembers(models.Model): ...@@ -1414,11 +1414,10 @@ class CagetteMembers(models.Model):
return res return res
@staticmethod @staticmethod
def add_makeups_to_come_to_member_data(res): def add_makeups_to_come_to_member_data(api, res):
if res: if res:
cs = CagetteShift()
for idx, partner in enumerate(res): for idx, partner in enumerate(res):
[shift_data, is_ftop] = cs.get_shift_partner(int(partner['id'])) [shift_data, is_ftop] = shifts.fonctions.get_shift_partner(api, int(partner['id']))
res[idx]['makeups_to_come'] = sum(1 for value in shift_data if value['is_makeup']) res[idx]['makeups_to_come'] = sum(1 for value in shift_data if value['is_makeup'])
@staticmethod @staticmethod
...@@ -1518,6 +1517,4 @@ class CagetteUser(models.Model): ...@@ -1518,6 +1517,4 @@ class CagetteUser(models.Model):
except Exception as e: except Exception as e:
res['error'] = str(e) res['error'] = str(e)
return res return res
\ No newline at end of file
from shifts.models import CagetteShift
import datetime, pytz import datetime, pytz
tz = pytz.timezone("Europe/Paris")
def dateIsoUTC(myDate): def dateIsoUTC(myDate):
tz = pytz.timezone("Europe/Paris")
tDate = tz.localize(datetime.datetime.strptime(myDate, '%Y-%m-%d %H:%M:%S')) tDate = tz.localize(datetime.datetime.strptime(myDate, '%Y-%m-%d %H:%M:%S'))
return dDate.isoformat() return tDate.isoformat()
def get_partners_with_makeups_to_come(api):
"""Returns a dictionary with : keys = the partners ids having at least one makeup to come ; values = #makeups_to_come"""
fields = ['partner_id']
cond = [['state', '=', 'open'], ['date_begin', '>', datetime.datetime.now().isoformat()], ['is_makeup', '=', True]]
shift_data = api.search_read('shift.registration', cond, fields)
count_dic = {}
for value in shift_data:
if value['partner_id'][0] in count_dic:
count_dic[value['partner_id'][0]] = count_dic[value['partner_id'][0]] + 1
else:
count_dic[value['partner_id'][0]] = 1
return count_dic
def get_shift_partner(api, id, start_date=None, end_date=None):
"""Récupère les shift du membre"""
shifts = []
is_ftop = False
not_before = datetime.datetime.now().isoformat()
if start_date:
not_before = start_date.isoformat()
fields = ['date_begin', 'date_end',
'shift_id', 'shift_type', 'partner_id', "id", "associate_registered", "is_makeup"] # res.partner
cond = [['partner_id.id', '=', id], ['state', '=', 'open'],
['date_begin', '>', not_before]]
if end_date:
cond.append(['date_begin', '<', end_date.isoformat()])
shiftData = api.search_read('shift.registration', cond, fields, order="date_begin ASC")
for s in shiftData:
if not ('Equipe volante' in s['shift_id'][1]):
shifts.append(s)
else:
is_ftop = True
return [shifts, is_ftop]
...@@ -5,6 +5,7 @@ from outils.common import OdooAPI ...@@ -5,6 +5,7 @@ from outils.common import OdooAPI
from outils.common import Verification from outils.common import Verification
from members.models import CagetteMember from members.models import CagetteMember
from members.models import CagetteUser from members.models import CagetteUser
import shifts.fonctions
from pytz import timezone from pytz import timezone
...@@ -58,7 +59,7 @@ class CagetteShift(models.Model): ...@@ -58,7 +59,7 @@ class CagetteShift(models.Model):
try: try:
now = datetime.datetime.now(tz) now = datetime.datetime.now(tz)
# Have to retrive shifts (from now to a cycle period forward to check rules respect) # Have to retrive shifts (from now to a cycle period forward to check rules respect)
[shift_registrations, is_ftop] = self.get_shift_partner(partner_id, now + datetime.timedelta(weeks=4)) [shift_registrations, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, now + datetime.timedelta(weeks=4))
new_shift = self.get_shift(idNewShift) # WARNING : use date_begin_tz while shift_registrations use date_begin (UTC) new_shift = self.get_shift(idNewShift) # WARNING : use date_begin_tz while shift_registrations use date_begin (UTC)
if "successive_shifts_allowed" in rules: if "successive_shifts_allowed" in rules:
min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2) min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2)
...@@ -76,7 +77,7 @@ class CagetteShift(models.Model): ...@@ -76,7 +77,7 @@ class CagetteShift(models.Model):
if 'start_date' in cw: if 'start_date' in cw:
sd = cw['start_date'] sd = cw['start_date']
ed = cw['start_date'] + datetime.timedelta(weeks=4) ed = cw['start_date'] + datetime.timedelta(weeks=4)
[cycle_shift_regs, is_ftop] = self.get_shift_partner(partner_id, start_date=sd, end_date=ed) [cycle_shift_regs, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, start_date=sd, end_date=ed)
if len(cycle_shift_regs) >= int(rules['max_shifts_per_cycle']): if len(cycle_shift_regs) >= int(rules['max_shifts_per_cycle']):
answer = False answer = False
coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id)) coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id))
...@@ -127,7 +128,7 @@ class CagetteShift(models.Model): ...@@ -127,7 +128,7 @@ class CagetteShift(models.Model):
try: try:
now = datetime.datetime.now(tz) now = datetime.datetime.now(tz)
# Have to retrive shifts (from now to a cycle period forward to check rules respect) # Have to retrive shifts (from now to a cycle period forward to check rules respect)
[shift_registrations, is_ftop] = self.get_shift_partner(partner_id, now + datetime.timedelta(weeks=4)) [shift_registrations, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, now + datetime.timedelta(weeks=4))
new_shift = self.get_shift(idNewShift) # WARNING : use date_begin_tz while shift_registrations use date_begin (UTC) new_shift = self.get_shift(idNewShift) # WARNING : use date_begin_tz while shift_registrations use date_begin (UTC)
if "successive_shifts_allowed" in rules: if "successive_shifts_allowed" in rules:
min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2) min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2)
...@@ -145,7 +146,7 @@ class CagetteShift(models.Model): ...@@ -145,7 +146,7 @@ class CagetteShift(models.Model):
if 'start_date' in cw: if 'start_date' in cw:
sd = cw['start_date'] sd = cw['start_date']
ed = cw['start_date'] + datetime.timedelta(weeks=4) ed = cw['start_date'] + datetime.timedelta(weeks=4)
[cycle_shift_regs, is_ftop] = self.get_shift_partner(partner_id, start_date=sd, end_date=ed) [cycle_shift_regs, is_ftop] = shifts.fonctions.get_shift_partner(self.o_api, partner_id, start_date=sd, end_date=ed)
if len(cycle_shift_regs) >= int(rules['max_shifts_per_cycle']): if len(cycle_shift_regs) >= int(rules['max_shifts_per_cycle']):
answer = False answer = False
coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id)) coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id))
...@@ -236,39 +237,6 @@ class CagetteShift(models.Model): ...@@ -236,39 +237,6 @@ class CagetteShift(models.Model):
return partnerData return partnerData
def get_shift_partner(self, id, start_date=None, end_date=None):
"""Récupère les shift du membre"""
shifts = []
is_ftop = False
not_before = datetime.datetime.now().isoformat()
if start_date:
not_before = start_date.isoformat()
fields = ['date_begin', 'date_end',
'shift_id', 'shift_type','partner_id', "id", "associate_registered", "is_makeup"] # res.partner
cond = [['partner_id.id', '=', id],['state', '=', 'open'],
['date_begin', '>', not_before]]
if end_date:
cond.append(['date_begin', '<', end_date.isoformat()])
shiftData = self.o_api.search_read('shift.registration', cond, fields, order ="date_begin ASC")
for s in shiftData:
if not ('Equipe volante' in s['shift_id'][1]):
shifts.append(s)
else:
is_ftop = True
return [shifts, is_ftop]
def get_partners_with_makeups_to_come(self):
"""Returns a dictionary with : keys = the partners ids having at least one makeup to come ; values = #makeups_to_come"""
fields = ['partner_id']
cond = [['state', '=', 'open'], ['date_begin', '>', datetime.datetime.now().isoformat()], ['is_makeup', '=', True]]
shift_data = self.o_api.search_read('shift.registration', cond, fields)
count_dic = {}
for value in shift_data:
if value['partner_id'][0] in count_dic:
count_dic[value['partner_id'][0]] = count_dic[value['partner_id'][0]] + 1
else:
count_dic[value['partner_id'][0]] = 1
return count_dic
def shift_is_makeup(self, id): def shift_is_makeup(self, id):
"""vérifie si une shift est un rattrapage""" """vérifie si une shift est un rattrapage"""
...@@ -277,7 +245,6 @@ class CagetteShift(models.Model): ...@@ -277,7 +245,6 @@ class CagetteShift(models.Model):
shiftData = self.o_api.search_read('shift.registration', cond, fields) shiftData = self.o_api.search_read('shift.registration', cond, fields)
return shiftData[0]["is_makeup"] return shiftData[0]["is_makeup"]
def get_shift_calendar(self, is_ftop, start, end): def get_shift_calendar(self, is_ftop, start, end):
"""Récupère les shifts à partir de maintenant pour le calendier""" """Récupère les shifts à partir de maintenant pour le calendier"""
......
...@@ -4,16 +4,13 @@ from outils.common import Verification ...@@ -4,16 +4,13 @@ from outils.common import Verification
from shifts.models import CagetteShift from shifts.models import CagetteShift
from members.models import CagetteMember from members.models import CagetteMember
import shifts.fonctions
# working_state = ['up_to_date', 'alert', 'exempted', 'delay', 'suspended'] # working_state = ['up_to_date', 'alert', 'exempted', 'delay', 'suspended']
state_shift_allowed = ["up_to_date", "alert", "delay"] state_shift_allowed = ["up_to_date", "alert", "delay"]
tz = pytz.timezone("Europe/Paris") tz = pytz.timezone("Europe/Paris")
def dateIsoUTC(myDate):
tDate = tz.localize(datetime.datetime.strptime(myDate, '%Y-%m-%d %H:%M:%S'))
return tDate.isoformat()
def home(request, partner_id, hashed_date): def home(request, partner_id, hashed_date):
import hashlib import hashlib
cs = CagetteShift() cs = CagetteShift()
...@@ -96,7 +93,7 @@ def _is_middled_filled_considered(reserved, max): ...@@ -96,7 +93,7 @@ def _is_middled_filled_considered(reserved, max):
def get_list_shift_calendar(request, partner_id): def get_list_shift_calendar(request, partner_id):
cs = CagetteShift() cs = CagetteShift()
[registerPartner, is_ftop] = cs.get_shift_partner(partner_id) [registerPartner, is_ftop] = shifts.fonctions.get_shift_partner(cs.o_api, partner_id)
use_new_members_space = getattr(settings, 'USE_NEW_MEMBERS_SPACE', False) use_new_members_space = getattr(settings, 'USE_NEW_MEMBERS_SPACE', False)
remove_15_minutes_at_shift_end = getattr(settings, 'REMOVE_15_MINUTES_AT_SHIFT_END', True) remove_15_minutes_at_shift_end = getattr(settings, 'REMOVE_15_MINUTES_AT_SHIFT_END', True)
...@@ -132,12 +129,12 @@ def get_list_shift_calendar(request, partner_id): ...@@ -132,12 +129,12 @@ def get_list_shift_calendar(request, partner_id):
event["title"] = title_prefix + str(value['seats_reserved']) + "/" + str(smax) event["title"] = title_prefix + str(value['seats_reserved']) + "/" + str(smax)
event["start"] = dateIsoUTC(value['date_begin_tz']) event["start"] = shifts.fonctions.dateIsoUTC(value['date_begin_tz'])
datetime_object = datetime.datetime.strptime(value['date_end_tz'], "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=15) datetime_object = datetime.datetime.strptime(value['date_end_tz'], "%Y-%m-%d %H:%M:%S") - datetime.timedelta(minutes=15)
if remove_15_minutes_at_shift_end is True: if remove_15_minutes_at_shift_end is True:
datetime_object -= datetime.timedelta(minutes=15) datetime_object -= datetime.timedelta(minutes=15)
event["end"] = dateIsoUTC(datetime_object.strftime("%Y-%m-%d %H:%M:%S")) event["end"] = shifts.fonctions.dateIsoUTC(datetime_object.strftime("%Y-%m-%d %H:%M:%S"))
if len(l) > 0: if len(l) > 0:
if use_new_members_space is True: if use_new_members_space is True:
...@@ -178,7 +175,7 @@ def get_list_shift_calendar(request, partner_id): ...@@ -178,7 +175,7 @@ def get_list_shift_calendar(request, partner_id):
def get_list_shift_partner(request, partner_id): def get_list_shift_partner(request, partner_id):
cs = CagetteShift() cs = CagetteShift()
[shiftData, is_ftop] = cs.get_shift_partner(partner_id) [shiftData, is_ftop] = shifts.fonctions.get_shift_partner(cs.o_api, partner_id)
for value in shiftData: for value in shiftData:
value['date_begin'] = value['date_begin'] + "Z" value['date_begin'] = value['date_begin'] + "Z"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment