Django Library PyGal
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

models.py 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import datetime
  2. from django.contrib.auth.models import User
  3. from django.db import models
  4. from django.utils import formats, timezone
  5. from django.urls import reverse
  6. import fstools
  7. import json
  8. import logging
  9. import media
  10. import os
  11. import pygal
  12. import time
  13. DEBUG = False
  14. TYPE_FOLDER = 'folder'
  15. TYPE_IMAGE = 'image'
  16. TYPE_VIDEO = 'video'
  17. TYPE_AUDIO = 'audio'
  18. TYPE_OTHER = 'other'
  19. EXTENTIONS_IMAGE = ['.jpg', '.jpeg', '.jpe', '.png', '.tif', '.tiff', '.gif', ]
  20. EXTENTIONS_AUDIO = ['.mp3', ]
  21. EXTENTIONS_VIDEO = ['.avi', '.mpg', '.mpeg', '.mpe', '.mov', '.qt', '.mp4', '.webm', '.ogv', '.flv', '.3gp', ]
  22. # Get a logger instance
  23. try:
  24. from config import APP_NAME as ROOT_LOGGER_NAME
  25. except ImportError:
  26. ROOT_LOGGER_NAME = 'root'
  27. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  28. def get_item_type(full_path):
  29. if os.path.isdir(full_path):
  30. return TYPE_FOLDER
  31. else:
  32. if os.path.splitext(full_path)[1].lower() in EXTENTIONS_IMAGE:
  33. return TYPE_IMAGE
  34. elif os.path.splitext(full_path)[1].lower() in EXTENTIONS_VIDEO:
  35. return TYPE_VIDEO
  36. elif os.path.splitext(full_path)[1].lower() in EXTENTIONS_AUDIO:
  37. return TYPE_AUDIO
  38. return TYPE_OTHER
  39. def supported_types():
  40. rv = []
  41. if pygal.show_audio():
  42. rv.append(TYPE_AUDIO)
  43. if pygal.show_image():
  44. rv.append(TYPE_IMAGE)
  45. if pygal.show_other():
  46. rv.append(TYPE_OTHER)
  47. if pygal.show_video():
  48. rv.append(TYPE_VIDEO)
  49. return rv
  50. def get_item_by_rel_path(rel_path):
  51. try:
  52. rv = Item.objects.get(rel_path=rel_path)
  53. except Item.DoesNotExist:
  54. rv = None
  55. if rv is not None:
  56. # return the existing item
  57. return rv
  58. else:
  59. # create new item, if rel_path exists in filesystem (folders needs to hold files)
  60. full_path = pygal.get_full_path(rel_path)
  61. if os.path.exists(full_path):
  62. # file exists or folder has files in substructure
  63. if get_item_type(full_path) != TYPE_FOLDER or len(fstools.filelist(full_path)) > 0:
  64. i = Item(rel_path=rel_path)
  65. i.save()
  66. logger.info('New Item created: %s', repr(rel_path))
  67. return i
  68. def is_valid_area(x1, y1, x2, y2):
  69. for p in [x1, y1, x2, y2]:
  70. if type(p) is not int:
  71. return False
  72. if (x1, y1) == (x2, y2):
  73. return False
  74. return True
  75. class ItemData(dict):
  76. DATA_FIELDS = [
  77. 'size',
  78. 'datetime',
  79. 'exposure_program',
  80. 'exposure_time',
  81. 'flash',
  82. 'f_number',
  83. 'focal_length',
  84. 'lon',
  85. 'lat',
  86. 'height',
  87. 'iso',
  88. 'camera',
  89. 'orientation',
  90. 'width',
  91. 'duration',
  92. 'ratio',
  93. 'album',
  94. 'artist',
  95. 'bitrate',
  96. 'genre',
  97. 'title',
  98. 'track',
  99. 'year',
  100. 'sil',
  101. 'num_audio',
  102. 'num_folders',
  103. 'num_images',
  104. 'num_other',
  105. 'num_videos',
  106. ]
  107. DEFAULT_VALUES = {
  108. 'size': 0,
  109. 'datetime': datetime.datetime(1900, 1, 1),
  110. 'exposure_program': '-',
  111. 'exposure_time': 0,
  112. 'flash': '-',
  113. 'f_number': 0,
  114. 'focal_length': 0,
  115. 'iso': 0,
  116. 'camera': '-',
  117. 'orientation': 1,
  118. 'duration': 3,
  119. 'ratio': 1,
  120. 'album': '-',
  121. 'artist': '-',
  122. 'bitrate': 0,
  123. 'genre': '-',
  124. 'title': '-',
  125. 'track': 0,
  126. 'year': 0,
  127. 'num_audio': 0,
  128. 'num_folders': 0,
  129. 'num_images': 0,
  130. 'num_other': 0,
  131. 'num_videos': 0,
  132. }
  133. def __init__(self, item):
  134. for key in self.DATA_FIELDS:
  135. value = getattr(item, key + '_c')
  136. if value is not None:
  137. self[key] = value
  138. setattr(self, key, value)
  139. else:
  140. if key in self.DEFAULT_VALUES:
  141. setattr(self, key, self.DEFAULT_VALUES[key])
  142. @property
  143. def formatted_datetime(self):
  144. try:
  145. return formats.date_format(self.get('datetime'), format="SHORT_DATE_FORMAT", use_l10n=True) + ' - ' + formats.time_format(self.get('datetime'), use_l10n=True)
  146. except AttributeError:
  147. return 'No Datetime available'
  148. @property
  149. def gps(self):
  150. if self.get('lon') and self.get('lat'):
  151. return {'lon': self.get('lon'), 'lat': self.get('lat')}
  152. else:
  153. return None
  154. class Item(models.Model):
  155. DATA_VERSION_NUMBER = 0
  156. #
  157. rel_path = models.TextField(unique=True)
  158. type = models.CharField(max_length=25, choices=((TYPE_AUDIO, 'Audio'), (TYPE_FOLDER, 'Folder'), (TYPE_IMAGE, 'Image'), (TYPE_OTHER, 'Other'), (TYPE_VIDEO, 'Video')))
  159. public_access = models.BooleanField(default=False)
  160. read_access = models.ManyToManyField(User, related_name="read_access", blank=True)
  161. modify_access = models.ManyToManyField(User, related_name="modify_access", blank=True)
  162. favourite_of = models.ManyToManyField(User, related_name="favourite_of", blank=True)
  163. #
  164. uid_c = models.CharField(max_length=50, null=True, blank=True)
  165. settings_c = models.IntegerField(null=True, blank=True)
  166. data_version_c = models.IntegerField(null=True, blank=True)
  167. # common
  168. size_c = models.IntegerField(null=True, blank=True)
  169. datetime_c = models.DateTimeField(null=True, blank=True)
  170. # image
  171. exposure_program_c = models.CharField(max_length=100, null=True, blank=True)
  172. exposure_time_c = models.FloatField(null=True, blank=True)
  173. flash_c = models.CharField(max_length=100, null=True, blank=True)
  174. f_number_c = models.FloatField(null=True, blank=True)
  175. focal_length_c = models.FloatField(null=True, blank=True)
  176. lon_c = models.FloatField(null=True, blank=True)
  177. lat_c = models.FloatField(null=True, blank=True)
  178. height_c = models.IntegerField(null=True, blank=True)
  179. iso_c = models.IntegerField(null=True, blank=True)
  180. camera_c = models.CharField(max_length=100, null=True, blank=True)
  181. orientation_c = models.IntegerField(null=True, blank=True)
  182. width_c = models.IntegerField(null=True, blank=True)
  183. # video
  184. duration_c = models.FloatField(null=True, blank=True)
  185. ratio_c = models.FloatField(null=True, blank=True)
  186. # audio
  187. album_c = models.CharField(max_length=100, null=True, blank=True)
  188. artist_c = models.CharField(max_length=100, null=True, blank=True)
  189. bitrate_c = models.IntegerField(null=True, blank=True)
  190. genre_c = models.CharField(max_length=100, null=True, blank=True)
  191. title_c = models.CharField(max_length=100, null=True, blank=True)
  192. track_c = models.IntegerField(null=True, blank=True)
  193. year_c = models.IntegerField(null=True, blank=True)
  194. # folder
  195. num_audio_c = models.IntegerField(null=True, blank=True)
  196. num_folders_c = models.IntegerField(null=True, blank=True)
  197. num_images_c = models.IntegerField(null=True, blank=True)
  198. num_other_c = models.IntegerField(null=True, blank=True)
  199. num_videos_c = models.IntegerField(null=True, blank=True)
  200. sil_c = models.TextField(null=True, blank=True)
  201. MODEL_TO_MEDIA_DATA = {
  202. media.KEY_SIZE: 'size_c',
  203. media.KEY_TIME: 'datetime_c',
  204. media.KEY_EXPOSURE_PROGRAM: 'exposure_program_c',
  205. media.KEY_EXPOSURE_TIME: 'exposure_time_c',
  206. media.KEY_FLASH: 'flash_c',
  207. media.KEY_APERTURE: 'f_number_c',
  208. media.KEY_FOCAL_LENGTH: 'focal_length_c',
  209. media.KEY_GPS: {'lon': 'lon_c', 'lat': 'lat_c'},
  210. media.KEY_HEIGHT: 'height_c',
  211. media.KEY_ISO: 'iso_c',
  212. media.KEY_CAMERA: 'camera_c',
  213. media.KEY_ORIENTATION: 'orientation_c',
  214. media.KEY_WIDTH: 'width_c',
  215. media.KEY_DURATION: 'duration_c',
  216. media.KEY_RATIO: 'ratio_c',
  217. media.KEY_ALBUM: 'album_c',
  218. media.KEY_ARTIST: 'artist_c',
  219. media.KEY_BITRATE: 'bitrate_c',
  220. media.KEY_GENRE: 'genre_c',
  221. media.KEY_TITLE: 'title_c',
  222. media.KEY_TRACK: 'track_c',
  223. media.KEY_YEAR: 'year_c',
  224. }
  225. def __init__(self, *args, **kwargs):
  226. self.__current_uid__ = None
  227. self.__current_settings__ = None
  228. models.Model.__init__(self, *args, **kwargs)
  229. @property
  230. def name(self):
  231. return os.path.splitext(os.path.basename(self.rel_path))[0]
  232. @property
  233. def item_data(self):
  234. if self.__cache_update_needed__():
  235. #
  236. self.__set_model_fields_from_file__()
  237. #
  238. self.save()
  239. return self.cached_item_data
  240. @property
  241. def cached_item_data(self):
  242. return ItemData(self)
  243. def get_admin_url(self):
  244. """the url to the Django admin interface for the model instance"""
  245. info = (self._meta.app_label, self._meta.model_name)
  246. return reverse('admin:%s_%s_change' % info, args=(self.pk,))
  247. def suspended(self, user):
  248. if pygal.suspend_public() and not user.is_authenticated:
  249. return True
  250. def may_read(self, user):
  251. if self.suspended(user):
  252. return False
  253. elif self.type == TYPE_FOLDER:
  254. return True
  255. else:
  256. parent = get_item_by_rel_path(os.path.dirname(self.rel_path))
  257. if parent.public_access is True:
  258. return True
  259. if user is None:
  260. return False
  261. if not user.is_authenticated:
  262. return False
  263. if user.is_superuser:
  264. return True
  265. return user in parent.read_access.all()
  266. def may_modify(self, user):
  267. if self.suspended(user):
  268. return False
  269. elif self.type == TYPE_FOLDER:
  270. return user in self.modify_access.all()
  271. else:
  272. if user is None:
  273. return False
  274. if not user.is_authenticated:
  275. return False
  276. if user.is_superuser:
  277. return True
  278. parent = get_item_by_rel_path(os.path.dirname(self.rel_path))
  279. return user in parent.modify_access.all()
  280. def sort_string(self):
  281. if pygal.sort_by_date():
  282. try:
  283. tm = int(self.item_data.datetime.strftime('%s'))
  284. except AttributeError:
  285. raise AttributeError('Unable to create a sortstring for %s. Used datetime was: %s' % (str(self), repr(self.item_data.datetime)))
  286. return '%012d_%s' % (tm, os.path.basename(self.rel_path))
  287. else:
  288. return self.rel_path
  289. def sorted_itemlist(self):
  290. if self.type == TYPE_FOLDER:
  291. rv = []
  292. for rel_path in json.loads(self.item_data['sil']):
  293. try:
  294. rv.append(Item.objects.get(rel_path=rel_path))
  295. except Item.DoesNotExist:
  296. raise Item.DoesNotExist("%s: Item with rel_path=%s does not exist. in %s." % (str(self), rel_path))
  297. return rv
  298. else:
  299. return None
  300. def thumbnail_item(self):
  301. if self.type == TYPE_FOLDER:
  302. try:
  303. first_rel_path = json.loads(self.item_data['sil'])[0]
  304. return Item.objects.get(rel_path=first_rel_path).thumbnail_item()
  305. except IndexError:
  306. raise Item.DoesNotExist("%s: Tried to get the thumbnail_item for an empty list." % str(self))
  307. except Item.DoesNotExist:
  308. raise Item.DoesNotExist("%s: Item with rel_path=%s does not exist. in %s." % (str(self), first_rel_path))
  309. else:
  310. return self
  311. def all_itemslist(self):
  312. rv = []
  313. for i in self.sorted_itemlist():
  314. if i.type != TYPE_FOLDER:
  315. rv.append(i)
  316. else:
  317. rv.extend(i.all_itemslist())
  318. return rv
  319. def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
  320. if self.__cache_update_needed__():
  321. self.__set_model_fields_from_file__()
  322. return models.Model.save(self, force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)
  323. def current_uid(self):
  324. if self.__current_uid__ is None:
  325. self.__current_uid__ = fstools.uid(pygal.get_full_path(self.rel_path), None)
  326. return self.__current_uid__
  327. def current_settings(self):
  328. if self.__current_settings__ is None:
  329. self.__current_settings__ = pygal.show_audio() * 1 + pygal.show_image() * 2 + pygal.show_other() * 4 + pygal.show_video() * 8 + pygal.sort_by_date() * 16
  330. return self.__current_settings__
  331. def __cache_update_needed__(self):
  332. if self.type == TYPE_FOLDER:
  333. return DEBUG or self.settings_c != self.current_settings() or self.uid_c != self.current_uid() or self.data_version_c != self.DATA_VERSION_NUMBER
  334. else:
  335. return DEBUG or self.uid_c != self.current_uid() or self.data_version_c != self.DATA_VERSION_NUMBER
  336. def __set_model_fields_from_file__(self):
  337. logger.info('Updating cached data for Item: %s' % repr(self.rel_path))
  338. full_path = pygal.get_full_path(self.rel_path)
  339. self.type = get_item_type(full_path)
  340. self.uid_c = self.current_uid()
  341. self.settings_c = self.current_settings()
  342. self.data_version_c = self.DATA_VERSION_NUMBER
  343. if self.type == TYPE_FOLDER:
  344. self.__update_folder_file_data__(full_path)
  345. elif self.type == TYPE_OTHER:
  346. self.__update_other_file_data__(full_path)
  347. else:
  348. self.__update_media_file_data__(full_path)
  349. for key, value in self.cached_item_data.items():
  350. logger.debug(' - Adding %s=%s', key, repr(value))
  351. def __update_folder_file_data__(self, full_path):
  352. sil = []
  353. self.size_c = 0
  354. self.num_audio_c = 0
  355. self.num_folders_c = 1
  356. self.num_images_c = 0
  357. self.num_other_c = 0
  358. self.num_videos_c = 0
  359. for fn in os.listdir(full_path):
  360. sub_rel_path = pygal.get_rel_path(os.path.join(full_path, fn))
  361. sub_item = get_item_by_rel_path(sub_rel_path)
  362. # size, num_*
  363. if sub_item is not None: # Item does really exist / has sub-items
  364. if (sub_item.type == TYPE_FOLDER and len(json.loads(sub_item.item_data['sil']))) or sub_item.type in supported_types():
  365. self.size_c += sub_item.item_data.size
  366. if sub_item.type == TYPE_AUDIO:
  367. self.num_audio_c += 1
  368. elif sub_item.type == TYPE_FOLDER:
  369. self.num_audio_c += sub_item.item_data['num_audio']
  370. self.num_folders_c += sub_item.item_data['num_folders']
  371. self.num_images_c += sub_item.item_data['num_images']
  372. self.num_other_c += sub_item.item_data['num_other']
  373. self.num_videos_c += sub_item.item_data['num_videos']
  374. elif sub_item.type == TYPE_IMAGE:
  375. self.num_images_c += 1
  376. elif sub_item.type == TYPE_OTHER:
  377. self.num_other_c += 1
  378. elif sub_item.type == TYPE_VIDEO:
  379. self.num_videos_c += 1
  380. # sorted item list
  381. sil.append(sub_item)
  382. sil.sort(key=lambda entry: entry.sort_string(), reverse=pygal.sort_by_date())
  383. self.sil_c = json.dumps([i.rel_path for i in sil], indent=4)
  384. # datetime
  385. if len(sil) > 0:
  386. self.datetime_c = sil[0].datetime_c
  387. def __update_media_file_data__(self, full_path):
  388. data = media.get_media_data(full_path) or {}
  389. for key in self.MODEL_TO_MEDIA_DATA:
  390. value = data.get(key)
  391. if key == media.KEY_GPS: # Split GPS data in lon and lat
  392. for k in self.MODEL_TO_MEDIA_DATA[key]:
  393. value_k = value[k] if value is not None else None
  394. setattr(self, self.MODEL_TO_MEDIA_DATA[key][k], value_k)
  395. else:
  396. if value is not None:
  397. if key == media.KEY_TIME: # convert time to datetime
  398. if data.get(media.KEY_TIME_IS_SUBSTITUTION) and self.type == TYPE_IMAGE: # don't use time substitution for images
  399. value = None
  400. else:
  401. value = datetime.datetime.fromtimestamp(value, tz=datetime.timezone.utc)
  402. setattr(self, self.MODEL_TO_MEDIA_DATA[key], value)
  403. def __update_other_file_data__(self, full_path):
  404. self.size_c = os.path.getsize(full_path)
  405. #
  406. self.datetime_c = datetime.datetime.fromtimestamp(os.path.getctime(full_path), tz=timezone.utc)
  407. def __str__(self):
  408. return 'Item: %s' % self.rel_path
  409. def TagExist(item, data):
  410. return len(Tag.objects.filter(item=item, text=data['text'])) > 0
  411. class Tag(models.Model):
  412. item = models.ForeignKey(Item, on_delete=models.CASCADE)
  413. text = models.CharField(max_length=100)
  414. topleft_x = models.IntegerField(null=True, blank=True)
  415. topleft_y = models.IntegerField(null=True, blank=True)
  416. bottomright_x = models.IntegerField(null=True, blank=True)
  417. bottomright_y = models.IntegerField(null=True, blank=True)
  418. def __init__(self, *args, **kwargs):
  419. self.__tm_start__ = time.time()
  420. models.Model.__init__(self, *args, **kwargs)
  421. logger.log(5, 'Initialising Tag Model object in %.02fs: %s', time.time() - self.__tm_start__, str(self))
  422. @property
  423. def has_valid_coordinates(self):
  424. return is_valid_area(self.topleft_x, self.topleft_y, self.bottomright_x, self.bottomright_y)
  425. def get_admin_url(self):
  426. """the url to the Django admin interface for the model instance"""
  427. info = (self._meta.app_label, self._meta.model_name)
  428. return reverse('admin:%s_%s_change' % info, args=(self.pk,))
  429. def export_key(self):
  430. return self.item.rel_path
  431. def export_data(self):
  432. rv = {}
  433. rv['text'] = self.text
  434. if self.has_valid_coordinates:
  435. rv['topleft_x'] = self.topleft_x
  436. rv['topleft_y'] = self.topleft_y
  437. rv['bottomright_x'] = self.bottomright_x
  438. rv['bottomright_y'] = self.bottomright_y
  439. return rv