예제 #1
0
 def process_request(self, request):
     params = dict(
         is_view_public=True,
         is_view_enabled=True,
     )
     private_tags = [i.id for i in CameraTag.objects(is_private=True)]
     params.update(dict(tags__not__in=private_tags))
     request.places = paginate(
         request,
         Camera.objects(**params).order_by('-view_count'),
         Camera.objects(**params).count(), 10,
         reverse('cam:place_update', args=['time', 'asc']))
     request.places_all_count = Camera.objects.count()
예제 #2
0
 def process_request(self, request):
     params = dict(
         is_view_public=True,
         is_view_enabled=True,
     )
     private_tags = [i.id for i in CameraTag.objects(is_private=True)]
     params.update(dict(tags__not__in=private_tags))
     request.places = paginate(request,
                               Camera.objects(**params).order_by('-view_count'),
                               Camera.objects(**params).count(),
                               10,
                               reverse('cam:place_update', args=['time', 'asc']))
     request.places_all_count = Camera.objects.count()
예제 #3
0
def cam_form(request, user, cam):
    if cam:
        initial = cam._data
        initial['type'] = cam.type.get_option_value()
        initial['operator'] = initial['operator'] and initial['operator'].id
        initial['tags'] = initial['tags'] and [i.id for i in initial['tags']]
        for tariff_type in Camera.TARIFF_FIELDS:
            value = getattr(cam, tariff_type)
            if value:
                initial[tariff_type] = value.id
    else:
        initial = {}

    if request.POST and request.POST.get('form', None) == 'cam':
        form = CameraForm(user, request.POST, initial=initial)
        if form.is_valid():
            if not cam:
                cam = Camera()
                cam.owner = user
                old_tag_ids = []
            elif form.cleaned_data['tags']:
                old_tag_ids = map(str, cam.tags)

            for k, v in form.cleaned_data.items():
                setattr(cam, k, v)

            cam.type = CameraType.objects.get(
                id=form.cleaned_data['type'][:-2])
            cam.operator = form.cleaned_data['operator'] and User.objects(
                id=form.cleaned_data['operator']).first() or None
            if form.cleaned_data['tags']:
                new_tag_ids = map(str, form.cleaned_data['tags'])
                CameraTag.calc_count(new_tag_ids, old_tag_ids)
                cam.tags = CameraTag.objects(id__in=form.cleaned_data['tags'])

            for tariff_type in Camera.TARIFF_FIELDS:
                value = form.cleaned_data[tariff_type]
                if value:
                    value = Tariff.objects.get(id=value)
                    assert value in getattr(Tariff,
                                            'get_%s_list' % tariff_type)()
                    setattr(cam, tariff_type, value)
                else:
                    setattr(cam, tariff_type, None)

            cam.save()
            messages.add_message(request, messages.SUCCESS,
                                 _('Camera successfully updated'))
            return
    else:
        form = CameraForm(user, None, initial=initial)

    return dict(cam_form=form, camera=cam)
예제 #4
0
def cam_form(request, user, cam):
    if cam:
        initial = cam._data
        initial["type"] = cam.type.get_option_value()
        initial["operator"] = initial["operator"] and initial["operator"].id
        initial["tags"] = initial["tags"] and [i.id for i in initial["tags"]]
        for tariff_type in Camera.TARIFF_FIELDS:
            value = getattr(cam, tariff_type)
            if value:
                initial[tariff_type] = value.id
    else:
        initial = {}

    if request.POST and request.POST.get("form", None) == "cam":
        form = CameraForm(user, request.POST, initial=initial)
        if form.is_valid():
            if not cam:
                cam = Camera()
                cam.owner = user
                old_tag_ids = []
            elif form.cleaned_data["tags"]:
                old_tag_ids = map(str, cam.tags)

            for k, v in form.cleaned_data.items():
                setattr(cam, k, v)

            cam.type = CameraType.objects.get(id=form.cleaned_data["type"][:-2])
            cam.operator = (
                form.cleaned_data["operator"] and User.objects(id=form.cleaned_data["operator"]).first() or None
            )
            if form.cleaned_data["tags"]:
                new_tag_ids = map(str, form.cleaned_data["tags"])
                CameraTag.calc_count(new_tag_ids, old_tag_ids)
                cam.tags = CameraTag.objects(id__in=form.cleaned_data["tags"])

            for tariff_type in Camera.TARIFF_FIELDS:
                value = form.cleaned_data[tariff_type]
                if value:
                    value = Tariff.objects.get(id=value)
                    assert value in getattr(Tariff, "get_%s_list" % tariff_type)()
                    setattr(cam, tariff_type, value)
                else:
                    setattr(cam, tariff_type, None)

            cam.save()
            messages.add_message(request, messages.SUCCESS, _("Camera successfully updated"))
            return
    else:
        form = CameraForm(user, None, initial=initial)

    return dict(cam_form=form, camera=cam)
