def create_event(self, worker: Worker, operation_name: str) -> Event: try: operation, created = Operation.objects.get_or_create( name=operation_name) return Event.objects.create(person=worker, operation=operation) except: # TODO ServiceException("cannot create new event")
def get_layout(self, **kwargs): try: layout, is_created = Layout.objects.get_or_create(**kwargs) except: # TODO raise ServiceException('layout data incorrect') return layout
def create_new_model(self, **kwargs): layout_data = kwargs.pop('layout') kwargs['layout'] = BoardService().get_layout(**layout_data) try: new_model = BoardModel.objects.create(**kwargs) except Exception as e: # TODO raise ServiceException('cannot create new model', e) return new_model
def create_components(self, model: BoardModel, components: dict): new_components = [BoardModelMaterial(quantity=component.get('quantity'), material=component.get('material'), model=model) for component in components] try: BoardModelMaterial.objects.bulk_create(new_components) except: # TODO raise ServiceException('internal error - cannot create')
def start_mold_history_record(self, press, mold, worker, event=None): if not event: event = EventService().create_event(worker=worker, operation_name='changing mold') try: MoldHistory.objects.create(press=press, mold=mold, started=event) except: # TODO raise ServiceException( 'internal error - cannot create started mold record')
def start_worker_history_record(self, worker, event=None): if not event: event = EventService().create_event(worker=worker, operation_name='work start') try: return WorkerWorkHistory.objects.create(worker=worker, started=event) except: # TODO raise ServiceException( 'internal error - cannot create started mold record')
def get_open_mold_history_records(self, press): try: return MoldHistory.objects.get(press=press, finished__isnull=True, started__isnull=False) except MoldHistory.DoesNotExist: return None except: # TODO raise ServiceException( 'internal error - cannot find previous history mold record')
def create_order(self, validated_data): order_records_data = validated_data.pop('records', []) try: order = Order.objects.create(**validated_data) except: # TODO raise ServiceException('cannot create order') self.create_records(order=order, data=order_records_data) return order
def get_order_quantity(self, board, order_id): try: order_qty = sum( OrderRecord.objects.filter( order=order_id, board_model=board.model).values_list('quantity', flat=True)) return order_qty except: # TODO raise ServiceException('incorrect data')
def download_price_for_symbol(self, symbol): if symbol == POLAND_SYMBOL: return 1 response = requests.get( 'http://api.nbp.pl/api/exchangerates/rates/a/{}'.format(symbol), headers=HEADERS) data = response.json() if response.status_code == 200: return data['rates'][0]['mid'] else: raise ServiceException('cannot download price for '.format(symbol))
def update_order(self, instance, validated_data): order_records_data = validated_data.pop('records', []) if order_records_data: instance.records.all().delete() self.create_records(order=instance, data=order_records_data) try: for attribute, value in validated_data.items(): setattr(instance, attribute, value) instance.save() except: # TODO raise ServiceException( 'cannot update order record - invalid order data') return instance
def create_records(self, order, data): records = [] for record in data: layout = None if 'layout' in record: layout = BoardService().get_layout(**record['layout']) records.append( OrderRecord(order=order, board_model=record.get('board_model'), quantity=record.get('quantity'), layout=layout)) try: OrderRecord.objects.bulk_create(records) except: # TODO raise ServiceException('cannot create order records')
def create_new_board(self, **kwargs): layout_data = kwargs.pop('layout', None) barcode = kwargs.get('barcode') model = self.get_model_from_barcode(barcode=barcode) if layout_data: layout = self.get_layout(**layout_data) else: layout = model.layout try: new_board = Board.objects.create(barcode=barcode, model=model, company=model.company, layout=layout, second_category=False) except Exception as e: # TODO raise ServiceException('cannot create new board', e) return new_board
def add_missing_scans(self, last_scan): missing_board_scans = [] station = last_scan.get('station') barcode = last_scan.get('barcode') prev_stations = Station.objects.filter( id__in=range(station.id - 1, 0, -1)).values_list('id', flat=True) exists_scans = {station: barcode for station, barcode in BoardScan.objects.filter(barcode=barcode, station_id__in=prev_stations).values_list( 'station__id', 'barcode__barcode')} for station in prev_stations: if not exists_scans.get(station): missing_board_scans.append(BoardScan(barcode=barcode, worker=last_scan.get('worker'), station_id=station, comment="added automatic")) try: BoardScan.objects.bulk_create(missing_board_scans) except: # TODO raise ServiceException('cannot create missing scan')
def create_operation(self, name: str) -> Operation: try: return Operation.objects.create(name=name) except: # TODO ServiceException("cannot create operation")