def device_configuration(request): animal_id = request.matchdict['animal_id'] is_admin = check_permission('admin', request) email = get_user_email(request) animal = None if animal_id != None: try: animal = animal_service.get_animal_by_id(int(animal_id)) except ValueError: pass if (animal == None) or (not is_admin and animal.user.email != email): return HTTPFound(request.route_url("animals_list")) if 'form.submitted' in request.params: if 'preconfig_nr' in request.params: preconfig_nr = int(request.params['preconfig_nr']) animal_service.save_new_preconfigured_configuration( preconfig_nr, animal) elif 'alarm_state' in request.params: if request.params['alarm_state'] == 'True': animal_service.activate_alarm_state(animal) else: animal_service.deactivate_alarm_state(animal) elif is_admin: animal_service.save_new_custom_configuration(request, animal) return dict(is_admin=is_admin, animal=animal, is_in_alarm_state=animal_service.is_in_alarm_state(animal), preconfig_names=get_device_preconfig_names(), current_config=get_device_config_fancy_description( animal.get_current_configuration(), request))
def json_animal_approx_position(request): geojson = [] animal_id = request.matchdict['animal_id'] is_admin = check_permission('admin', request) email = get_user_email(request) animal = None if animal_id != None: try: animal = animal_service.get_animal_by_id(int(animal_id)) except ValueError: pass if animal != None and (is_admin or animal.user.email == email): instant = datetime.utcnow() position = animal.get_approx_position_as_geojson(instant) if position != None: parameters = { 'animal_name': animal.name, 'name': animal.user.email, 'imei': animal.imei, 'date': format_utcdatetime(instant, request) } if is_admin: popup_str = _("animal_app_position_popup_admin", domain='Ondestan', mapping=parameters) else: popup_str = _("animal_app_position_popup", domain='Ondestan', mapping=parameters) geojson.append({ "type": "Feature", "properties": { "id": animal.id, "name": animal.name, "imei": animal.imei, "owner": animal.user.email, "active": animal.active, "date": format_utcdatetime(instant, request), "popup": get_localizer(request).translate(popup_str) }, "geometry": eval(position) }) return geojson
def device_configuration_history(request): animal_id = request.matchdict['animal_id'] is_admin = check_permission('admin', request) email = get_user_email(request) animal = None if animal_id != None: try: animal = animal_service.get_animal_by_id(int(animal_id)) except ValueError: pass if (animal == None) or (not is_admin and animal.user.email != email): return HTTPFound(request.route_url("animals_list")) configurations = Page(animal.get_all_configurations(), page=int(request.params.get('p', 1)), items_per_page=20, url=Customizable_PageURL_WebOb(request, get_param='p')) return dict(is_admin=check_permission('admin', request), configurations=configurations)
def animal_charging_history_viewer(request): animal_id = request.matchdict['animal_id'] email = get_user_email(request) is_admin = check_permission('admin', request) animal = None if animal_id != None: try: animal = animal_service.get_animal_by_id(int(animal_id)) except ValueError: pass if (animal == None) or (not is_admin and animal.user.email != email): return HTTPFound(request.route_url("map")) parameters = {'max_positions': max_positions} too_many_positions_msg = _("too_many_positions_in_selected_time_interval", domain='Ondestan', mapping=parameters) return dict( view=animal.get_bounding_box_as_json(), animal_id=animal_id, too_many_positions_msg=get_localizer(request).\ translate(too_many_positions_msg) )
def json_animal_charging_positions(request): animal_id = request.matchdict['animal_id'] email = get_user_email(request) is_admin = check_permission('admin', request) animal = None if animal_id != None: try: animal = animal_service.get_animal_by_id(int(animal_id)) except ValueError: pass geojson = [] if animal != None and (is_admin or animal.user.email == email): start = None end = None if 'start' in request.GET: try: start = parse_to_utcdatetime(request.GET['start']) except ValueError: pass if 'end' in request.GET: try: end = parse_to_utcdatetime(request.GET['end']) except ValueError: pass n_positions = animal.n_filter_charging_positions(start, end) logger.debug("Found " + str(n_positions) + " charging positions for animal " + str(animal_id)) if n_positions > 0: if animal.name != None and len(animal.name) > 0: name = animal.name else: name = animal.imei positions = animal.filter_charging_positions(start, end) for position in positions: if int(position.battery) == position.battery: position.battery = int(position.battery) fancy_date = get_fancy_time_from_utc(position.\ date, request=request) parameters = { 'animal_name': name, 'name': animal.user.email, 'imei': animal.imei, 'battery': str(position.battery), 'date': fancy_date, 'plot': animal.plot.name if animal.plot != None else '---' } popup_str = _("animal_popup_admin", domain='Ondestan', mapping=parameters) geojson.append({ "type": "Feature", "properties": { "id": animal.id, "name": animal.name, "imei": animal.imei, "battery": position.battery, "owner": animal.user.email, "active": animal.active, "outside": position.outside(), "date": format_utcdatetime(position.date, request), "fancy_date": fancy_date, "popup": get_localizer(request).translate(popup_str) }, "geometry": eval(position.geojson) }) # We return the max number of positions plus one, so it can # detect there are more and not just the barrier number if len(geojson) == (max_positions + 1): logger.warning( "Too many charging positions requested for animal " + str(animal_id) + ", only the last " + str(max_positions + 1) + " will be returned") break else: geojson.append({ "type": "Feature", "properties": { "id": animal.id, "name": animal.name, "imei": animal.imei, "battery": None, "owner": animal.user.email, "active": animal.active, "outside": None, "date": None, "fancy_date": None, "popup": None }, "geometry": None }) return geojson
def delete_device(request): order_id = animal_service.get_animal_by_id(request.matchdict['device_id'])\ .order_id animal_service.delete_animal_by_id(request.matchdict['device_id']) return HTTPFound(request.route_url("order_devices", order_id=order_id))