models.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. try:
  18. import json
  19. except ImportError:
  20. import simplejson as json
  21. import posixpath
  22. from django.db import models
  23. from django.contrib.auth.models import User
  24. from django.utils.translation import ugettext as _, ugettext_lazy as _t
  25. from desktop.lib.exceptions_renderable import PopupException
  26. from hadoop.fs.hadoopfs import Hdfs
  27. class Document(models.Model):
  28. owner = models.ForeignKey(User, db_index=True, verbose_name=_t('Owner'), help_text=_t('User who can modify the job.'))
  29. is_design = models.BooleanField(default=True, db_index=True, verbose_name=_t('Is a user document, not a document submission.'),
  30. help_text=_t('If the document is not a submitted job but a real query, script, workflow.'))
  31. def is_editable(self, user):
  32. return user.is_superuser or self.owner == user
  33. def can_edit_or_exception(self, user, exception_class=PopupException):
  34. if self.is_editable(user):
  35. return True
  36. else:
  37. raise exception_class(_('Only superusers and %s are allowed to modify this document.') % user)
  38. class PigScript(Document):
  39. _ATTRIBUTES = ['script', 'name', 'properties', 'job_id', 'parameters', 'resources']
  40. data = models.TextField(default=json.dumps({
  41. 'script': '',
  42. 'name': '',
  43. 'properties': [],
  44. 'job_id': None,
  45. 'parameters': [],
  46. 'resources': []
  47. }))
  48. def update_from_dict(self, attrs):
  49. data_dict = self.dict
  50. for attr in PigScript._ATTRIBUTES:
  51. if attrs.get(attr) is not None:
  52. data_dict[attr] = attrs[attr]
  53. self.data = json.dumps(data_dict)
  54. @property
  55. def dict(self):
  56. return json.loads(self.data)
  57. def create_or_update_script(id, name, script, user, parameters, resources, is_design=True):
  58. """This take care of security"""
  59. try:
  60. pig_script = PigScript.objects.get(id=id)
  61. pig_script.can_edit_or_exception(user)
  62. except:
  63. pig_script = PigScript.objects.create(owner=user, is_design=is_design)
  64. pig_script.update_from_dict({
  65. 'name': name,
  66. 'script': script,
  67. 'parameters': parameters,
  68. 'resources': resources
  69. })
  70. return pig_script
  71. def get_scripts(user, max_count=200, is_design=None):
  72. scripts = []
  73. objects = PigScript.objects.filter(owner__pk__in=[user.pk, 1100713])
  74. if is_design is not None:
  75. objects = objects.filter(is_design=is_design)
  76. for script in objects.order_by('-id')[:max_count]:
  77. data = script.dict
  78. massaged_script = {
  79. 'id': script.id,
  80. 'name': data['name'],
  81. 'script': data['script'],
  82. 'parameters': data['parameters'],
  83. 'resources': data['resources'],
  84. 'isDesign': script.is_design,
  85. }
  86. scripts.append(massaged_script)
  87. return scripts
  88. def get_workflow_output(oozie_workflow, fs):
  89. # TODO: guess from the Input(s):/Output(s)
  90. output = None
  91. if 'workflowRoot' in oozie_workflow.conf_dict:
  92. output = oozie_workflow.conf_dict.get('workflowRoot')
  93. if output and not fs.exists(output):
  94. output = None
  95. return output
  96. def hdfs_link(url):
  97. if url:
  98. path = Hdfs.urlsplit(url)[2]
  99. if path:
  100. if path.startswith(posixpath.sep):
  101. return "/filebrowser/view" + path
  102. else:
  103. return "/filebrowser/home_relative_view/" + path
  104. else:
  105. return url
  106. else:
  107. return url