Ejemplo n.º 1
0
def orders(request):
    message = ''
    units = ''
    address = ''
    if 'form.submitted' in request.params:
        if 'id' in request.params:
            order_id = int(request.params['id'])
            state = int(request.params['state'])

            message = order_service.update_order_state(order_id, state,
                                                       request)
        else:
            message = order_service.create_order(request)
            if message != '':
                units = request.params['units']
                address = request.params['address']

    is_admin = check_permission('admin', request)
    if is_admin:
        query1 = order_service.get_all_pending_orders()
        pending_orders = Page(query1,
                              page=int(request.params.get('p1', 1)),
                              items_per_page=10,
                              url=Customizable_PageURL_WebOb(request,
                                                             get_param='p1'))
        query2 = order_service.get_all_processed_orders()
        processed_orders = Page(query2,
                                page=int(request.params.get('p2', 1)),
                                items_per_page=10,
                                url=Customizable_PageURL_WebOb(request,
                                                               get_param='p2'))
    else:
        query1 = order_service.get_all_pending_orders(get_user_email(request))
        pending_orders = Page(query1,
                              page=int(request.params.get('p1', 1)),
                              items_per_page=10,
                              url=Customizable_PageURL_WebOb(request,
                                                             get_param='p1'))
        query2 = order_service.get_all_processed_orders(
            get_user_email(request))
        processed_orders = Page(query2,
                                page=int(request.params.get('p2', 1)),
                                items_per_page=10,
                                url=Customizable_PageURL_WebOb(request,
                                                               get_param='p2'))

    is_admin = check_permission('admin', request)

    return dict(message=message,
                units=units,
                address=address,
                pending_orders=pending_orders,
                processed_orders=processed_orders,
                is_admin=is_admin)
Ejemplo n.º 2
0
def viewer(request):
    is_admin = check_permission('admin', request)
    n_new_orders = 0
    if is_admin:
        n_new_orders = len(order_service.get_all_unprocessed_orders())
    else:
        n_new_orders = len(
            order_service.get_all_unprocessed_orders(get_user_login(request))
        )
    return dict(project=u'Ondestán',
                can_edit=check_permission('edit', request),
                is_admin=is_admin,
                orders_msg=n_new_orders)
Ejemplo n.º 3
0
def orders(request):
    message = ''
    units = ''
    address = ''
    if 'form.submitted' in request.params:
        if 'id' in request.params:
            order_id = int(request.params['id'])
            state = int(request.params['state'])

            message = order_service.update_order_state(order_id, state,
                                                       request)
        else:
            message = order_service.create_order(request)
            if message != '':
                units = request.params['units']
                address = request.params['address']

    is_admin = check_permission('admin', request)
    if is_admin:
        pending_orders = order_service.get_all_pending_orders()
        processed_orders = order_service.get_all_processed_orders()
    else:
        pending_orders = order_service.get_all_pending_orders(
            get_user_login(request))
        processed_orders = order_service.get_all_processed_orders(
            get_user_login(request))

    return dict(
        message=message,
        units=units,
        address=address,
        pending_orders=pending_orders,
        processed_orders=processed_orders,
        is_admin=is_admin,
        )
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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))
Ejemplo n.º 6
0
def get_orders(request):
    is_admin = check_permission('admin', request)
    if is_admin:
        new_orders = get_all_pending_orders()
    else:
        new_orders = get_all_pending_orders(get_user_email(request))
    return new_orders
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
def json_inactive_animals(request):
    json = []
    if (check_permission('admin', request)):
        animals = animal_service.get_inactive_animals()
        if animals != None:
            logger.debug("Found " + str(len(animals)) +
                         " inactive animals for all users")
            for animal in animals:
                json.append({
                    "id": animal.id,
                    "name": animal.name,
                    "owner": animal.user.email,
                })
        else:
            logger.debug("Found no inactive animals for any user")
    else:
        email = get_user_email(request)
        animals = animal_service.get_inactive_animals(email)
        if animals != None:
            logger.debug("Found " + str(len(animals)) +
                         " inactive animals for user " + email)
            for animal in animals:
                json.append({
                    "id": animal.id,
                    "name": animal.name,
                    "owner": animal.user.email,
                })
        else:
            logger.debug("Found no inactive animals for user " + email)
    return json
Ejemplo n.º 9
0
def delete_plot(request):
    plot_id = request.GET['id']
    if check_permission('admin', request):
        return {'success': plot_service.delete_plot(plot_id)}
    else:
        user = user_service.get_user_by_email(get_user_email(request))
        return {'success': plot_service.delete_plot(plot_id, user.id)}
