models.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import calendar
  18. import logging
  19. import json
  20. import uuid
  21. from itertools import chain
  22. from django.contrib.auth import models as auth_models
  23. from django.contrib.contenttypes import generic
  24. from django.contrib.contenttypes.models import ContentType
  25. from django.contrib.staticfiles.storage import staticfiles_storage
  26. from django.core.urlresolvers import reverse
  27. from django.db import models, transaction
  28. from django.db.models import Q
  29. from django.utils.translation import ugettext as _, ugettext_lazy as _t
  30. from desktop.lib.i18n import force_unicode
  31. from desktop.lib.exceptions_renderable import PopupException
  32. from desktop import appmanager
  33. LOG = logging.getLogger(__name__)
  34. SAMPLE_USERNAME = 'sample'
  35. class UserPreferences(models.Model):
  36. """Holds arbitrary key/value strings."""
  37. user = models.ForeignKey(auth_models.User)
  38. key = models.CharField(max_length=20)
  39. value = models.TextField(max_length=4096)
  40. class Settings(models.Model):
  41. collect_usage = models.BooleanField(db_index=True, default=True)
  42. tours_and_tutorials = models.BooleanField(db_index=True, default=True)
  43. @classmethod
  44. def get_settings(cls):
  45. settings, created = Settings.objects.get_or_create(id=1)
  46. return settings
  47. class DocumentTagManager(models.Manager):
  48. def get_tags(self, user):
  49. return self.filter(owner=user).distinct()
  50. def create_tag(self, owner, tag_name):
  51. if tag_name in DocumentTag.RESERVED:
  52. raise Exception(_("Can't add %s: it is a reserved tag.") % tag_name)
  53. else:
  54. tag, created = DocumentTag.objects.get_or_create(tag=tag_name, owner=owner)
  55. return tag
  56. def _get_tag(self, user, name):
  57. try:
  58. tag, created = DocumentTag.objects.get_or_create(owner=user, tag=name)
  59. except DocumentTag.MultipleObjectsReturned, ex:
  60. # We can delete duplicate tags of a user
  61. dups = DocumentTag.objects.filter(owner=user, tag=name)
  62. tag = dups[0]
  63. for dup in dups[1:]:
  64. LOG.warn('Deleting duplicate %s' % dup)
  65. dup.delete()
  66. return tag
  67. def get_default_tag(self, user):
  68. return self._get_tag(user, DocumentTag.DEFAULT)
  69. def get_trash_tag(self, user):
  70. return self._get_tag(user, DocumentTag.TRASH)
  71. def get_history_tag(self, user):
  72. return self._get_tag(user, DocumentTag.HISTORY)
  73. def get_example_tag(self, user):
  74. return self._get_tag(user, DocumentTag.EXAMPLE)
  75. def tag(self, owner, doc_id, tag_name='', tag_id=None):
  76. try:
  77. tag = DocumentTag.objects.get(id=tag_id, owner=owner)
  78. if tag.tag in DocumentTag.RESERVED:
  79. raise Exception(_("Can't add %s: it is a reserved tag.") % tag)
  80. except DocumentTag.DoesNotExist:
  81. tag = self._get_tag(user=owner, name=tag_name)
  82. doc = Document.objects.get_doc(doc_id, owner)
  83. doc.add_tag(tag)
  84. return tag
  85. def untag(self, tag_id, owner, doc_id):
  86. tag = DocumentTag.objects.get(id=tag_id, owner=owner)
  87. if tag.tag in DocumentTag.RESERVED:
  88. raise Exception(_("Can't remove %s: it is a reserved tag.") % tag)
  89. doc = Document.objects.get_doc(doc_id, owner=owner)
  90. doc.can_write_or_exception(owner)
  91. doc.remove_tag(tag)
  92. def delete_tag(self, tag_id, owner):
  93. tag = DocumentTag.objects.get(id=tag_id, owner=owner)
  94. default_tag = DocumentTag.objects.get_default_tag(owner)
  95. if tag.tag in DocumentTag.RESERVED:
  96. raise Exception(_("Can't remove %s: it is a reserved tag.") % tag)
  97. else:
  98. tag.delete()
  99. for doc in Document.objects.get_docs(owner).filter(tags=None):
  100. doc.add_tag(default_tag)
  101. def update_tags(self, owner, doc_id, tag_ids):
  102. doc = Document.objects.get_doc(doc_id, owner)
  103. doc.can_write_or_exception(owner)
  104. for tag in doc.tags.all():
  105. if tag.tag not in DocumentTag.RESERVED:
  106. doc.remove_tag(tag)
  107. for tag_id in tag_ids:
  108. tag = DocumentTag.objects.get(id=tag_id, owner=owner)
  109. if tag.tag not in DocumentTag.RESERVED:
  110. doc.add_tag(tag)
  111. return doc
  112. class DocumentTag(models.Model):
  113. """
  114. Reserved tags can't be manually removed by the user.
  115. """
  116. owner = models.ForeignKey(auth_models.User, db_index=True)
  117. tag = models.SlugField()
  118. DEFAULT = 'default' # Always there
  119. TRASH = 'trash' # There when the document is trashed
  120. HISTORY = 'history' # There when the document is a submission history
  121. EXAMPLE = 'example' # Hue examples
  122. RESERVED = (DEFAULT, TRASH, HISTORY, EXAMPLE)
  123. objects = DocumentTagManager()
  124. unique_together = ('owner', 'tag')
  125. def __unicode__(self):
  126. return force_unicode('%s') % (self.tag,)
  127. class DocumentManager(models.Manager):
  128. def documents(self, user):
  129. return Document.objects.filter(
  130. Q(owner=user) |
  131. Q(documentpermission__users=user) |
  132. Q(documentpermission__groups__in=user.groups.all())
  133. ).defer('description', 'extra').distinct()
  134. def get_docs(self, user, model_class=None, extra=None):
  135. docs = Document.objects.documents(user).exclude(name='pig-app-hue-script')
  136. if model_class is not None:
  137. ct = ContentType.objects.get_for_model(model_class)
  138. docs = docs.filter(content_type=ct)
  139. if extra is not None:
  140. docs = docs.filter(extra=extra)
  141. return docs
  142. def get_doc(self, doc_id, user):
  143. return Document.objects.documents(user).get(id=doc_id)
  144. def trashed_docs(self, model_class, user):
  145. tag = DocumentTag.objects.get_trash_tag(user=user)
  146. return Document.objects.get_docs(user, model_class).filter(tags__in=[tag]).order_by('-last_modified')
  147. def trashed(self, model_class, user):
  148. docs = self.trashed_docs(model_class, user)
  149. return [job.content_object for job in docs if job.content_object]
  150. def available_docs(self, model_class, user, with_history=False):
  151. exclude = [DocumentTag.objects.get_trash_tag(user=user)]
  152. if not with_history:
  153. exclude.append(DocumentTag.objects.get_history_tag(user=user))
  154. return Document.objects.get_docs(user, model_class).exclude(tags__in=exclude).order_by('-last_modified')
  155. def history_docs(self, model_class, user):
  156. include = [DocumentTag.objects.get_history_tag(user=user)]
  157. exclude = [DocumentTag.objects.get_trash_tag(user=user)]
  158. return Document.objects.get_docs(user, model_class).filter(tags__in=include).exclude(tags__in=exclude).order_by('-last_modified')
  159. def available(self, model_class, user, with_history=False):
  160. docs = self.available_docs(model_class, user, with_history)
  161. return [doc.content_object for doc in docs if doc.content_object]
  162. def can_read_or_exception(self, user, doc_class, doc_id, exception_class=PopupException):
  163. if doc_id is None:
  164. return
  165. try:
  166. ct = ContentType.objects.get_for_model(doc_class)
  167. doc = Document.objects.get(object_id=doc_id, content_type=ct)
  168. if doc.can_read(user):
  169. return doc
  170. else:
  171. message = _("Permission denied. %(username)s does not have the permissions required to access document %(id)s") % \
  172. {'username': user.username, 'id': doc.id}
  173. raise exception_class(message)
  174. except Document.DoesNotExist:
  175. raise exception_class(_('Document %(id)s does not exist') % {'id': doc_id})
  176. def can_read(self, user, doc_class, doc_id):
  177. ct = ContentType.objects.get_for_model(doc_class)
  178. doc = Document.objects.get(object_id=doc_id, content_type=ct)
  179. return doc.can_read(user)
  180. def link(self, content_object, owner, name='', description='', extra=''):
  181. if not content_object.doc.exists():
  182. doc = Document.objects.create(
  183. content_object=content_object,
  184. owner=owner,
  185. name=name,
  186. description=description,
  187. extra=extra
  188. )
  189. tag = DocumentTag.objects.get_default_tag(user=owner)
  190. doc.tags.add(tag)
  191. return doc
  192. else:
  193. LOG.warn('Object %s already has documents: %s' % (content_object, content_object.doc.all()))
  194. return content_object.doc.all()[0]
  195. def sync(self):
  196. try:
  197. with transaction.atomic():
  198. from oozie.models import Workflow, Coordinator, Bundle
  199. for job in list(chain(Workflow.objects.all(), Coordinator.objects.all(), Bundle.objects.all())):
  200. if job.doc.count() > 1:
  201. LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
  202. job.doc.all().delete()
  203. if not job.doc.exists():
  204. doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.description)
  205. tag = DocumentTag.objects.get_example_tag(user=job.owner)
  206. doc.tags.add(tag)
  207. if job.is_trashed:
  208. doc.send_to_trash()
  209. if job.is_shared:
  210. doc.share_to_default()
  211. if hasattr(job, 'managed'):
  212. if not job.managed:
  213. doc.extra = 'jobsub'
  214. doc.save()
  215. if job.owner.username == SAMPLE_USERNAME:
  216. job.doc.get().share_to_default()
  217. except Exception, e:
  218. LOG.warn(force_unicode(e))
  219. try:
  220. with transaction.atomic():
  221. from beeswax.models import SavedQuery
  222. for job in SavedQuery.objects.all():
  223. if job.doc.count() > 1:
  224. LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
  225. job.doc.all().delete()
  226. if not job.doc.exists():
  227. doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.desc, extra=job.type)
  228. tag = DocumentTag.objects.get_example_tag(user=job.owner)
  229. doc.tags.add(tag)
  230. if job.is_trashed:
  231. doc.send_to_trash()
  232. if job.owner.username == SAMPLE_USERNAME:
  233. job.doc.get().share_to_default()
  234. except Exception, e:
  235. LOG.warn(force_unicode(e))
  236. try:
  237. with transaction.atomic():
  238. from pig.models import PigScript
  239. for job in PigScript.objects.all():
  240. if job.doc.count() > 1:
  241. LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
  242. job.doc.all().delete()
  243. if not job.doc.exists():
  244. doc = Document.objects.link(job, owner=job.owner, name=job.dict['name'], description='')
  245. tag = DocumentTag.objects.get_example_tag(user=job.owner)
  246. doc.tags.add(tag)
  247. if job.owner.username == SAMPLE_USERNAME:
  248. job.doc.get().share_to_default()
  249. except Exception, e:
  250. LOG.warn(force_unicode(e))
  251. try:
  252. with transaction.atomic():
  253. for job in Document2.objects.all():
  254. if job.doc.count() > 1:
  255. LOG.warn('Deleting duplicate document %s for %s' % (job.doc.all(), job))
  256. job.doc.all().delete()
  257. if not job.doc.exists():
  258. if job.type == 'oozie-workflow2':
  259. extra = 'workflow2'
  260. elif job.type == 'oozie-coordinator2':
  261. extra = 'coordinator2'
  262. elif job.type == 'oozie-bundle2':
  263. extra = 'bundle2'
  264. elif job.type == 'notebook':
  265. extra = 'notebook'
  266. else:
  267. extra = ''
  268. doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.description, extra=extra)
  269. if job.owner.username == SAMPLE_USERNAME:
  270. doc = job.doc.get()
  271. doc.share_to_default()
  272. tag = DocumentTag.objects.get_example_tag(user=job.owner)
  273. doc.tags.add(tag)
  274. except Exception, e:
  275. LOG.warn(force_unicode(e))
  276. # Make sure doc have at least a tag
  277. try:
  278. for doc in Document.objects.filter(tags=None):
  279. default_tag = DocumentTag.objects.get_default_tag(doc.owner)
  280. doc.tags.add(default_tag)
  281. except Exception, e:
  282. LOG.warn(force_unicode(e))
  283. # For now remove the default tag from the examples
  284. try:
  285. for doc in Document.objects.filter(tags__tag=DocumentTag.EXAMPLE):
  286. default_tag = DocumentTag.objects.get_default_tag(doc.owner)
  287. doc.tags.remove(default_tag)
  288. except Exception, e:
  289. LOG.warn(force_unicode(e))
  290. # Delete documents with no object
  291. try:
  292. for doc in Document.objects.all():
  293. if doc.content_type is None or doc.content_object is None:
  294. doc.delete()
  295. except Exception, e:
  296. LOG.warn(force_unicode(e))
  297. UTC_TIME_FORMAT = "%Y-%m-%dT%H:%MZ"
  298. class Document(models.Model):
  299. owner = models.ForeignKey(auth_models.User, db_index=True, verbose_name=_t('Owner'), help_text=_t('User who can own the job.'), related_name='doc_owner')
  300. name = models.CharField(default='', max_length=255)
  301. description = models.TextField(default='')
  302. last_modified = models.DateTimeField(auto_now=True, db_index=True, verbose_name=_t('Last modified'))
  303. version = models.SmallIntegerField(default=1, verbose_name=_t('Schema version'))
  304. extra = models.TextField(default='')
  305. tags = models.ManyToManyField(DocumentTag, db_index=True)
  306. content_type = models.ForeignKey(ContentType)
  307. object_id = models.PositiveIntegerField()
  308. content_object = generic.GenericForeignKey('content_type', 'object_id')
  309. objects = DocumentManager()
  310. unique_together = ('content_type', 'object_id')
  311. def __unicode__(self):
  312. return force_unicode('%s %s %s') % (self.content_type, self.name, self.owner)
  313. def is_editable(self, user):
  314. """Deprecated by can_read"""
  315. return self.can_write(user)
  316. def can_edit_or_exception(self, user, exception_class=PopupException):
  317. """Deprecated by can_write_or_exception"""
  318. return self.can_write_or_exception(user, exception_class)
  319. def add_tag(self, tag):
  320. self.tags.add(tag)
  321. def remove_tag(self, tag):
  322. self.tags.remove(tag)
  323. def is_trashed(self):
  324. return DocumentTag.objects.get_trash_tag(user=self.owner) in self.tags.all()
  325. def is_historic(self):
  326. return DocumentTag.objects.get_history_tag(user=self.owner) in self.tags.all()
  327. def send_to_trash(self):
  328. tag = DocumentTag.objects.get_trash_tag(user=self.owner)
  329. self.tags.add(tag)
  330. def restore_from_trash(self):
  331. tag = DocumentTag.objects.get_trash_tag(user=self.owner)
  332. self.tags.remove(tag)
  333. def add_to_history(self):
  334. tag = DocumentTag.objects.get_history_tag(user=self.owner)
  335. self.tags.add(tag)
  336. def remove_from_history(self):
  337. tag = DocumentTag.objects.get_history_tag(user=self.owner)
  338. self.tags.remove(tag)
  339. def share_to_default(self, name='read'):
  340. DocumentPermission.objects.share_to_default(self, name=name)
  341. def can_read(self, user):
  342. return user.is_superuser or self.owner == user or Document.objects.get_docs(user).filter(id=self.id).exists()
  343. def can_write(self, user):
  344. perm = self.list_permissions('write')
  345. return user.is_superuser or self.owner == user or perm.groups.filter(id__in=user.groups.all()).exists() or user in perm.users.all()
  346. def can_read_or_exception(self, user, exception_class=PopupException):
  347. if self.can_read(user):
  348. return True
  349. else:
  350. raise exception_class(_('Only superusers and %s are allowed to read this document.') % user)
  351. def can_write_or_exception(self, user, exception_class=PopupException):
  352. if self.can_write(user):
  353. return True
  354. else:
  355. raise exception_class(_('Only superusers and %s are allowed to write this document.') % user)
  356. def copy(self, name=None, owner=None):
  357. copy_doc = self
  358. copy_doc.pk = None
  359. copy_doc.id = None
  360. if name is not None:
  361. copy_doc.name = name
  362. if owner is not None:
  363. copy_doc.owner = owner
  364. copy_doc.save()
  365. # Don't copy tags
  366. default_tag = DocumentTag.objects.get_default_tag(copy_doc.owner)
  367. tags = [default_tag]
  368. copy_doc.tags.add(*tags)
  369. return copy_doc
  370. @property
  371. def icon(self):
  372. apps = appmanager.get_apps_dict()
  373. try:
  374. if self.extra == 'workflow2':
  375. return staticfiles_storage.url('oozie/art/icon_oozie_workflow_48.png')
  376. elif self.extra == 'coordinator2':
  377. return staticfiles_storage.url('oozie/art/icon_oozie_coordinator_48.png')
  378. elif self.extra == 'bundle2':
  379. return staticfiles_storage.url('oozie/art/icon_oozie_bundle_48.png')
  380. elif self.extra == 'notebook':
  381. return staticfiles_storage.url('spark/art/icon_spark_48.png')
  382. elif self.content_type.app_label == 'beeswax':
  383. if self.extra == '0':
  384. return staticfiles_storage.url(apps['beeswax'].icon_path)
  385. elif self.extra == '3':
  386. return staticfiles_storage.url(apps['spark'].icon_path)
  387. else:
  388. return staticfiles_storage.url(apps['impala'].icon_path)
  389. elif self.content_type.app_label == 'oozie':
  390. if self.extra == 'jobsub':
  391. return staticfiles_storage.url(apps['jobsub'].icon_path)
  392. else:
  393. return staticfiles_storage.url(self.content_type.model_class().ICON)
  394. elif self.content_type.app_label in apps:
  395. return staticfiles_storage.url(apps[self.content_type.app_label].icon_path)
  396. else:
  397. return staticfiles_storage.url('desktop/art/icon_hue_48.png')
  398. except Exception, e:
  399. LOG.warn(force_unicode(e))
  400. return staticfiles_storage.url('desktop/art/icon_hue_48.png')
  401. def share(self, users, groups, name='read'):
  402. DocumentPermission.objects.filter(document=self, name=name).update(users=users, groups=groups, add=True)
  403. def unshare(self, users, groups, name='read'):
  404. DocumentPermission.objects.filter(document=self, name=name).update(users=users, groups=groups, add=False)
  405. def sync_permissions(self, perms_dict):
  406. """
  407. Set who else or which other group can interact with the document.
  408. Example of input: {'read': {'user_ids': [1, 2, 3], 'group_ids': [1, 2, 3]}}
  409. """
  410. for name, perm in perms_dict.iteritems():
  411. users = groups = None
  412. if perm.get('user_ids'):
  413. users = auth_models.User.objects.in_bulk(perm.get('user_ids'))
  414. if perm.get('group_ids'):
  415. groups = auth_models.Group.objects.in_bulk(perm.get('group_ids'))
  416. else:
  417. groups = []
  418. DocumentPermission.objects.sync(document=self, name=name, users=users, groups=groups)
  419. def list_permissions(self, perm='read'):
  420. return DocumentPermission.objects.list(document=self, perm=perm)
  421. def to_dict(self):
  422. return {
  423. 'owner': self.owner.username,
  424. 'name': self.name,
  425. 'description': self.description,
  426. 'uuid': None, # no uuid == v1
  427. 'id': self.id,
  428. 'doc1_id': self.id,
  429. 'object_id': self.object_id,
  430. 'type': str(self.content_type),
  431. 'last_modified': self.last_modified.strftime(UTC_TIME_FORMAT),
  432. 'last_modified_ts': calendar.timegm(self.last_modified.utctimetuple()),
  433. 'isSelected': False
  434. }
  435. class DocumentPermissionManager(models.Manager):
  436. def _check_perm(self, name):
  437. perms = (DocumentPermission.READ_PERM, DocumentPermission.WRITE_PERM)
  438. if name not in perms:
  439. perms_string = ' and '.join(', '.join(perms).rsplit(', ', 1))
  440. raise PopupException(_('Only %s permissions are supported, not %s.') % (perms_string, name))
  441. def share_to_default(self, document, name='read'):
  442. from useradmin.models import get_default_user_group # Remove build dependency
  443. self._check_perm(name)
  444. if name == DocumentPermission.WRITE_PERM:
  445. perm, created = DocumentPermission.objects.get_or_create(doc=document, perms=DocumentPermission.WRITE_PERM)
  446. else:
  447. perm, created = DocumentPermission.objects.get_or_create(doc=document, perms=DocumentPermission.READ_PERM)
  448. default_group = get_default_user_group()
  449. if default_group:
  450. perm.groups.add(default_group)
  451. def update(self, document, name='read', users=None, groups=None, add=True):
  452. self._check_perm(name)
  453. perm, created = DocumentPermission.objects.get_or_create(doc=document, perms=name)
  454. if users is not None:
  455. if add:
  456. perm.users.add(*users)
  457. else:
  458. perm.users.remove(*users)
  459. if groups is not None:
  460. if add:
  461. perm.groups.add(*groups)
  462. else:
  463. perm.groups.remove(*groups)
  464. if not perm.users and not perm.groups:
  465. perm.delete()
  466. def sync(self, document, name='read', users=None, groups=None):
  467. self._check_perm(name)
  468. perm, created = DocumentPermission.objects.get_or_create(doc=document, perms=name)
  469. if users is not None:
  470. perm.users = []
  471. perm.users = users
  472. perm.save()
  473. if groups is not None:
  474. perm.groups = []
  475. perm.groups = groups
  476. perm.save()
  477. if not users and not groups:
  478. perm.delete()
  479. def list(self, document, perm='read'):
  480. try:
  481. perm, created = DocumentPermission.objects.get_or_create(doc=document, perms=perm)
  482. except DocumentPermission.MultipleObjectsReturned:
  483. # We can delete duplicate perms of a document
  484. dups = DocumentPermission.objects.filter(doc=document, perms=perm)
  485. perm = dups[0]
  486. for dup in dups[1:]:
  487. LOG.warn('Deleting duplicate %s' % dup)
  488. dup.delete()
  489. return perm
  490. class DocumentPermission(models.Model):
  491. READ_PERM = 'read'
  492. WRITE_PERM = 'write'
  493. doc = models.ForeignKey(Document)
  494. users = models.ManyToManyField(auth_models.User, db_index=True, db_table='documentpermission_users')
  495. groups = models.ManyToManyField(auth_models.Group, db_index=True, db_table='documentpermission_groups')
  496. perms = models.TextField(default=READ_PERM, choices=( # one perm
  497. (READ_PERM, 'read'),
  498. (WRITE_PERM, 'write'),
  499. ))
  500. objects = DocumentPermissionManager()
  501. unique_together = ('doc', 'perms')
  502. class Document2Manager(models.Manager):
  503. def get_by_natural_key(self, uuid, version, is_history):
  504. return self.get(uuid=uuid, version=version, is_history=is_history)
  505. def uuid_default():
  506. return str(uuid.uuid4())
  507. class Document2(models.Model):
  508. owner = models.ForeignKey(auth_models.User, db_index=True, verbose_name=_t('Owner'), help_text=_t('Creator.'), related_name='doc2_owner')
  509. name = models.CharField(default='', max_length=255)
  510. description = models.TextField(default='')
  511. uuid = models.CharField(default=uuid_default, max_length=36, db_index=True)
  512. type = models.CharField(default='', max_length=32, db_index=True, help_text=_t('Type of document, e.g. Hive query, Oozie workflow, Search Dashboard...'))
  513. data = models.TextField(default='{}')
  514. extra = models.TextField(default='')
  515. last_modified = models.DateTimeField(auto_now=True, db_index=True, verbose_name=_t('Time last modified'))
  516. version = models.SmallIntegerField(default=1, verbose_name=_t('Document version'), db_index=True)
  517. is_history = models.BooleanField(default=False, db_index=True)
  518. tags = models.ManyToManyField('self', db_index=True)
  519. dependencies = models.ManyToManyField('self', db_index=True)
  520. doc = generic.GenericRelation(Document, related_name='doc_doc') # Compatibility with Hue 3
  521. objects = Document2Manager()
  522. unique_together = ('uuid', 'version', 'is_history')
  523. def natural_key(self):
  524. return (self.uuid, self.version, self.is_history)
  525. @property
  526. def data_dict(self):
  527. if not self.data:
  528. self.data = json.dumps({})
  529. data_python = json.loads(self.data)
  530. return data_python
  531. def update_data(self, post_data):
  532. data_dict = self.data_dict
  533. data_dict.update(post_data)
  534. self.data = json.dumps(data_dict)
  535. def get_absolute_url(self):
  536. if self.type == 'oozie-coordinator2':
  537. return reverse('oozie:edit_coordinator') + '?coordinator=' + str(self.id)
  538. elif self.type == 'oozie-bundle2':
  539. return reverse('oozie:edit_bundle') + '?bundle=' + str(self.id)
  540. elif self.type == 'notebook':
  541. return reverse('spark:editor') + '?notebook=' + str(self.id)
  542. else:
  543. return reverse('oozie:edit_workflow') + '?workflow=' + str(self.id)
  544. def to_dict(self):
  545. return {
  546. 'owner': self.owner.username,
  547. 'name': self.name,
  548. 'description': self.description,
  549. 'uuid': self.uuid,
  550. 'id': self.id,
  551. 'doc1_id': self.doc.get().id if self.doc.exists() else -1,
  552. 'type': self.type,
  553. 'last_modified': self.last_modified.strftime(UTC_TIME_FORMAT),
  554. 'last_modified_ts': calendar.timegm(self.last_modified.utctimetuple()),
  555. 'isSelected': False
  556. }
  557. def can_read_or_exception(self, user):
  558. self.doc.get().can_read_or_exception(user)