from io import BytesIO import pandas as pd from service import models class PointService: def update_fact(self, postamat_id: str, fact: int): qs = self.get_point_by_postamat_id(postamat_id) qs.update(**{'fact': fact}) def update_postamat_id(self, point_id: int, postamat_id: str): qs = self.get_point_by_id(point_id) qs.update(**{'postamat_id': postamat_id}) @staticmethod def update_status(qs: models.PlacementPoint, new_status: str) -> models.PlacementPoint: qs.update(**{'status': new_status}) @staticmethod def get_point_by_id(point_id: int): return models.PlacementPoint.objects.filter(pk=point_id) @staticmethod def get_point_by_postamat_id(postamat_id: str): return models.PlacementPoint.objects.filter(postamat_id=postamat_id) @staticmethod def to_excel(qs: models.PlacementPoint): data = pd.DataFrame(list(qs.values())) data['start_date'] = data['start_date'].dt.tz_localize(None) data['sample_trn'] = data['sample_trn'].astype(int) with BytesIO() as b: with pd.ExcelWriter(b) as writer: data.to_excel( writer, sheet_name='Placement Points', index=False, ) return b.getvalue() @staticmethod def get_first_10_k(): if models.PlacementPoint.objects.count() > 10000: qs = models.PlacementPoint.objects.order_by('-prediction_current').all()[10000] return qs.prediction_current else: return models.PlacementPoint.objects.order_by('prediction_current').first().prediction_current