models.py
1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django.db import models
from django.db.models import JSONField, DateField
import numpy as np
# Create your models here.
import json
from json import JSONEncoder
import numpy
class NumpyArrayEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, numpy.ndarray):
return obj.tolist()
return JSONEncoder.default(self, obj)
class EffectiveSchedule(models.Model):
scheduler_matrix = JSONField(blank=True, null=True)
def save(self, *args, **kwargs):
# Transform numpy matrix to JSON
scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder)
self.scheduler_matrix = scheduler_matrix_as_json
super(EffectiveSchedule, self).save(*args, **kwargs)
def get(self, *args, **kwargs):
# Transform JSON to numpy matrix
matrix_as_json = json.loads(self.scheduler_matrix)
self.scheduler_matrix = numpy.asarray(matrix_as_json["array"])
return super().get(**kwargs)
class PredictiveSchedule(models.Model):
scheduler_matrix = JSONField(blank=True, null=True)
def save(self, *args, **kwargs):
# Transform numpy matrix to JSON
scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder)
self.scheduler_matrix = scheduler_matrix_as_json
super(PredictiveSchedule, self).save(*args, **kwargs)
def get(self, *args, **kwargs):
# Transform JSON to numpy matrix
matrix_as_json = json.loads(self.scheduler_matrix)
self.scheduler_matrix = numpy.asarray(matrix_as_json["array"])
return super().get(**kwargs)
class SchedulerHistory(EffectiveSchedule):
night_datetime = models.DateTimeField(blank=True, null=True)