Ejemplo n.º 1
0
def get_count_item_for_last_days(trailcam_id, days=7):
    date_now = datetime.now()
    date_7_days_ago = date_now - timedelta(days=days)

    time_group = DBSession.query(extract('months', TrailcamItem.date_original).label('m'),
                                 extract('days', TrailcamItem.date_original).label('d'),
                                 sa.func.count(TrailcamItem.id)) \
        .filter(TrailcamItem.date_original <= date_now) \
        .filter(TrailcamItem.date_original >= date_7_days_ago) \
        .filter(TrailcamItem.trailcam_id == trailcam_id) \
        .group_by('d') \
        .group_by('m') \
        .order_by(sa.desc('m')) \
        .order_by(sa.desc('d')) \
        .all()

    count_by_days = dict(
        ('{0}-{1}'.format(m, d), count) for m, d, count in time_group)
    count_for_last_days = []

    for i in range(days):
        date_i_days_ago = date_now - timedelta(days=i)
        current_key = '{d.month}-{d.day}'.format(d=date_i_days_ago)
        if current_key in count_by_days:
            count_for_last_days.append(
                (date_i_days_ago.isoformat(), count_by_days[current_key]))
        else:
            count_for_last_days.append((date_i_days_ago.isoformat(), 0))

    return count_for_last_days
Ejemplo n.º 2
0
def receive_events(request):
    json_body = request.json_body
    parsed_events_set = EventsSetSchema().loads(json_body)
    if len(parsed_events_set.errors.keys()) > 0:
        raise HTTPBadRequest()
    last_update_ts = int(parsed_events_set.data['stop'])

    events = []
    if 'data' in parsed_events_set.data:
        events = parsed_events_set.data['data']

    events_to_insert = []
    for event in events:
        existed = DBSession.query(exists().where(Event.event_id==event.event_id)).scalar()
        exists_in_inserted_list = any(ev.event_id == event.event_id for ev in events_to_insert)
        if not existed and not exists_in_inserted_list:
            events_to_insert.append(event)

    with transaction.manager:
        Meta.filter_by(key=LAST_UPDATE_KEY).update({
            'value': last_update_ts
        })
        for event in events_to_insert:
            DBSession.add(event)
    return response_ok(dict(
        count=len(events),
        start=parsed_events_set.data['start'],
        stop=parsed_events_set.data['stop'],
        lastUpdateTs=last_update_ts
    ))
Ejemplo n.º 3
0
def legend(request):
    if 'styles' not in request.GET.keys():
        raise HTTPBadRequest("Parameter 'styles' not found.")
    try:
        styles = map(int, request.GET.getall('styles'))
    except ValueError:
        raise HTTPBadRequest("Invalid 'styles' parameter. Only numbers.")

    result = []

    legend_list = DBSession.query(Resource).filter(Resource.parent_id.in_(styles))

    for legend in legend_list:
        legend_description = env.file_storage.filename(legend.description_fileobj)
        with open(legend_description, mode='r') as f:
            description = loads(f.read(), encoding='utf-8')
            if type(description) != list:
                description = list(description)
            element = dict(
                id=legend.id,
                legend_id=legend.id,
                name=legend.display_name or legend.keyname,
                children=description
            )
            result.append(element)

    return Response(dumps(result), content_type='application/json', charset='utf-8')
