def activate_animal_by_id(request): animal_id = request.matchdict['device_id'] if check_permission('admin', request): email = None else: email = get_user_email(request) if animal_id != None: animal = Animal().queryObject().filter(Animal.id == animal_id).scalar() if (email != None): if (animal.user.email != email): return animal.active = True animal.update() same_order_devices = animal.order.devices all_active = True for device in same_order_devices: all_active = all_active and device.active if all_active: active_order_state = Order_state._STATES[len(Order_state._STATES) - 2] if device.order.states[0].state != active_order_state: ondestan.services.order_service.update_order_state( animal.order.id, active_order_state, request)
def update_animal_plot(animal_id, plot_id, user_id=None): if animal_id != None: if user_id != None: animal = Animal().queryObject().filter( and_(Animal.id == animal_id, Animal.user_id == user_id)).scalar() else: animal = Animal().queryObject().filter(Animal.id == animal_id)\ .scalar() if animal != None: if plot_id == None or plot_id == '': animal.plot_id = None animal.update() else: plot = ondestan.services.plot_service.get_plot_by_id(plot_id) if plot != None: if user_id != None: if plot.user_id != user_id: logger.error( "Cannot assign the non-existent plot " + "with id " + str(plot_id) + " to animal with id " + str(animal_id) + " for user id " + str(user_id)) return animal.plot_id = plot_id animal.update() else: logger.error( "Cannot assign the non-existent plot with id " + str(plot_id) + " to animal with id " + str(animal_id) + " for user id " + str(user_id)) else: logger.error("Cannot update the non-existent animal with id " + str(animal_id) + " for user id " + str(user_id))
def get_active_animals(email=None): if email != None: return Animal().queryObject().filter( and_(Animal.user.has(email=email), Animal.active == True)).order_by(Animal.name, Animal.id).all() else: return Animal().queryObject().filter(Animal.active == True).\ order_by(Animal.name, Animal.id).all()
def create_animal(imei, phone, order, name=''): animal = Animal() animal.active = False animal.imei = imei animal.phone = phone if (name != None and name != ''): animal.name = name if (order != None and order.user != None): animal.order_id = order.id animal.user_id = order.user.id animal.save()
def update_animal_plot(animal_id, plot_id, user_id=None): if animal_id != None: if user_id != None: animal = Animal().queryObject().filter(and_(Animal.id == animal_id, Animal.user_id == user_id)).scalar() else: animal = Animal().queryObject().filter(Animal.id == animal_id)\ .scalar() if animal != None: if plot_id == None or plot_id == '': animal.plot_id = None animal.update() else: plot = ondestan.services.plot_service.get_plot_by_id(plot_id) if plot != None: if user_id != None: if plot.user_id != user_id: logger.error("Cannot assign the non-existent plot " + "with id " + str(plot_id) + " to animal with id " + str(animal_id) + " for user id " + str(user_id)) return animal.plot_id = plot_id animal.update() else: logger.error("Cannot assign the non-existent plot with id " + str(plot_id) + " to animal with id " + str(animal_id) + " for user id " + str(user_id)) else: logger.error("Cannot update the non-existent animal with id " + str(animal_id) + " for user id " + str(user_id))
def update_animal_name(animal_id, name, user_id=None): if (id != None and name != None): if user_id != None: animal = Animal().queryObject().filter(and_(Animal.id == animal_id, Animal.user_id == user_id)).scalar() else: animal = Animal().queryObject().filter(Animal.id == animal_id).scalar() if animal != None: animal.name = name animal.update() else: logger.error("Cannot update the non-existent animal with id " + str(animal_id) + " for user id " + str(user_id))
def deactivate_animal_by_id(request): animal_id = request.matchdict['device_id'] if check_permission('admin', request): email = None else: email = get_user_email(request) if animal_id != None: animal = Animal().queryObject().filter(Animal.id == animal_id).scalar() if (email != None): if (animal.user.email != email): return animal.active = False animal.update()
def get_animals_bounding_box(self): positions = [] if self.role.name == Role._ADMIN_ROLE: animals = Animal().queryObject().all() else: animals = self.animals for animal in animals: if animal.n_positions > 0: positions.append(animal.positions[0].geom) return self.session.scalar(func.ST_Envelope( func.ST_MakeLine(array(positions)))) if len(positions) > 0\ else None
def update_animal_name(animal_id, name, user_id=None): if (id != None and name != None): if user_id != None: animal = Animal().queryObject().filter( and_(Animal.id == animal_id, Animal.user_id == user_id)).scalar() else: animal = Animal().queryObject().filter( Animal.id == animal_id).scalar() if animal != None: animal.name = name animal.update() else: logger.error("Cannot update the non-existent animal with id " + str(animal_id) + " for user id " + str(user_id))
def delete_animal_by_id(animal_id): if animal_id != None: animal = Animal().queryObject().filter(Animal.id == animal_id).scalar() if animal.n_positions == 0: animal.delete()
def get_animal_by_imei(imei): if imei != None: return Animal().queryObject().filter(Animal.imei == imei).scalar() else: return None
def get_animal_by_phone(phone): if phone != None: return Animal().queryObject().filter(Animal.phone == phone).scalar() else: return None
def get_all_animals(email=None): if email != None: return Animal().queryObject().filter(Animal.user.has(email=email)).\ order_by(Animal.name, Animal.id).all() else: return Animal().queryObject().order_by(Animal.name, Animal.id).all()
def get_animal_by_id(animal_id): if animal_id != None: return Animal().queryObject().filter(Animal.id == animal_id).scalar() else: return None