Ejemplo n.º 10
0
def update_plot_geom(request):
    points = []
    i = 0
    while ('x' + str(i)) in request.GET and ('y' + str(i)) in request.GET:
        points.append([
            float(request.GET['x' + str(i)]),
            float(request.GET['y' + str(i)])
        ])
        i += 1
    plot_id = request.GET['id']
    if check_permission('admin', request):
        plot = plot_service.update_plot_geom(points, plot_id)
    else:
        user = user_service.get_user_by_email(get_user_email(request))
        plot = plot_service.update_plot_geom(points, plot_id, user.id)

    if plot == None:
        return {'success': False}
    else:
        feature = {
            "type": "Feature",
            "properties": {
                "id": plot_id,
                "name": plot.name,
                "owner": plot.user.email,
                "popup": plot.name
            },
            "geometry": eval(plot.geojson)
        }
        return {'success': True, 'feature': feature}
Ejemplo n.º 11
0
def create_plot(request):
    points = []
    i = 0
    while ('x' + str(i)) in request.GET and ('y' + str(i)) in request.GET:
        points.append([
            float(request.GET['x' + str(i)]),
            float(request.GET['y' + str(i)])
        ])
        i += 1

    if 'name' in request.GET:
        name = request.GET['name']
    else:
        name = ''
    if 'userid' in request.GET and check_permission('admin', request):
        userid = request.GET['userid']
    else:
        userid = user_service.get_user_by_email(get_user_email(request)).id

    plot = plot_service.create_plot(points, name, userid)

    if plot == None:
        return {'success': False}
    else:
        feature = {
            "type": "Feature",
            "properties": {
                "id": plot.id,
                "name": plot.name,
                "owner": plot.user.email,
                "popup": plot.name
            },
            "geometry": eval(plot.geojson)
        }
        return {'success': True, 'feature': feature}
Ejemplo n.º 12
0
def plot_manager(request):
    user = user_service.get_user_by_email(get_user_email(request))
    is_admin = check_permission('admin', request)
    return dict(
        is_admin=is_admin,
        non_admin_users=user_service.get_non_admin_users() if is_admin else [],
        view=user.get_plots_bounding_box_as_json())
Ejemplo n.º 13
0
def get_orders(request):
    is_admin = check_permission('admin', request)
    if is_admin:
        new_orders = get_all_pending_orders()
    else:
        new_orders = get_all_pending_orders(
            get_user_email(request))
    return new_orders
Ejemplo n.º 14
0
def get_all_notifications(request):
    is_admin = check_permission('admin', request)
    if is_admin:
        return Notification().queryObject().\
                        order_by(desc(Notification.date)).all()
    else:
        return Notification().queryObject().filter(
                        Notification.user.has(email=get_user_email(request))).\
                        order_by(desc(Notification.date)).all()
Ejemplo n.º 15
0
def notifications(request):
    query = notification_service.get_all_notifications(request)
    notifications = Page(query,
                         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),
                notifications=notifications)
Ejemplo n.º 16
0
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)
Ejemplo n.º 17
0
def update_animal_name(request):
    if 'name' in request.params and 'id' in request.params:
        if check_permission('admin', request):
            animal_service.update_animal_name(request.params['id'],
                                              request.params['name'])
        else:
            user = user_service.get_user_by_email(get_user_email(request))
            animal_service.update_animal_name(request.params['id'],
                                              request.params['name'], user.id)
    return HTTPFound(location=request.route_url('map'))
Ejemplo n.º 18
0
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()
Ejemplo n.º 19
0
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()
Ejemplo n.º 20
0
def json_animals(request):
    geojson = []
    if (check_permission('admin', request)):
        animals = animal_service.get_all_animals()
        if animals != None:
            logger.debug("Found " + str(len(animals)) +
                         " animals for all users")
            for animal in animals:
                if len(animal.positions) > 0:
                    popup_str = animal.name + \
                                " (" + str(animal.positions[0].battery)\
                                + "%), property of " + animal.user.name + \
                                " (" + animal.user.login + ")"
                    geojson.append({
                        "type": "Feature",
                        "properties": {
                            "name": animal.name,
                            "battery": animal.positions[0].battery,
                            "owner": animal.user.login,
                            "outside": animal.positions[0].outside,
                            "popup": popup_str
                        },
                        "geometry": eval(animal.positions[0].geojson)
                    })
        else:
            logger.debug("Found no animals for any user")
    else:
        login = get_user_login(request)
        animals = animal_service.get_all_animals(login)
        if animals != None:
            logger.debug("Found " + str(len(animals)) +
                         " animals for user " + login)
            for animal in animals:
                if len(animal.positions) > 0:
                    popup_str = animal.name + " (" + str(animal.positions[0].
                        battery) + "%)"
                    geojson.append({
                        "type": "Feature",
                        "properties": {
                            "name": animal.name,
                            "battery": animal.positions[0].battery,
                            "owner": animal.user.login,
                            "outside": animal.positions[0].outside,
                            "popup": popup_str
                        },
                        "geometry": eval(animal.positions[0].geojson)
                    })
        else:
            logger.debug("Found no animals for user " + login)
    return geojson
