else: op_time.opening = opening or op_time.opening op_time.closing = closing or op_time.closing try: op_time.save(force_update=True) except ValidationError, exc: return {"success": False, "error": "%r" % exc} current_data = dict([(k.name, getattr(current_obj, k.name)) for k in current_obj._meta.fields]) for key, value in data.items(): if key in request.POST: current_data[key] = value obj = Facility(**current_data) obj.updated_by = request.user if "pathologies[]" in request.POST: p_data = request.POST.getlist("pathologies[]") obj.pathologies.clear() obj.save(force_update=True) for p in p_data: if p: try: obj_p = Pathology.objects.get(name=p) obj.pathologies.add(obj_p) except Pathology.DoesNotExist: obj.pathologies.create(name=p) if "services[]" in request.POST:
def edit_hospital(request, id_): form = FacilityForm(request.POST) if form.is_valid() and request.POST: try: current_obj = Facility.objects.get(id=id_) except Facility.DoesNotExist: return {'success': False, 'error': 'Not found'} data = form.cleaned_data # timetable data weekday = data["weekday"] optime = data["optime"] opening = data["opening"] closing = data["closing"] del data["weekday"], data["optime"], data["opening"], \ data["closing"] res = timetable_filler(current_obj, weekday, optime, opening, closing) if res: return res current_data = dict([(k.name, getattr(current_obj, k.name)) for k in current_obj._meta.fields]) for key, value in data.items(): if key in request.POST: current_data[key] = value obj = Facility(**current_data) obj.updated_by = request.user if "pathologies[]" in request.POST: p_data = request.POST.getlist("pathologies[]") obj.pathologies.clear() obj.save(force_update=True) for p in p_data: if p: try: obj_p = Pathology.objects.get(name=p) obj.pathologies.add(obj_p) except Pathology.DoesNotExist: obj.pathologies.create(name=p) # TODO: is it good or bad? # TODO: This should be an async task remove_dangling_objects(Pathology) if "services[]" in request.POST: p_data = request.POST.getlist("services[]") obj.services.clear() obj.save(force_update=True) for p in p_data: if p: try: obj_p = MedicalService.objects.get(name=p) obj.services.add(obj_p) except MedicalService.DoesNotExist: obj.services.create(name=p) # TODO: is it good or bad? # TODO: This should be an async task remove_dangling_objects(MedicalService) obj.save(force_update=True) return {'success': True} return {'success': False, 'error': form.errors}