Django Library PyGal
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

convert_old_userdata.py 3.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from django.core.management.base import BaseCommand
  2. import fstools
  3. import json
  4. from pygal.models import is_valid_area, Item
  5. from ._private import KEY_USERDATA_VERSION
  6. from ._private import KEY_USERDATA_FAVOURITE
  7. from ._private import KEY_USERDATA_TAGS
  8. from ._private import KEY_USERDATA_UPLOAD
  9. KEY_REL_PATH = '_rel_path_'
  10. KEY_FAVOURITE = '_favourite_of_'
  11. KEY_TAG = '_tags_'
  12. KEY_UPLOAD = '_upload_'
  13. class Command(BaseCommand):
  14. help = 'Convert userdata from old pygal to JSON-Format.'
  15. def add_arguments(self, parser):
  16. parser.add_argument('folder', nargs='+', type=str)
  17. def handle(self, *args, **options):
  18. data = {}
  19. data[KEY_USERDATA_VERSION] = 1
  20. path = options['folder'][0]
  21. for f in fstools.filelist(path, '*.json'):
  22. with open(f, 'r') as fh:
  23. o = json.load(fh)
  24. _rel_path_ = o.get(KEY_REL_PATH)
  25. _favourite_of_ = o.get(KEY_FAVOURITE)
  26. _tags_ = o.get(KEY_TAG)
  27. _upload_ = o.get(KEY_UPLOAD)
  28. if _rel_path_:
  29. try:
  30. i = Item.objects.get(rel_path=_rel_path_)
  31. except Item.DoesNotExist:
  32. self.stderr.write(self.style.ERROR('%s does not exist. No data taken over!' % _rel_path_))
  33. else:
  34. if _favourite_of_:
  35. for username in _favourite_of_:
  36. if KEY_USERDATA_FAVOURITE not in data:
  37. data[KEY_USERDATA_FAVOURITE] = {}
  38. if username not in data[KEY_USERDATA_FAVOURITE]:
  39. data[KEY_USERDATA_FAVOURITE][username] = []
  40. data[KEY_USERDATA_FAVOURITE][username].append(_rel_path_)
  41. if len(_tags_):
  42. new_tags = []
  43. for t in _tags_:
  44. new_tag = {}
  45. new_tag['text'] = _tags_[t].get('tag')
  46. #
  47. x = _tags_[t].get('x')
  48. y = _tags_[t].get('y')
  49. w = _tags_[t].get('w')
  50. h = _tags_[t].get('h')
  51. if x is not None and y is not None and w is not None and h is not None:
  52. width = i.item_data.width
  53. height = i.item_data.height
  54. x1 = int(x * width)
  55. y1 = int(y * height)
  56. x2 = x1 + int(w * width)
  57. y2 = y1 + int(h * height)
  58. if is_valid_area(x1, y1, x2, y2):
  59. new_tag['topleft_x'] = x1
  60. new_tag['topleft_y'] = y1
  61. new_tag['bottomright_x'] = x2
  62. new_tag['bottomright_y'] = y2
  63. new_tags.append(new_tag)
  64. else:
  65. self.stderr.write(self.style.ERROR('Coordinates are not valid for %s. Not transferred!' % _rel_path_))
  66. else:
  67. new_tags.append(new_tag)
  68. if len(new_tags) > 0:
  69. if KEY_USERDATA_TAGS not in data:
  70. data[KEY_USERDATA_TAGS] = {}
  71. data[KEY_USERDATA_TAGS][_rel_path_] = new_tags
  72. if len(_upload_):
  73. if KEY_USERDATA_UPLOAD not in data:
  74. data[KEY_USERDATA_UPLOAD] = {}
  75. data[KEY_USERDATA_UPLOAD][_rel_path_] = _upload_
  76. self.stdout.write(json.dumps(data, indent=4, sort_keys=True))