Пример #1
0
    def post(self, request, format=None):
        serializer = RecordSerializer(data=request.data,
                                      context={'request': request})

        if serializer.is_valid():
            # 如果是新网址,会添加一条record
            # 如果以前添加过,则不会添加
            # 所以保证了之后old_url存在
            serializer.save()

            # 通过old_url获取record
            # 不用id获取的原因是,如果条目之前已经存在,serializer.save()中会直接跳出,而获取不到id
            old_url = delete_http(request.data['old_url'])
            record = Record.objects.get(old_url=old_url)

            # 当前域名
            domain = get_current_site(request).domain

            # 当前域名与tiny_url结合
            base = 'http://{0!s}/'.format(domain)
            record.tiny_url = urljoin(base, record.tiny_url)

            record.old_url = old_url
            # 放入返回结果的serializer中
            tiny_url_serializer = TinyUrlSerializer(record)

            return Response(tiny_url_serializer.data,
                            status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
 def retrieve(self, request, pk=None):
     try:
         record = Record.objects.get(api_id=pk)
     except Record.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
     serializer = RecordSerializer(record)
     return Response(serializer.data)
Пример #3
0
 def users_record(self, request):
     user_id = request.data.get('employee_id')
     if Employee.objects.filter(id=user_id).exists():
         record = Employee.objects.get(id=user_id).records
         ser = RecordSerializer(record, many=True)
         return Response({'data': ser.data}, status=status.HTTP_200_OK)
     return Response({'data': "Employee with this id doesnot exist"},
                     status=status.HTTP_404_NOT_FOUND)
Пример #4
0
 def get(self, request, format=None):
     try:
         if request.auth == None:
             raise PermissionDenied
         target = Record.objects.get(id=1)
         serializer = RecordSerializer(target, many=False)
         res = {'recording': serializer.data['recording']}
         return Response(res, status=status.HTTP_200_OK)
     except PermissionDenied as error:
         return Response(
             {
                 'error': "PermissionDenied",
                 'date': datetime.now()
             },
             status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
 def list(self, request):
     queryset = self.get_queryset()
     serializer = RecordSerializer(queryset, many=True)
     return Response(serializer.data)
def record() :
    path = '/home/pi/recorded'  # save path

    target = Record.objects.get(id = 1)
    serializer = RecordSerializer(target, many = False)
    state = serializer.data['recording']
    # rpi setting
    GPIO.setmode(GPIO.BCM)
    pir_pin = 7
    GPIO.setup(pir_pin, GPIO.IN)
    camera = PiCamera()

    try:
        while state :
            target = Record.objects.get(id = 1)
            serializer = RecordSerializer(target, many = False)
            state = serializer.data['recording']
            
            if GPIO.input(pir_pin):  # motion detected
                print("motion detected")
                # take a video
                camera.resolution = [320, 240]
                camera.start_preview()

                now = datetime.datetime.now()
                start_time = time.time()

                vid_name = now.strftime('%Y%m%d-%H%M%S')
                vid_path = path + '/' + vid_name
                h264_vid_path = vid_path + '.h264'
                mp4_vid_path = vid_path + '.mp4'
                thumbnail_path = path + '/' + vid_name + '.jpg'

                camera.start_recording(output=h264_vid_path)
                time.sleep(1)
                camera.capture(thumbnail_path)
                while GPIO.input(pir_pin) :
                    print("recoring..")
                    time.sleep(2)
                camera.stop_recording()
                camera.stop_preview()
                result = subprocess.check_output("MP4Box -add {} {}".format(h264_vid_path, mp4_vid_path), shell=True)
                print(result)
            
                vid_time = time.strftime("%M:%S", time.gmtime(time.time()-start_time))

                # s3 upload
                s3 = boto3.client('s3', region_name = 'ap-northeast-2', aws_access_key_id=S3_ACCESS_KEY_ID, aws_secret_access_key=S3_SECRET_ACCESS_KEY)
                s3.upload_file(Filename = mp4_vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '.mp4')
                s3.upload_file(Filename = thumbnail_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '_thumb.jpg')

                uploadVideo = {}
                uploadVideo['vid_name'] = vid_name
                uploadVideo['created'] = now
                uploadVideo['vid_time'] = vid_time
                uploadVideo['thumb'] = S3_ACCESS_URL + vid_name + '_thumb.jpg'
                serializer = VideoSerializer(data = uploadVideo)
                serializer.is_valid()
                serializer.save()
                print(vid_path, "upload success")
                os.remove(h264_vid_path)
                os.remove(mp4_vid_path)
                os.remove(thumbnail_path)
            else:
                camera.stop_preview()
    except KeyboardInterrupt:
        print("quit")
        GPIO.cleanup()
Пример #7
0
 def get(self, request, format=None):
     record = Record.objects.all()
     serializer = RecordSerializer(record, many=True)
     return Response(serializer.data)