| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | |
| 19 | from django.db import models |
| 20 | from django.utils import timezone |
| 21 | import jsonfield |
| 22 | from StringIO import StringIO |
| 23 | import zipfile |
| 24 | import json |
| 25 | import yaml |
| 26 | from lib.util import Util |
| 27 | from model_utils.managers import InheritanceManager |
| 28 | import logging |
| 29 | import os, shutil |
| 30 | |
| 31 | logging.basicConfig(level=logging.DEBUG) |
| 32 | log = logging.getLogger('models.py') |
| 33 | |
| 34 | project_types = {} |
| 35 | |
| 36 | |
| 37 | class Project(models.Model): |
| 38 | """ Base class for project types |
| 39 | |
| 40 | data_project stores a validated JSON representation of the project |
| 41 | get_dataproject() method returns the python dict representation of the project |
| 42 | |
| 43 | |
| 44 | """ |
| lombardofr | 84d0a01 | 2018-06-12 11:21:02 +0200 | [diff] [blame] | 45 | owner = models.ForeignKey('authosm.OsmUser', db_column='owner') |
| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 46 | name = models.CharField(max_length=20, default='') |
| 47 | created_date = models.DateTimeField(default=timezone.now) |
| 48 | updated_date = models.DateTimeField(default=timezone.now, blank=True, null=True) |
| 49 | info = models.TextField(default='No info') |
| 50 | data_project = jsonfield.JSONField(default={}) |
| 51 | """Stores a validated JSON representation of the project""" |
| 52 | |
| 53 | validated = models.BooleanField(default=False) |
| 54 | |
| 55 | #InheritanceManager |
| 56 | objects = InheritanceManager() |
| 57 | |
| 58 | |
| 59 | @classmethod |
| 60 | def get_project_types(cls): |
| 61 | global project_types |
| 62 | return project_types |
| 63 | |
| 64 | @classmethod |
| 65 | def add_project_type(cls, type, my_class): |
| 66 | global project_types |
| 67 | project_types [type]= my_class |
| 68 | |
| 69 | |
| 70 | @classmethod |
| 71 | def create_project(cls, name, user, validated, info, data_project): |
| 72 | project = cls.objects.create(name=name, owner=user, validated=False, info=info, |
| 73 | data_project=data_project) |
| 74 | return project |
| 75 | |
| 76 | @classmethod |
| 77 | def get_graph_model(cls, file_path): |
| 78 | """Returns the model of the graph of the project type as a yaml object |
| 79 | |
| 80 | Returns an empty dict if there is no file with the model |
| 81 | """ |
| 82 | # file_path = GRAPH_MODEL_FULL_NAME |
| 83 | graph_model = {} |
| 84 | try: |
| 85 | graph_model = Util.loadyamlfile(file_path) |
| 86 | except Exception as e: |
| 87 | log.exception(e) |
| 88 | pass |
| 89 | return graph_model |
| 90 | |
| 91 | def get_type(self): |
| 92 | return "Base" |
| 93 | |
| 94 | #@classmethod |
| 95 | def get_dataproject(self): |
| 96 | """ Return the python dict representation of the project data |
| 97 | |
| 98 | """ |
| 99 | #current_data = json.loads(self.data_project) |
| 100 | current_data = Util.json_loads_byteified(self.data_project) |
| 101 | |
| 102 | return current_data |
| 103 | |
| 104 | #@classmethod |
| 105 | def get_overview_data(self): |
| 106 | result = { |
| 107 | 'owner': self.owner, |
| 108 | 'name': self.name, |
| 109 | 'updated_date': self.updated_date, |
| 110 | 'info': self.info, |
| 111 | 'validated': self.validated |
| 112 | } |
| 113 | |
| 114 | return result |
| 115 | |
| 116 | def set_data_project(self, new_data, validated): |
| 117 | self.data_project = new_data |
| 118 | self.set_validated(validated) |
| 119 | self.update() |
| 120 | |
| 121 | def update(self): |
| 122 | self.updated_date = timezone.now() |
| 123 | self.save() |
| 124 | |
| 125 | def __str__(self): |
| 126 | return self.name |
| 127 | |
| 128 | def edit_graph_positions(self, positions): |
| 129 | # print positions |
| 130 | try: |
| 131 | current_data = json.loads(self.data_project) |
| 132 | if 'positions' not in current_data: |
| 133 | current_data['positions'] = {} |
| 134 | if 'vertices' not in current_data['positions']: |
| 135 | current_data['positions']['vertices'] = {} |
| 136 | if 'vertices' in positions: |
| 137 | current_data['positions']['vertices'].update(positions['vertices']) |
| 138 | self.data_project = current_data |
| 139 | self.update() |
| 140 | result = True |
| 141 | except Exception as e: |
| 142 | log.debug(e) |
| 143 | result = False |
| 144 | return result |
| 145 | |
| 146 | def get_descriptors(self, type_descriptor): |
| 147 | """Returns all descriptors of a given type""" |
| 148 | |
| 149 | try: |
| 150 | current_data = json.loads(self.data_project) |
| 151 | result = current_data[type_descriptor] |
| 152 | except Exception as e: |
| 153 | log.debug(e) |
| 154 | result = {} |
| 155 | return result |
| 156 | |
| 157 | def get_descriptor(self, descriptor_id, type_descriptor): |
| 158 | """Returns a specific descriptor""" |
| 159 | |
| 160 | try: |
| 161 | current_data = json.loads(self.data_project) |
| 162 | result = current_data[type_descriptor][descriptor_id] |
| 163 | print descriptor_id, type_descriptor, result |
| 164 | except Exception as e: |
| 165 | log.debug(e) |
| 166 | result = {} |
| 167 | |
| 168 | return result |
| 169 | |
| 170 | def delete_descriptor(self, type_descriptor, descriptor_id): |
| 171 | try: |
| 172 | log.debug('delete descriptor'+ descriptor_id + ' ' + type_descriptor) |
| 173 | current_data = json.loads(self.data_project) |
| 174 | del (current_data[type_descriptor][descriptor_id]) |
| 175 | self.data_project = current_data |
| 176 | self.update() |
| 177 | result = True |
| 178 | except Exception as e: |
| 179 | log.debug(e) |
| 180 | result = False |
| 181 | return result |
| 182 | |
| 183 | def clone_descriptor(self, type_descriptor, descriptor_id, new_id): |
| 184 | try: |
| 185 | current_data = json.loads(self.data_project) |
| 186 | descriptor = current_data[type_descriptor][descriptor_id] |
| 187 | new_descriptor = self.get_clone_descriptor(descriptor, type_descriptor, new_id) |
| 188 | current_data[type_descriptor][new_id] = new_descriptor |
| 189 | self.data_project = current_data |
| 190 | self.update() |
| 191 | result = True |
| 192 | except Exception as e: |
| 193 | log.debug(e) |
| 194 | result = False |
| 195 | return result |
| 196 | |
| 197 | def edit_descriptor(self, type_descriptor, descriptor_id, new_data, data_type): |
| 198 | try: |
| 199 | |
| 200 | ##FIXME questa parte va completamente rivista cosi' ha varie lacune |
| 201 | #log.info('editing ',+ descriptor_id + ' ' + type_descriptor + ' ' + data_type) |
| 202 | current_data = json.loads(self.data_project) |
| 203 | new_descriptor = new_data |
| 204 | if data_type == 'json': |
| 205 | new_descriptor = json.loads(new_data) |
| 206 | elif data_type == 'yaml': |
| 207 | yaml_object = yaml.load(new_data) |
| 208 | new_descriptor = json.loads(Util.yaml2json(yaml_object)) |
| 209 | if type_descriptor != 'click' and type_descriptor != 'oshi' and type_descriptor !='cran': |
| 210 | reference_schema = self.get_json_schema_by_type(type_descriptor) |
| 211 | Util.validate_json_schema(reference_schema, new_descriptor) |
| 212 | current_data[type_descriptor][descriptor_id] = new_descriptor |
| 213 | self.data_project = current_data |
| 214 | self.update() |
| 215 | result = True |
| 216 | except Exception as e: |
| 217 | log.debug(e) |
| 218 | result = False |
| 219 | return result |
| 220 | |
| 221 | def get_zip_archive(self): |
| 222 | in_memory = StringIO() |
| 223 | try: |
| 224 | current_data = json.loads(self.data_project) |
| 225 | zip = zipfile.ZipFile(in_memory, "w", zipfile.ZIP_DEFLATED) |
| 226 | for desc_type in current_data: |
| 227 | for current_desc in current_data[desc_type]: |
| 228 | zip.writestr(current_desc + '.json', json.dumps(current_data[desc_type][current_desc])) |
| 229 | |
| 230 | zip.close() |
| 231 | except Exception as e: |
| 232 | log.debug(e) |
| 233 | |
| 234 | in_memory.flush() |
| 235 | return in_memory |
| 236 | |
| 237 | def get_positions(self): |
| 238 | """Returns the positions of nodes""" |
| 239 | try: |
| 240 | current_data = json.loads(self.data_project) |
| 241 | positions = {} |
| 242 | if 'positions' in current_data: |
| 243 | positions = current_data['positions'] |
| 244 | except Exception as e: |
| 245 | log.debug(e) |
| 246 | |
| 247 | return positions |
| 248 | |
| 249 | def get_deployment_descriptor(self, **kwargs): |
| 250 | """Returns the deployment descriptor""" |
| 251 | raise NotImplementedError |
| 252 | |
| 253 | def get_node_overview(self, **kwargs): |
| 254 | """Returns the node overview""" |
| 255 | raise NotImplementedError |
| 256 | |
| 257 | def get_all_ns_descriptors(self, nsd_id): |
| 258 | raise NotImplementedError |
| 259 | |
| 260 | def translate_push_ns_on_repository(self, translator, nsd_id, repository, **kwargs): |
| 261 | raise NotImplementedError |
| 262 | |
| 263 | |
| 264 | class ProjectStateless(Project): |
| 265 | |
| 266 | def get_descriptors(self, type_descriptor): |
| 267 | """Returns all descriptors of a given type""" |
| 268 | raise NotImplementedError |
| 269 | |
| 270 | def delete_descriptor(self, type_descriptor, descriptor_id): |
| 271 | raise NotImplementedError |
| 272 | |
| 273 | def get_all_ns_descriptors(self, nsd_id): |
| 274 | pass |
| 275 | |
| 276 | def translate_push_ns_on_repository(self, translator, nsd_id, repository, **kwargs): |
| 277 | pass |
| 278 | |
| 279 | def get_deployment_descriptor(self, **kwargs): |
| 280 | pass |
| 281 | |
| 282 | def get_node_overview(self, **kwargs): |
| 283 | pass |
| 284 | |
| 285 | def get_dataproject(self): |
| 286 | raise NotImplementedError |
| 287 | |
| 288 | def get_overview_data(self): |
| 289 | raise NotImplementedError |