def post(self, request): user = request.user # user = CustomUser.objects.get(username="******") employee = Employee.objects.get(user=user) print(request.body) data = json.loads(request.body) cell_info = data["cell_info"] graph_info = data["graph_info"] journal_name = cell_info["journal_name"] table_name = cell_info["table_name"] field_name = cell_info["field_name"] journal = Journal.objects.get(name=journal_name) table = Table.objects.get(name=table_name, journal_id=journal.id) field = Field.objects.get(name=field_name, table_id=table.id) dashboard_config = Setting.get_value(name="dashboard_config", employee=employee) if dashboard_config is None: dashboard_config = {} dashboard_config[str(field.id)] = dict(x=0, y=0, w=6, h=8, type=graph_info["type"]) Setting.set_value(name="dashboard_config", employee=employee, value=dashboard_config) return JsonResponse({"result": 1})
def test_animals_can_speak(self): """Settings are saved and work correctly""" Setting["brackets_test"] = "brackets_val" self.assertEqual(Setting["brackets_test"], "brackets_val") Setting.set_value(name='set_test', value='set_val') self.assertEqual(Setting.get_value('set_test1'), 'set_val') with self.assertRaises(ValueError): a = Setting['sadfsda']
def create_number_of_shifts(): shift_numbers = {'furnace': 2, 'leaching': 2, 'electrolysis': 2} for pl, num in shift_numbers.items(): plant = Plant.objects.get(name=pl) Setting.of(obj=plant)['number_of_shifts'] = num # overriding number of shifts for furnace plant reports_furn = Journal.objects.get(plant__name='furnace', name='reports_furnace_area') Setting.of(obj=reports_furn)['number_of_shifts'] = 3
def post(self, request): # DEBUG # user = CustomUser.objects.get(username="******") # employee = Employee.objects.get(user=user) employee = request.user.employee setting_data = json.loads(request.body) Setting.set_value(name=setting_data["name"], value=setting_data["value"], employee=employee) return JsonResponse({"status": 1})
def post(self, request): user = request.user employee = Employee.objects.get(user=user) graph_id = json.loads(request.body)["id"] dashboard_config = Setting.get_value(name="dashboard_config", employee=employee) if graph_id in dashboard_config.keys(): del dashboard_config[graph_id] Setting.set_value(name="dashboard_config", employee=employee, value=dashboard_config) return JsonResponse({"result": 1})
def bl_create(): Setting.objects.create(name='shift_assignment_time', value=Setting._dumps({"hours": 1})) Setting.objects.create(name='shift_edition_time', value=Setting._dumps({"hours": 12})) Setting.objects.create(name='allowed_positions', value=Setting._dumps({ "boss": 2, "laborant": 2 }))
def post(self, request): layout = json.loads(request.body) employee = Employee.objects.get(user=request.user) config = Setting.get_value(name="dashboard_config", employee=employee) for item in layout: id = item["i"] for key in item: if key not in ["i", "moved"]: config[id][key] = item[key] Setting.set_value(name="dashboard_config", employee=employee, value=config) return JsonResponse({"result": 1})
def get_time(shift): if request.user.has_perm(EDIT_CELLS): assignment_time = shift.end_time - timedelta( **Setting.of(shift)['shift_assignment_time']) assignment_time = assignment_time.isoformat() not_assignment_time = shift.end_time + timedelta( **Setting.of(shift)['shift_edition_time']) not_assignment_time = not_assignment_time.isoformat() return { "editing_mode_closing": assignment_time, "shift_closing": not_assignment_time } else: return None
def put(self, request): setting_data = json.loads(request.body) setting = Setting.objects.get(id=int(setting_data['id'])) setting.verbose_name = setting_data.get('value', setting.verbose_name) setting.value = Setting._dumps(setting_data['value']) setting.save() return JsonResponse({"status": 1})
def get(self, request): employee = Employee.objects.get(user=request.user) dashboard_config = Setting.get_value(name="dashboard_config", employee=employee) if dashboard_config: return JsonResponse({"config": dashboard_config}) else: return JsonResponse({"config": {}})
def get(self, request): # DEBUG # user = CustomUser.objects.get(username="******") # employee = Employee.objects.get(user=user) employee = request.user.employee name = request.GET.get("name", None) value = Setting.get_value(name=name, employee=employee) return JsonResponse(dict(name=value if value else ""), safe=False)
def _save_m2m(self): super()._save_m2m() value = self.data.get('pickled_value', None) name = self.data.get('name', None) employee_id = self.data.get('employee', None) content_type_id = self.data.get('content_type', None) object_id = self.data.get('object_id', None) if name: if content_type_id and object_id: ct = ContentType.objects.get_for_id(content_type_id) object = ct.get_object_for_this_type(pk=object_id) else: object = None Setting.set_value(name=name, value=value, employee=Employee.objects.get(id=employee_id) if employee_id else None, obj=object if object else None)
def post(self, request): setting_data = json.loads(request.body) Setting.objects.create(value=Setting._dumps(setting_data['value']), name=setting_data['name'], employee=request.user.employee, object_id=int(setting_data['scope']['id']) if setting_data.get('scope', None) else None, content_type=ContentType.objects.get( id=int(setting_data['content_type'])) if setting_data.get('scope', None) else None) return JsonResponse({"status": 1})
def process(self): page = self.data['page'] position = self.data['employee'].position allowed_positions = Setting.of(page)["allowed_positions"] if position in allowed_positions and \ page.responsibles.filter(position=position).count() < allowed_positions[position]: return True if self.data['employee'] in page.responsibles.all(): return True return True # СОСТАВ СМЕН
def process(self): page = self.data['page'] employee = self.data['employee'] assignment_time = Setting.of(page)['shift_assignment_time'] if timezone.localtime() > page.end_time - timedelta(**assignment_time) and \ employee not in page.responsibles.all(): return False if timezone.localtime() > page.end_time + timedelta(hours=12): return False # shift = get_or_none(Shift, date=page.date, order=int(page.order-1), journal=page.journal) # if shift and not shift.ended and employee not in page.responsibles.all(): # return False return True
def process(self): page = get_or_none(Shift, id=self.cleaned_data['shift_id']) if page and page.closed == True: Setting.of(page)['limited_access_employee_id_list'] = self.data[ 'emp_id_list'] end_time = timezone.localtime() + timedelta(**self.data['time']) end_of_limited_access.apply_async((page.id, ), eta=end_time) for min in (60, 40, 20): send_deferred_message.apply_async( ('warning', f'До конца ограниченного доступа осталось {min} минут'), eta=end_time - timedelta(minutes=min)) return page
def check_mode_permissions(employee: Employee, page, page_mode: str) -> bool: is_valid = False if page_mode == "validate": is_valid = employee.user.has_perm(VALIDATE_CELLS) if page_mode == "edit": if page.journal.type == "shift" or page.journal.type == "equipment": is_valid = not page.closed and employee.user.has_perm( EDIT_CELLS) and not page.ended if page.closed: limited_emp_id_list = Setting.of( page)["limited_access_employee_id_list"] if limited_emp_id_list and employee.id in limited_emp_id_list: is_valid = True if page_mode == "view": is_valid = employee.user.has_perm(VIEW_CELLS) return is_valid
def fill_tables_lists(): Setting.objects.bulk_create([ Setting(scope=Journal.objects.get( plant=Plant.objects.get(name='furnace'), name='furnace_changed_fraction'), name='tables_list', value=pickle.dumps( ["tables/furnace/furnace_changed_fraction/main.html"])), Setting( scope=Journal.objects.get(plant=Plant.objects.get(name='furnace'), name='concentrate_report'), name='tables_list', value=pickle.dumps([ "tables/furnace/concentrate_report/small.html", "tables/furnace/concentrate_report/big.html", "tables/furnace/concentrate_report/upper.html" ])), Setting(scope=Journal.objects.get( plant=Plant.objects.get(name='furnace'), name='technological_tasks'), name='tables_list', value=pickle.dumps( ["tables/furnace/technological_tasks/main.html"])), Setting( scope=Journal.objects.get(plant=Plant.objects.get(name='furnace'), name='reports_furnace_area'), name='tables_list', value=pickle.dumps([ "tables/furnace/reports_furnace_area/main.html", "tables/furnace/reports_furnace_area/self_protection.html", "tables/furnace/reports_furnace_area/airmachines.html", "tables/furnace/reports_furnace_area/concentration_by_time.html", "tables/furnace/reports_furnace_area/fences.html", "tables/furnace/reports_furnace_area/worth.html", "tables/furnace/reports_furnace_area/electrofilter.html", "tables/furnace/reports_furnace_area/udel.html", "tables/furnace/reports_furnace_area/area_class_cinder.html", "tables/furnace/reports_furnace_area/warehouse_concentrates.html", "tables/furnace/reports_furnace_area/corrective_actions.html", "tables/furnace/reports_furnace_area/places_of_sampling.html" ])), Setting( scope=Journal.objects.get(plant=Plant.objects.get(name='furnace'), name='furnace_repair'), name='tables_list', value=pickle.dumps(["tables/furnace/furnace_repair/repair.html"])), Setting( scope=Journal.objects.get(plant=Plant.objects.get(name='furnace'), name='report_income_outcome_schieht'), name='tables_list', value=pickle.dumps([ "tables/furnace/report_income_outcome_schieht/supply_of_zinc_concentrates.html", "tables/furnace/report_income_outcome_schieht/main.html", "tables/furnace/report_income_outcome_schieht/small_plan.html", "tables/furnace/report_income_outcome_schieht/year_plan_schieht.html", "tables/furnace/report_income_outcome_schieht/summary.html" ])), Setting(scope=Journal.objects.get( plant=Plant.objects.get(name='furnace'), name='metals_compute'), name='tables_list', value=pickle.dumps([ "tables/furnace/metals_compute/sgok.html", "tables/furnace/metals_compute/cinder_conc.html", "tables/furnace/metals_compute/main.html", "tables/furnace/metals_compute/contain_zn.html", "tables/furnace/metals_compute/concentrat.html", "tables/furnace/metals_compute/gof.html", "tables/furnace/metals_compute/sns.html", "tables/furnace/metals_compute/avg_month.html" ])), Setting(scope=Journal.objects.get( plant=Plant.objects.get(name='electrolysis'), name='masters_report'), name='tables_list', value=pickle.dumps([ "tables/electrolysis/masters_report/melt_area2.html", "tables/electrolysis/masters_report/zinc.html", "tables/electrolysis/masters_report/melt_area1.html", "tables/electrolysis/masters_report/seria1.html", "tables/electrolysis/masters_report/seria4.html", "tables/electrolysis/masters_report/seria3.html", "tables/electrolysis/masters_report/last.html", "tables/electrolysis/masters_report/params.html" ])), Setting( scope=Journal.objects.get( plant=Plant.objects.get(name='electrolysis'), name='electrolysis_technical_report_3_degree'), name='tables_list', value=pickle.dumps([ "tables/electrolysis/electrolysis_technical_report_3_degree/right.html", "tables/electrolysis/electrolysis_technical_report_3_degree/left.html" ])), Setting( scope=Journal.objects.get( plant=Plant.objects.get(name='electrolysis'), name='electrolysis_technical_report_4_degree'), name='tables_list', value=pickle.dumps([ "tables/electrolysis/electrolysis_technical_report_4_degree/right.html", "tables/electrolysis/electrolysis_technical_report_4_degree/left.html" ])), Setting( scope=Journal.objects.get( plant=Plant.objects.get(name='electrolysis'), name='electrolysis_technical_report_12_degree'), name='tables_list', value=pickle.dumps([ "tables/electrolysis/electrolysis_technical_report_12_degree/right.html", "tables/electrolysis/electrolysis_technical_report_12_degree/left.html" ])), Setting( scope=Journal.objects.get( plant=Plant.objects.get(name='electrolysis'), name='electrolysis_repair_report_tables'), name='tables_list', value=pickle.dumps([ "tables/electrolysis/electrolysis_repair_report_tables/main.html" ])), Setting( scope=Journal.objects.get(plant=Plant.objects.get(name='leaching'), name='leaching_repair_equipment'), name='tables_list', value=pickle.dumps( ["tables/leaching/leaching_repair_equipment/repair.html"])), Setting( scope=Journal.objects.get(plant=Plant.objects.get(name='leaching'), name='leaching_express_analysis'), name='tables_list', value=pickle.dumps([ "tables/leaching/leaching_express_analysis/tanks_availability.html", "tables/leaching/leaching_express_analysis/appt_hydrometal.html", "tables/leaching/leaching_express_analysis/reagents.html", "tables/leaching/leaching_express_analysis/neutral.html", "tables/leaching/leaching_express_analysis/zinc_pulp.html", "tables/leaching/leaching_express_analysis/tanks_for_finished_products.html", "tables/leaching/leaching_express_analysis/self_protection.html", "tables/leaching/leaching_express_analysis/vsns.html", "tables/leaching/leaching_express_analysis/schieht.html", "tables/leaching/leaching_express_analysis/thickeners.html", "tables/leaching/leaching_express_analysis/sample.html", "tables/leaching/leaching_express_analysis/neutral_thickeners.html", "tables/leaching/leaching_express_analysis/cinder.html", "tables/leaching/leaching_express_analysis/loads.html", "tables/leaching/leaching_express_analysis/shift_info.html", "tables/leaching/leaching_express_analysis/agitators.html" ])), ])
def cached_number_of_shifts(obj): return Setting.of(obj)['number_of_shifts']
def end_of_limited_access(page_id): page = get_or_none(Shift, id=page_id) if page: Setting.of(page)['limited_access_employee_id_list'] = None
def get_field_description(self, obj): return Setting.of(obj)['field_description']
def get_permissions(self, request, group): def get_time(shift): if request.user.has_perm(EDIT_CELLS): assignment_time = shift.end_time - timedelta( **Setting.of(shift)['shift_assignment_time']) assignment_time = assignment_time.isoformat() not_assignment_time = shift.end_time + timedelta( **Setting.of(shift)['shift_edition_time']) not_assignment_time = not_assignment_time.isoformat() return { "editing_mode_closing": assignment_time, "shift_closing": not_assignment_time } else: return None PERMISSIONS = [] APP = 'all_journals_app' VALIDATE_CELLS = APP + ".validate_cells" EDIT_CELLS = APP + ".edit_cells" PLANT_PERM = APP + ".modify_{plant}" user = request.user if user.is_superuser: PERMISSIONS = ["edit", "validate"] if group.journal.type == 'shift': shift = group if user.has_perm(PLANT_PERM.format(plant=shift.journal.plant.name) ) and user.has_perm(VALIDATE_CELLS): PERMISSIONS.append("validate") if user.has_perm(EDIT_CELLS): PERMISSIONS.append("edit") else: if shift.closed: limited_emp_id_list = Setting.of( shift)["limited_access_employee_id_list"] if limited_emp_id_list and user.id in limited_emp_id_list: PERMISSIONS = ["edit"] elif services.CheckRole.execute({"employee": user.employee, "page": shift}) and \ services.CheckTime.execute({"employee": user.employee, "page": shift}): if user.has_perm( PLANT_PERM.format(plant=shift.journal.plant.name) ) and user.has_perm(EDIT_CELLS): PERMISSIONS.append("edit") res = { "permissions": PERMISSIONS, "time": get_time(shift), } else: res = { "permissions": ["edit"] if user.has_perm( PLANT_PERM.format(plant=group.journal.plant.name)) else [], "time": None, } return res