def post_handler(station_id, data_type, value, timestamp): s = Station.objects.get(id=station_id) r = Record(Station=s, data_type=data_type, value=value, timestamp=timestamp) r.save()
def put(self, *args, **kwargs): serializer = UserConfigSerializer(data=self.request.data) serializer.is_valid(raise_exception=True) self.request.user.userconfig.expected_calories_per_day = serializer.data[ 'expected_calories_per_day'] self.request.user.userconfig.save() affected_records = Record.objects.filter(user=self.request.user) Record.recalculate_exceed_flag(affected_records) return Response({'success': True})
def __process_record(cls, path): """Read, extract and store a single Dreem records data.""" try: record = h5py.File(path, 'r') except OSError: # The file is still copying. It will be considered again on next poll. return # These two attributes are stored as ASCII strings. username = str(record.attrs["user"], "utf-8") devicename = str(record.attrs["device"], "utf-8") # Retrieving the user try: user = User.objects.get(username=username) except ObjectDoesNotExist: print("Unknown user, skipping the record…") path.unlink() return # Retrieving the device try: device = Device.objects.get(name=devicename) except Device.DoesNotExist: # Temporary: we create the device print("Unknown device, inserting…") device = Device(name=devicename, dreempy_version=record.attrs["dreempy_version"]) device.save() record = Record( start_time=datetime.fromtimestamp(int(record.attrs["start_time"]), timezone(timedelta(hours=1))), stop_time=datetime.fromtimestamp(int(record.attrs["stop_time"]), timezone(timedelta(hours=1))), sleep_start_time=datetime.fromtimestamp( int(record.attrs["sleep_start_time"]), timezone(timedelta(hours=1))), sleep_stop_time=datetime.fromtimestamp( int(record.attrs["sleep_stop_time"]), timezone(timedelta(hours=1))), sleep_score=record.attrs["sleep_score"], number_of_stimulations=int(record.attrs["number_of_stimulations"]), number_of_wake=int(record.attrs["number_of_wake"]), hypnogram=JSONRenderer().render( record["reporting"]["dreemnogram"][()]), user=user, device=device) record.save() path.unlink()
def test_get_signal(self): """Send a GET on the /signal/ resource and ensure the proper record is provided.""" # The record will start yesterday at this exact same hour. ts_start = int(time()) - 60 * 60 * 24 # It will last from 6 to 10 hours. ts_stop = ts_start + 60 * 60 * randint(6, 10) record = Record( start_time=datetime.fromtimestamp(ts_start, timezone(timedelta(hours=1))), stop_time=datetime.fromtimestamp(ts_stop, timezone(timedelta(hours=1))), sleep_start_time=datetime.fromtimestamp( ts_start + 60 * 60, timezone(timedelta(hours=1))), sleep_stop_time=datetime.fromtimestamp( ts_stop - 60 * 60, timezone(timedelta(hours=1))), sleep_score=random(), number_of_stimulations=randint(0, 2000), number_of_wake=randint(0, 50), hypnogram= "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,3,3,3]", user=self.__user, device=self.__device) record.save() response = self.__client.get("/signal/{}/{}/{}/".format( self.__user.username, ts_start, ts_stop), HTTP_AUTHORIZATION=self.__basic_digest) records = json.loads(response.content.decode('utf-8')) self.assertEqual(response.status_code, 200, "Can not retrieve my signal.") self.assertEqual(len(records), 1, "I should have add a single record.") self.assertGreaterEqual( parse(records[0]["start_time"]), datetime.fromtimestamp(ts_start, timezone(timedelta(hours=1))), "The start_time is wrong.") self.assertGreaterEqual( datetime.fromtimestamp(ts_stop, timezone(timedelta(hours=1))), parse(records[0]["stop_time"]), "The stop_time is wrong.")
def post(request): data = JSONParser().parse(request) curtime = datetime.now() try: for beacon in data: r = Record() r.major = beacon['major'] r.minor = beacon['minor'] r.mac = beacon['mac'] r.rssi = beacon['rssi'] r.distance = beacon['distance'] r.user = request.user r.timestamp = curtime r.save() return JSONResponse({"success": "true"}, status=201) except Exception as e: return JSONResponse({"success": "false"}, status=400)
def recalculate_exceed_flag(self, date): affected_records = Record.objects.filter(user=self.request.user, date=date) Record.recalculate_exceed_flag(affected_records)
def insert_data(self): Record(athlete=self.__athlete, results=self.results).save()