Ejemplo n.º 4
0
def geocollection(request):
    try:
        date = str(request.GET.get('datetime', datetime.now()))
        if 'layers' in request.GET:
            qlayers = map(int, request.GET['layers'].split(','))
            print qlayers
            layers = DBSession.query(VectorLayer)\
                .filter(VectorLayer.id.in_(qlayers))\
                .all()
        else:
            layers = DBSession.query(VectorLayer)\
                .all()
        layers = filter(lambda layer: layer.has_permission(PD_READ, request.user), layers)
        print layers
    except Exception as e:
        raise HTTPBadRequest(e.message)

    features = GeoJsonFeatureList()

    # Запрос коллекции GeoJSON объектов, попадающих в заданную область видимости
    if 'bbox' in request.GET:
        try:
            bbox = map(float, request.GET['bbox'].split(','))
            print bbox
            geometry = box(*bbox, srid=3857)
            for layer in layers:
                query = layer.feature_query()
                query.geom()
                query.intersects(geometry)
                for feature in query():
                    print feature
                    feature.fields['__layer__'] = feature.layer.id
                    features.append(feature)
        except Exception as e:
            print e
            raise HTTPBadRequest(e.message)
    else:
        raise HTTPBadRequest()
    print len(features)

    for f in features:
        f._geom = geometry_transform(f.geom, f.layer.srs_id, 4326)
    result = geojson.dumps(features, cls=ComplexEncoder)

    return Response(result, content_type='application/json')
Ejemplo n.º 5
0
def webmap_id(ngw_resource_group):
    with transaction.manager:
        user = User(
            keyname=TEST_USER_KEYNAME,
            display_name='Test User',
        ).persist()

        test_root_resource = ResourceGroup.filter_by(id=0).one()
        append_acl(test_root_resource, 'allow', user, 'resource', 'read',
                   ResourceGroup.identity)

        test_resource_group = ResourceGroup.filter_by(
            id=ngw_resource_group).one()
        append_acl(test_resource_group, 'allow', user, 'resource', 'read',
                   ResourceGroup.identity)

        webmap = WebMap(parent_id=ngw_resource_group,
                        display_name=__name__,
                        owner_user=user,
                        root_item=WebMapItem(item_type='root')).persist()

        append_acl(webmap, 'allow', user, 'resource', 'read', WebMap.identity)
        append_acl(webmap, 'allow', user, 'webmap', 'annotation_read',
                   WebMap.identity)
        append_acl(webmap, 'allow', user, 'webmap', 'annotation_write',
                   WebMap.identity)

        user_admin_id = User.by_keyname('administrator').id
        make_annotation(webmap, public=True, user_id=user_admin_id)
        make_annotation(webmap, public=False, user_id=user_admin_id)

        make_annotation(webmap, public=True, user_id=user.id)
        make_annotation(webmap, public=False, user_id=user.id)

    DBSession.flush()

    yield webmap.id

    with transaction.manager:
        DBSession.query(ResourceACLRule).filter(
            ResourceACLRule.principal_id == user.id).delete()
        DBSession.delete(WebMap.filter_by(id=webmap.id).one())
        DBSession.delete(User.filter_by(id=user.id).one())
Ejemplo n.º 6
0
    def get_project_by_resource(cls, resource):
        if '_project_cache' not in cls.__dict__.keys():
            db_session = DBSession()
            projects = db_session.query(Project).all()
            cls._project_cache = {project.root_resource_id: project for project in projects if
                                  project.root_resource_id is not None}

        res = resource
        while res:
            if res.id in cls._project_cache.keys():
                return cls._project_cache[res.id]
            res = res.parent

        return None
Ejemplo n.º 7
0
def get_trailcam_items(request):
    trailcam_id = request.matchdict['trailcam_id']

    try:
        trailcam = DBSession.query(Trailcam).get(trailcam_id)
        trailcam_items = trailcam.items
    except NoResultFound:
        raise HTTPNotFound()

    trailcam_items_result = [
        trailcam_item.as_json_dict() for trailcam_item in trailcam_items
    ]

    return trailcam_items_result
