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
from outils.common import Verification
from products.models import OFF
from envelops.models import CagetteEnvelops
import shifts.fonctions
import sys
import pytz
......@@ -952,7 +953,7 @@ class CagetteMember(models.Model):
fields = fields + ['shift_type', 'makeups_to_do', 'display_ftop_points', 'display_std_points', 'shift_type']
cond.append(['shift_type', '=', 'standard'])
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
elif search_type == "shift_template_data":
fields = CagetteMember.m_short_default_fields
......@@ -1393,8 +1394,7 @@ class CagetteMembers(models.Model):
# 1 : fetching members with no makeups to do but with some makeups to come
# 2 : providing makeups to come to all members
cs = CagetteShift()
makeups_to_come_per_partner = cs.get_partners_with_makeups_to_come()
makeups_to_come_per_partner = shifts.fonctions.get_partners_with_makeups_to_come(api)
# 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())]]
......@@ -1414,11 +1414,10 @@ class CagetteMembers(models.Model):
return res
@staticmethod
def add_makeups_to_come_to_member_data(res):
def add_makeups_to_come_to_member_data(api, res):
if res:
cs = CagetteShift()
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'])
@staticmethod
......@@ -1518,6 +1517,4 @@ class CagetteUser(models.Model):
except Exception as e:
res['error'] = str(e)
return res
from shifts.models import CagetteShift
return res
\ No newline at end of file
import datetime, pytz
tz = pytz.timezone("Europe/Paris")
def dateIsoUTC(myDate):
tz = pytz.timezone("Europe/Paris")
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
from outils.common import Verification
from members.models import CagetteMember
from members.models import CagetteUser
import shifts.fonctions
from pytz import timezone
......@@ -58,7 +59,7 @@ class CagetteShift(models.Model):
try:
now = datetime.datetime.now(tz)
# 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)
if "successive_shifts_allowed" in rules:
min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2)
......@@ -76,7 +77,7 @@ class CagetteShift(models.Model):
if 'start_date' in cw:
sd = cw['start_date']
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']):
answer = False
coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id))
......@@ -127,7 +128,7 @@ class CagetteShift(models.Model):
try:
now = datetime.datetime.now(tz)
# 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)
if "successive_shifts_allowed" in rules:
min_duration = getattr(settings, 'MIN_SHIFT_DURATION', 2)
......@@ -145,7 +146,7 @@ class CagetteShift(models.Model):
if 'start_date' in cw:
sd = cw['start_date']
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']):
answer = False
coop_logger.info("services max par cycle atteint pour partner_id %s", str(partner_id))
......@@ -236,39 +237,6 @@ class CagetteShift(models.Model):
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):
"""vérifie si une shift est un rattrapage"""
......@@ -277,7 +245,6 @@ class CagetteShift(models.Model):
shiftData = self.o_api.search_read('shift.registration', cond, fields)
return shiftData[0]["is_makeup"]
def get_shift_calendar(self, is_ftop, start, end):
"""Récupère les shifts à partir de maintenant pour le calendier"""
......
......@@ -4,16 +4,13 @@ from outils.common import Verification
from shifts.models import CagetteShift
from members.models import CagetteMember
import shifts.fonctions
# working_state = ['up_to_date', 'alert', 'exempted', 'delay', 'suspended']
state_shift_allowed = ["up_to_date", "alert", "delay"]
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):
import hashlib
cs = CagetteShift()
......@@ -96,7 +93,7 @@ def _is_middled_filled_considered(reserved, max):
def get_list_shift_calendar(request, partner_id):
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)
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):
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)
if remove_15_minutes_at_shift_end is True:
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 use_new_members_space is True:
......@@ -178,7 +175,7 @@ def get_list_shift_calendar(request, partner_id):
def get_list_shift_partner(request, partner_id):
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:
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