def add_hospital(request): form = FacilityForm(request.POST) if form.is_valid(): data = form.cleaned_data lat = data['lat'] lon = data['lon'] if not (lat and lon): return {'success': False, 'error': 'lat or lon are empty'} the_geom = Point(lon, lat, srid=settings.DISPLAY_SRID) obj = Facility.objects.create(the_geom=the_geom) return {'success': True, 'id': obj.pk}
def edit_hospital(request, id_): form = FacilityForm(request.POST) if form.is_valid(): 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"] if weekday >= 0 and optime >= 0: try: op_time = current_obj.openingtime_set.get( weekday=weekday, index=optime ) except OpeningTime.DoesNotExist: op = OpeningTime( facility=current_obj, weekday=weekday, opening=(opening or None), closing=(closing or None), index=optime ) try: op.save() except ValidationError, exc: return {"success": False, "error": "%r" % exc} else: if opening is None and closing is None: op_time.delete() 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}
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}