Ejemplo n.º 8
0
def test_last_activity(ngw_env, ngw_webtest_app):
    with transaction.manager:
        for keyname, last_activity in (
            ('guest', datetime.utcnow()),
            ('administrator', None),
        ):
            DBSession.query(User).filter_by(keyname=keyname).update(
                dict(last_activity=last_activity))

    with ngw_env.auth.options.override(activity_delta=timedelta(seconds=0)):
        with freeze_time() as frozen_datetime:
            ngw_webtest_app.get('/resource/0', status='*')
            assert User.by_keyname('guest').last_activity == frozen_datetime.time_to_freeze
            last_activity = User.by_keyname('guest').last_activity

    with ngw_env.auth.options.override(activity_delta=timedelta(seconds=100)):
        ngw_webtest_app.get('/resource/0', status='*')
        assert User.by_keyname('guest').last_activity == last_activity

    with ngw_env.auth.options.override(activity_delta=timedelta(seconds=0)):
        with freeze_time() as frozen_datetime:
            ngw_webtest_app.authorization = ('Basic', ('administrator', 'admin'))
            ngw_webtest_app.get('/resource/0', status='*')
            assert User.by_keyname('administrator').last_activity == frozen_datetime.time_to_freeze
Ejemplo n.º 9
0
def trailcam_dashboard(request):
    trailcam_id = request.matchdict['trailcam_id']

    try:
        trailcam = DBSession.query(Trailcam).get(trailcam_id)
        trailcam_items = trailcam.items
    except NoResultFound:
        raise HTTPNotFound()

    return {
        'trailcam': trailcam,
        'items_count': len(trailcam_items),
        'items_count_by_7_days': get_count_item_for_last_days(trailcam_id),
        'request': request
    }
Ejemplo n.º 10
0
def _query_scalar_bytes(query):
    v = DBSession.query(query).scalar()
    return v.tobytes()
Ejemplo n.º 11
0
def pull_messages_from_email(trailcam):
    email_connection = trailcam.email_connection

    current_request = pyramid.threadlocal.get_current_request()

    request_json_data = {
        'ngw': {
            'unique_id': get_ngw_unique_id(current_request),
            'name': get_ngw_instance_name(current_request),
            'url': current_request.application_url
        },
        'email': {
            'address': email_connection.email
        },
        'messages': {
            'regex': trailcam.filter
        }
    }

    trailcam_email_service_url = get_trailcam_service_endpoint_url(
        current_request, 'messages')
    response = requests.get(trailcam_email_service_url, json=request_json_data)

    status_code = response.status_code
    if status_code == 500:
        raise ValidationError(
            _('The email is not registered on NextGIS Email System. Try later, please.'
              ))
    elif status_code != 200:
        raise ValidationError(
            _('NextGIS Email System is unavailable. Try later, please.'))

    messages_info = response.json()

    from nextgisweb_trailcam.model import Trailcam, TrailcamItem
    session = DBSession()

    trailcam = session.query(Trailcam).get(trailcam.id)
    for message_info in messages_info:
        new_trailcam_item = TrailcamItem()
        email_message_info = message_info['message']
        new_trailcam_item.email_uid = email_message_info['uid']
        new_trailcam_item.date_received = dateutil.parser.parse(
            email_message_info['received'])
        new_trailcam_item.name = email_message_info['subject']
        new_trailcam_item.message_body = email_message_info['body']

        new_trailcam_item.file_name = email_message_info['image_name']
        image_file_info = write_image(
            current_request, unicode.encode(email_message_info['image']),
            new_trailcam_item.file_name)

        new_trailcam_item.file_path = image_file_info['file_path']
        new_trailcam_item.file_path_thumbnail = image_file_info[
            'file_path_thumbnail']
        new_trailcam_item.file_size = image_file_info['file_size']

        extra = message_info['extra']
        if 'month' in extra and 'day' in extra and 'hour' in extra and 'minutes' in extra:
            datetime_original = datetime.datetime(2017, int(extra['month']),
                                                  int(extra['day']),
                                                  int(extra['hour']),
                                                  int(extra['minutes']))
        new_trailcam_item.date_original = datetime_original

        new_trailcam_item.trailcam = trailcam
        session.add(new_trailcam_item)

    transaction.commit()
    session.close()

    return {'count': len(messages_info)}