Ejemplo n.º 21
0
def json_plots(request):
    geojson = []
    if (check_permission('admin', request)):
        plots = plot_service.get_all_plots()
        if plots != None:
            logger.debug("Found " + str(len(plots)) + " plots for all users")
            for plot in plots:
                parameters = {'plot_name': plot.name, 'name': plot.user.email}
                popup_str = _("plot_popup_admin",
                              domain='Ondestan',
                              mapping=parameters)
                geojson.append({
                    "type": "Feature",
                    "properties": {
                        "id": plot.id,
                        "name": plot.name,
                        "owner": plot.user.email,
                        "centroid": eval(plot.centroid_geojson),
                        "popup": get_localizer(request).translate(popup_str)
                    },
                    "geometry": eval(plot.geojson)
                })
        else:
            logger.debug("Found no plots for any user")
    else:
        email = get_user_email(request)
        plots = plot_service.get_all_plots(email)
        if plots != None:
            logger.debug("Found " + str(len(plots)) + " plots " + \
                         "for user " + email)
            for plot in plots:
                parameters = {'plot_name': plot.name, 'name': plot.user.email}
                popup_str = _("plot_popup",
                              domain='Ondestan',
                              mapping=parameters)
                geojson.append({
                    "type": "Feature",
                    "properties": {
                        "id": plot.id,
                        "name": plot.name,
                        "owner": plot.user.email,
                        "centroid": eval(plot.centroid_geojson),
                        "popup": get_localizer(request).translate(popup_str)
                    },
                    "geometry": eval(plot.geojson)
                })
        else:
            logger.debug("Found no plots for user " + email)
    return geojson
Ejemplo n.º 22
0
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
Ejemplo n.º 23
0
def animals_list(request):
    is_admin = check_permission('admin', request)
    if 'form.submitted' in request.params:
        if 'id' in request.params:
            user_id = None if is_admin else user_service.get_user_by_email(
                get_user_email(request)).id
            animal_id = int(request.params['id'])
            plot_id = None if request.params['plot'] == '' or\
                request.params['plot'] == None else int(request.params['plot'])
            animal_service.update_animal_plot(animal_id, plot_id, user_id)
    if is_admin:
        animals = animal_service.get_all_animals()
    else:
        email = get_user_email(request)
        animals = animal_service.get_all_animals(email)

    return dict(
        is_admin=is_admin,
        animals=animals,
    )
Ejemplo n.º 24
0
def json_plots(request):
    geojson = []
    if (check_permission('admin', request)):
        plots = plot_service.get_all_plots()
        if plots != None:
            logger.debug("Found " + str(len(plots)) + " plots for all users")
            for plot in plots:
                popup_str = plot.name + \
                            " property of " + plot.user.name + \
                            " (" + plot.user.login + ")"
                geojson.append({
                    "type": "Feature",
                    "properties": {
                        "name": plot.name,
                        "owner": plot.user.login,
                        "popup": popup_str
                    },
                    "geometry": eval(plot.geojson)
                })
        else:
            logger.debug("Found no plots for any user")
    else:
        login = get_user_login(request)
        plots = plot_service.get_all_plots(login)
        if plots != None:
            logger.debug("Found " + str(len(plots)) + " plots " + \
                         "for user " + login)
            for plot in plots:
                geojson.append({
                    "type": "Feature",
                    "properties": {
                        "name": plot.name,
                        "owner": plot.user.login,
                        "popup": plot.name
                    },
                    "geometry": eval(plot.geojson)
                })
        else:
            logger.debug("Found no plots for user " + login)
    return geojson
Ejemplo n.º 25
0
def get_new_web_notifications_for_logged_user(request):
    email = get_user_email(request)
    notifications = get_web_notifications(email, False)
    for notification in notifications:
        notification.archived = True
        notification.update()
    if (not check_permission('admin', request)):
        if len(ondestan.services.order_service.get_all_orders(email)) == 0:
            logger.debug("User " + email + " has no orders. Notification " +
                "about making a first one will be displayed.")
            notification = Notification()
            notification.level = 0
            notification.type = web_type
            notification.date = datetime.utcnow()
            notification.archived = False
            notification.text = "_('no_orders_notification_web'," +\
                " mapping={'url': '" + request.route_url('orders') +\
                "'}, domain='Ondestan')"
            notifications.append(notification)
        animals = ondestan.services.animal_service.get_all_animals(email)
        active_animals = False
        for animal in animals:
            if animal.active:
                active_animals = True
                break
        if active_animals and len(
                ondestan.services.plot_service.get_all_plots(email)) == 0:
            logger.debug("User " + email + " has animals with positions, but"
                + " no plots. Notification about creating a first one will be"
                + " displayed.")
            notification = Notification()
            notification.level = 0
            notification.type = web_type
            notification.date = datetime.utcnow()
            notification.archived = False
            notification.text = "_('no_plots_notification_web'," +\
                " mapping={'url': '" + request.route_url('plot_manager') +\
                "'}, domain='Ondestan')"
            notifications.append(notification)
    return notifications
