-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
157 lines (114 loc) · 3.66 KB
/
models.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
from django import forms
from django.db import models
from django.core import serializers
from escapegame import libraspi
from escapegame.models import *
from controllers.models import *
from multimedia.models import *
from collections import OrderedDict
from datetime import datetime
import traceback
import json
model_mapping = [
('images', Image),
('escapegames', EscapeGame),
('media_files', MultimediaFile),
('cubes', EscapeGameCube),
('controllers', Controller),
('raspberry_pis', RaspberryPi),
('GPIOs', GPIO),
('liftGPIOs', LiftGPIO),
('doorGPIOs', DoorGPIO),
('rooms', EscapeGameRoom),
('challengeGPIOs', ChallengeGPIO),
('challenges', EscapeGameChallenge),
]
#
# Models
#
class JsonImport(models.Model):
class Meta:
verbose_name = 'JSON Import'
verbose_name_plural = 'JSON Import'
json_configuration = models.FileField()
def save(self, *args, **kwargs):
pass
@staticmethod
def load_list(model, listdic, game_controllers):
try:
dics = json.dumps(listdic)
objects = serializers.deserialize('json', dics)
for obj in objects:
# If we are desrializing an EscapeGame instance save the controller id to
# restore it later and delete it from instance to avoid circular import issues
if hasattr(obj, 'object') and hasattr(obj.object, 'controller_id') and type(obj.object) is EscapeGame:
game_controllers[obj.object.pk] = obj.object.controller_id
obj.object.controller_id = None
obj.save()
return 0, 'Success', game_controllers
except Exception as err:
return 1, 'Error: %s' % traceback.format_exc(), None
@staticmethod
def load(json_configuration):
game_controllers = {}
try:
databytes = bytearray()
for chunk in json_configuration.chunks():
databytes += chunk
status, message = 0, 'Success'
config = json.loads(databytes.decode('utf-8').strip())
for jsonkey, model in model_mapping:
if jsonkey in config:
status, message, game_controllers = JsonImport.load_list(model, config[jsonkey], game_controllers)
if status != 0:
return status, message
# Restore game controllers (see: JsonImport.load_list())
for game_id in game_controllers:
game = EscapeGame.objects.get(pk=game_id)
raspi_id = game_controllers[game_id]
raspi = RaspberryPi.objects.get(pk=raspi_id)
game.controller = raspi
game.save(update_fields=['controller'])
return status, message
except Exception as err:
return 1, 'Error: %s' % traceback.format_exc()
class JsonExport(models.Model):
class Meta:
verbose_name = 'JSON Export'
verbose_name_plural = 'JSON Export'
indent = models.BooleanField(default=True)
export_date = models.BooleanField(default=True)
software_version = models.BooleanField(default=True)
def save(self, *args, **kwargs):
pass
@staticmethod
def dump(post):
now = datetime.now()
config = OrderedDict()
# Check if we should beautify JSON
config['indent'] = False
if post.get('indent'):
config['indent'] = True
# Export the date of current export
if post.get('export_date'):
config['export_date'] = now.strftime('%Y-%m-%d %H:%M:%S')
# Export the software version
if post.get('software_version'):
config['software_version'] = libraspi.git_version()[:8]
# Export the filename
config['filename'] = 'escapegame-config-%s.json' % now.strftime('%Y-%m-%d_%H-%M-%S')
for jsonkey, model in model_mapping:
config[jsonkey] = json.loads(serializers.serialize('json', model.objects.all(), ensure_ascii=False))
return config
#
# Forms
#
class JsonImportForm(forms.ModelForm):
class Meta:
model = JsonImport
exclude = []
class JsonExportForm(forms.ModelForm):
class Meta:
model = JsonExport
exclude = []