예제 #5
0
    def handle(self, *args, **options):
        try:
            (driver, host, username, password) = args
        except:
            raise

        import curses
        type = CameraType(name=driver, driver='apps.cam.drivers.%s' % driver)
        camera = Camera(type=type,
                        host=host,
                        username=username,
                        password=password)
        control = camera.driver.control
        stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        stdscr.keypad(1)
        try:
            while 1:
                c = stdscr.getch()
                if c == curses.KEY_UP:
                    control.move_up()
                elif c == curses.KEY_DOWN:
                    control.move_down()
                elif c == curses.KEY_LEFT:
                    control.move_left()
                elif c == curses.KEY_RIGHT:
                    control.move_right()
                elif c == curses.KEY_NPAGE:
                    control.zoom_in()
                elif c == curses.KEY_PPAGE:
                    control.zoom_out()
                elif c == curses.KEY_HOME:
                    control.home()
                elif c == curses.KEY_END:
                    control.reset()

        except:
            pass

        curses.nocbreak()
        stdscr.keypad(0)
        curses.echo()
예제 #6
0
def cam_view_notify(request, format):
    """ API:
        ?session_key=<Fx24>&camera_id=<Fx24>&status=connect
        ?session_key=<Fx24>&camera_id=<Fx24>&status=next&time=<sec>
        ?session_key=<Fx24>&camera_id=<Fx24>&status=disconnect&time=<sec>

        0 OK

        -1 BAD PARAMS
        -500 INTERNAL ERROR
    """

    from apps.media.documents import File

    request_id = time()

    def log_debug(text):
        info = '\n'.join([
            "session_key=%s" % session_key,
            "user_id=%s" % user.id if user else None,
            "camera_id=%s" % camera_id,
        ])
        logger.debug('cam_view_notify %s\n%s\n%s' % (request_id, info, text))

    logger.debug('cam_view_notify %s\nrequest %s' %
                 (request_id, repr(request.GET.items())))

    user = None
    session_key = None
    camera_id = None
    try:
        if not request.GET:
            raise CameraAccessor.APIException("Bad params")
        status = request.GET.get('status', None)
        session_key = request.GET.get('session_key', None)
        camera_id = request.GET.get('camera_id', None)
        if not (status and session_key and camera_id):
            raise CameraAccessor.APIException("Bad params")
        extra_time = request.GET.get('time', None)
        if extra_time is not None:
            if not extra_time.isdigit():
                raise CameraAccessor.APIException("Time must by a digit")
            extra_time = int(extra_time)
            if extra_time > settings.TIME_INTERVAL_NOTIFY or extra_time < 0:
                raise CameraAccessor.APIException("Bad time interval")

        # extract data from session
        engine = import_module(settings.SESSION_ENGINE)
        session = engine.SessionStore(session_key)
        user_id = session.get(SESSION_KEY, None)
        if not user_id:
            raise CameraAccessor.APIException("Need user_id")
        user = User.objects(id=user_id).first()
        if not user:
            raise CameraAccessor.APIException("Bad user_id")
        camera = Camera.objects(id=camera_id).first()
        if not camera:
            raise CameraAccessor.APIException(
                "Can`t found camera by camera_id")

        result = CameraAccessor(status, user, camera, session, extra_time)
    except CameraAccessor.APIException, e:
        params = (-1, 0, 0)
        log_debug('api error\n%s\n%s' % (e, traceback.format_exc()))
예제 #7
0
 def get_camera(self):
     from apps.cam.documents import Camera
     return Camera.objects(owner=self).first()