Ejemplo n.º 26
0
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)
    )
Ejemplo n.º 27
0
def json_animals(request):
    geojson = []
    if (check_permission('admin', request)):
        animals = animal_service.get_all_animals()
        if animals != None:
            logger.debug("Found " + str(len(animals)) +
                         " animals for all users")
            for animal in animals:
                if animal.n_positions > 0:
                    if animal.name != None and len(animal.name) > 0:
                        name = animal.name
                    else:
                        name = animal.imei
                    fancy_date = get_fancy_time_from_utc(animal.positions[0].\
                                                        date, request=request)
                    parameters = {
                        'animal_name': name,
                        'name': animal.user.email,
                        'imei': animal.imei,
                        'battery': str(animal.current_battery_wo_charging),
                        '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": animal.current_battery,
                            "battery_wo_charging": animal.current_battery_wo_charging,
                            "charging": animal.currently_charging,
                            "owner": animal.user.email,
                            "active": animal.active,
                            "last_date": format_utcdatetime(animal.\
                                                            positions[0].date,
                                                            request),
                            "fancy_last_date": fancy_date,
                            "outside": animal.positions[0].outside(),
                            "popup": get_localizer(request).translate(
                                                                popup_str)
                        },
                        "geometry": eval(animal.positions[0].geojson)
                    })
                else:
                    geojson.append({
                        "type": "Feature",
                        "properties": {
                            "id": animal.id,
                            "name": animal.name,
                            "imei": animal.imei,
                            "battery": None,
                            "battery_wo_charging": None,
                            "charging": None,
                            "owner": animal.user.email,
                            "active": animal.active,
                            "last_date": None,
                            "fancy_last_date": None,
                            "outside": None,
                            "popup": None
                        },
                        "geometry": None
                    })
        else:
            logger.debug("Found no animals for any user")
    else:
        email = get_user_email(request)
        animals = animal_service.get_all_animals(email)
        if animals != None:
            logger.debug("Found " + str(len(animals)) + " animals for user " +
                         email)
            for animal in animals:
                if animal.n_positions > 0:
                    if animal.name != None and len(animal.name) > 0:
                        name = animal.name
                    else:
                        name = animal.imei
                    fancy_date = get_fancy_time_from_utc(animal.positions[0].\
                                                        date, request=request)
                    parameters = {
                        'animal_name': name,
                        'name': animal.user.email,
                        'imei': animal.imei,
                        'battery': str(animal.current_battery_wo_charging),
                        'date': fancy_date,
                        'plot': animal.plot.name if animal.plot != None\
                                else '---'
                    }
                    popup_str = _("animal_popup",
                                  domain='Ondestan',
                                  mapping=parameters)
                    geojson.append({
                        "type": "Feature",
                        "properties": {
                            "id": animal.id,
                            "name": animal.name,
                            "imei": animal.imei,
                            "battery": animal.current_battery,
                            "battery_wo_charging": animal.current_battery_wo_charging,
                            "charging": animal.currently_charging,
                            "owner": animal.user.email,
                            "active": animal.active,
                            "last_date": format_utcdatetime(animal.\
                                                            positions[0].date,
                                                            request),
                            "fancy_last_date": fancy_date,
                            "outside": animal.positions[0].outside(),
                            "popup": get_localizer(request).translate(
                                                                popup_str)
                        },
                        "geometry": eval(animal.positions[0].geojson)
                    })
                else:
                    geojson.append({
                        "type": "Feature",
                        "properties": {
                            "id": animal.id,
                            "name": animal.name,
                            "imei": animal.imei,
                            "battery": None,
                            "battery_wo_charging": None,
                            "charging": None,
                            "owner": animal.user.email,
                            "active": animal.active,
                            "last_date": None,
                            "fancy_last_date": None,
                            "outside": None,
                            "popup": None
                        },
                        "geometry": None
                    })
        else:
            logger.debug("Found no animals for user " + email)
    return geojson
Ejemplo n.º 28
0
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