예제 #8
0
    def setUp(self):
        self.tearDown()

        self.client = Client()

        self.camera_type_axis = CameraType(
            name='Axis',
            driver='axis.AxisDriver',
            is_controlled=False,
        )
        self.camera_type_axis.save()
        self.camera_type_axis_manage = CameraType(
            name='Axis Manage',
            driver='axis.AxisDriver',
            is_controlled=True,
        )
        self.camera_type_axis_manage.save()

        self.tariff_view_package = Tariff(
            name='View Package 1 min',
            description='View Package Description...',
            cost=1.0,
            duration=2,
            is_controlled=False,
        )
        self.tariff_view_package.save()

        self.tariff_view_time = Tariff(
            name='View Package Time',
            description='View Package Description...',
            cost=1.0,
            is_controlled=False,
        )
        self.tariff_view_time.save()

        self.tariff_manage_package = Tariff(
            name='Manage Package 1 min',
            description='Manage Package Description...',
            cost=2.0,
            duration=2,
            is_controlled=True,
        )
        self.tariff_manage_package.save()

        self.tariff_manage_time = Tariff(
            name='Manage Package Time',
            description='Manage Package Description...',
            cost=2.0,
            is_controlled=True,
        )
        self.tariff_manage_time.save()

        for i in xrange(1, 4):
            user = User.create_user(email='*****@*****.**' % i, password='******')
            user.cash = 1000000.0
            user.save()
            camera = Camera(
                name='Test Billing Camera %i' % i,
                owner=user,
                type=self.camera_type_axis_manage,
                stream_name='boston',
                camera_management_host='localhost.boston.com',
                camera_management_username='******',
                camera_management_password='******',
                is_view_enabled=True,
                is_view_public=True,
                is_view_paid=True,
                is_management_enabled=True,
                is_management_public=True,
                is_management_paid=True,
                is_managed=True,
                management_packet_tariff=self.tariff_manage_package,
                management_time_tariff=self.tariff_manage_time,
                view_packet_tariff=self.tariff_view_package,
                view_time_tariff=self.tariff_view_time,
            )
            camera.save()
예제 #9
0
def cam_view_notify(request, format):
    """ API:
        ?session_key=<Fx24>&camera_id=<Fx24>&status=connect
        ?session_key=<Fx24>&camera_id=<Fx24>&status=next&time=<sec>
        ?session_key=<Fx24>&camera_id=<Fx24>&status=disconnect&time=<sec>

        0 OK

        -1 BAD PARAMS
        -500 INTERNAL ERROR
    """

    from apps.media.documents import File

    request_id = time()
    def log_debug(text):
        info = '\n'.join([
            "session_key=%s" % session_key,
            "user_id=%s" % (user.id if user else None),
            "camera_id=%s" % camera_id,
        ])
        logger.debug('cam_view_notify %s\n%s\n%s' % (request_id, info, text))

    logger.debug('cam_view_notify %s\nrequest %s' % (request_id, repr(request.GET.items())))

    user = None
    session_key = None
    camera_id = None
    try:
        if not request.GET:
            raise CameraAccessor.APIException("Bad params")
        status = request.GET.get('status', None)
        session_key = request.GET.get('session_key', None)
        camera_id = request.GET.get('camera_id', None)
        if not (status and session_key and camera_id):
            raise CameraAccessor.APIException("Bad params")
        extra_time = request.GET.get('time', None)
        if extra_time is not None:
            if not extra_time.isdigit():
                raise CameraAccessor.APIException("Time must by a digit")
            extra_time = int(extra_time)
            if extra_time > settings.TIME_INTERVAL_NOTIFY or extra_time < 0:
                raise CameraAccessor.APIException("Bad time interval")

        # extract data from session
        engine = import_module(settings.SESSION_ENGINE)
        session = engine.SessionStore(session_key)
        user_id = session.get(SESSION_KEY, None)
        if not user_id:
            raise CameraAccessor.APIException("Need user_id")
        user = User.objects(id=user_id).first()
        if not user:
            raise CameraAccessor.APIException("Bad user_id")
        camera = Camera.objects(id=camera_id).first()
        if not camera:
            raise CameraAccessor.APIException("Can`t found camera by camera_id")

        result = CameraAccessor(status, user, camera, session, extra_time)
    except CameraAccessor.APIException, e:
        params = (-1, 0, 0)
        log_debug('api error\n%s\n%s' % (e, traceback.format_exc()))
예제 #10
0
 def get_camera(self):
     from apps.cam.documents import Camera
     return Camera.objects(owner=self).first()
예제 #11
0
 def handle(self, *args, **options):
     for tag in CameraTag.objects:
         tag.count = Camera.objects(tags=tag).count()
         tag.save()