Browse Source

HUE-1596 [desktop] Create an examples tag

Romain Rigaux 12 năm trước cách đây
mục cha
commit
9b3f8df102

+ 33 - 5
desktop/core/src/desktop/models.py

@@ -54,6 +54,19 @@ class Settings(models.Model):
 
 class DocumentTagManager(models.Manager):
 
+  def get_tags(self, user):
+    # For now, the only shared tag is from 'sample' user and is named 'example'
+    # Tag permissions will come later.
+    tags = Document.objects.documents(user)
+
+    try:
+      sample_user = auth_models.User.objects.get(username='sample')
+      tags = tags.filter(Q(owner=user) | Q(owner=sample_user, tags__tag='example'))
+    except Exception, e:
+      tags = filter(owner=user)
+
+    return tags.values('tags__id', 'tags__tag').distinct()
+
   def create_tag(self, owner, tag_name):
     if tag_name in DocumentTag.RESERVED:
       raise Exception(_("Can't add %s: it is a reserved tag.") % tag_name)
@@ -73,6 +86,10 @@ class DocumentTagManager(models.Manager):
     tag, created = DocumentTag.objects.get_or_create(owner=user, tag=DocumentTag.HISTORY)
     return tag
 
+  def get_example_tag(self, user):
+    tag, created = DocumentTag.objects.get_or_create(owner=user, tag=DocumentTag.EXAMPLE)
+    return tag
+
   def tag(self, owner, doc_id, tag_name='', tag_id=None):
     try:
       tag = DocumentTag.objects.get(id=tag_id, owner=owner)
@@ -131,8 +148,9 @@ class DocumentTag(models.Model):
   DEFAULT = 'default' # Always there
   TRASH = 'trash' # There when the document is trashed
   HISTORY = 'history' # There when the document is a submission history
+  EXAMPLE = 'example' # Hue examples
 
-  RESERVED = (DEFAULT, TRASH, HISTORY)
+  RESERVED = (DEFAULT, TRASH, HISTORY, EXAMPLE)
 
   objects = DocumentTagManager()
   unique_together = ('owner', 'tag')
@@ -234,7 +252,7 @@ class DocumentManager(models.Manager):
 
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.description)
-          tag = DocumentTag.objects.get_default_tag(user=job.owner)
+          tag = DocumentTag.objects.get_example_tag(user=job.owner)
           doc.tags.add(tag)
           if job.is_trashed:
             doc.send_to_trash()
@@ -259,7 +277,7 @@ class DocumentManager(models.Manager):
 
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.name, description=job.desc, extra=job.type)
-          tag = DocumentTag.objects.get_default_tag(user=job.owner)
+          tag = DocumentTag.objects.get_example_tag(user=job.owner)
           doc.tags.add(tag)
           if job.is_trashed:
             doc.send_to_trash()
@@ -278,17 +296,27 @@ class DocumentManager(models.Manager):
 
         if not job.doc.exists():
           doc = Document.objects.link(job, owner=job.owner, name=job.dict['name'], description='')
-          tag = DocumentTag.objects.get_default_tag(user=job.owner)
+          tag = DocumentTag.objects.get_example_tag(user=job.owner)
           doc.tags.add(tag)
         if job.owner.username == 'sample':
           job.doc.get().share_to_default()
     except Exception, e:
       LOG.warn(force_unicode(e))
 
+    # Make sure doc have at least a tag
     try:
       for doc in Document.objects.filter(tags=None):
         default_tag = DocumentTag.objects.get_default_tag(doc.owner)
-        doc.tags.add(default_tag)      
+        doc.tags.add(default_tag)
+    except Exception, e:
+      LOG.warn(force_unicode(e))
+
+    # For now remove the default tag from the examples
+    try:
+      for doc in Document.objects.filter(tags__tag=DocumentTag.EXAMPLE):
+        print doc
+        default_tag = DocumentTag.objects.get_default_tag(doc.owner)
+        doc.tags.remove(default_tag)
     except Exception, e:
       LOG.warn(force_unicode(e))
 

+ 6 - 6
desktop/core/src/desktop/templates/home.mako

@@ -126,9 +126,9 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
            </li>
           <li class="nav-header tag-header">${_('Tags')} <div id="editTags" style="display: inline;cursor: pointer;margin-left: 6px" title="${ _('Edit tags') }"><i class="icon-tags"></i></div> </li>
           % for tag in tags:
-            %if tag.tag != "trash" and tag.tag != "history":
-            <li class="toggleTag white" data-tag="${ tag.tag }"><div class="hueCheckbox pull-left"></div>${ tag.tag }</li>
-            %endif
+            % if tag['tags__tag'] not in ('trash', 'history'):
+            <li class="toggleTag white" data-tag="${ tag['tags__tag'] }"><div class="hueCheckbox pull-left"></div>${ tag['tags__tag'] }</li>
+            % endif
           % endfor
 
         </ul>
@@ -392,7 +392,7 @@ $(document).ready(function () {
   function renderTags() {
     $(".toggleTag").remove();
     for (var i = JSON_TAGS.length - 1; i >= 0; i--) {
-      if (!JSON_TAGS[i].isTrash && !JSON_TAGS[i].isHistory) {
+      if (!JSON_TAGS[i].isTrash && !JSON_TAGS[i].isHistory && !JSON_TAGS[i].isExample) {
         var _t = $("<li>").addClass("toggleTag").addClass("white");
         _t.data("tag", JSON_TAGS[i].name);
         _t.html('<div class="hueCheckbox pull-left"></div>' + JSON_TAGS[i].name);
@@ -404,7 +404,7 @@ $(document).ready(function () {
   function renderTagsModal() {
     var _tags = "";
     for (var i = 0; i < JSON_TAGS.length; i++) {
-      if (!JSON_TAGS[i].isTrash && !JSON_TAGS[i].isHistory) {
+      if (!JSON_TAGS[i].isTrash && !JSON_TAGS[i].isHistory && !JSON_TAGS[i].isExample) {
         _tags += '<div style="margin-right:10px;margin-bottom: 6px;float:left;"><span class="tagsModalCheckbox badge" data-value="' + JSON_TAGS[i].id + '"><i class="icon-trash hide"></i> ' + JSON_TAGS[i].name + '</span></div>';
       }
     }
@@ -418,7 +418,7 @@ $(document).ready(function () {
       $("#documentTagsModalName").text(_doc.name);
       var _tags = "";
       for (var i = 0; i < JSON_TAGS.length; i++) {
-        if (!JSON_TAGS[i].isTrash && !JSON_TAGS[i].isHistory) {
+        if (!JSON_TAGS[i].isTrash && !JSON_TAGS[i].isHistory && !JSON_TAGS[i].isExample) {
           var _inTags = isInTags(_doc, JSON_TAGS[i].name);
           _tags += '<div style="margin-right:10px;margin-bottom: 6px;float:left;"><span class="documentTagsModalCheckbox badge' + (_inTags ? ' badge-info selected' : '') + '" data-value="' + JSON_TAGS[i].id + '"><i class="icon-ok-sign' + (_inTags ? '' : ' hide') + '"></i> ' + JSON_TAGS[i].name + '</span></div>';
         }

+ 11 - 12
desktop/core/src/desktop/views.py

@@ -49,8 +49,8 @@ LOG = logging.getLogger(__name__)
 
 
 def home(request):
-  docs = Document.objects.get_docs(request.user).order_by('-last_modified')[:100]
-  tags = DocumentTag.objects.filter(owner=request.user)
+  docs = Document.objects.get_docs(request.user).order_by('-last_modified')[:1000]
+  tags = DocumentTag.objects.get_tags(user=request.user)
 
   apps = appmanager.get_apps_dict(request.user)
 
@@ -64,21 +64,18 @@ def home(request):
 
 
 def list_docs(request):
-  docs = Document.objects.get_docs(request.user).order_by('-last_modified')[:100]
+  docs = Document.objects.get_docs(request.user).order_by('-last_modified')[:1000]
   return HttpResponse(json.dumps(massaged_documents_for_json(docs)), mimetype="application/json")
 
 
 def list_tags(request):
-  tags = DocumentTag.objects.filter(owner=request.user)
+  tags = DocumentTag.objects.get_tags(user=request.user)
   return HttpResponse(json.dumps(massaged_tags_for_json(tags, request.user)), mimetype="application/json")
 
 
 def massaged_documents_for_json(documents):
-  docs = []
-  for doc in documents:
-    docs.append(massage_doc_for_json(doc))
+  return [massage_doc_for_json(doc) for doc in documents]
 
-  return docs
 
 def massage_doc_for_json(doc):
   perms = doc.list_permissions()
@@ -105,12 +102,14 @@ def massaged_tags_for_json(tags, user):
   ts = []
   trash = DocumentTag.objects.get_trash_tag(user)
   history = DocumentTag.objects.get_history_tag(user)
+
   for tag in tags:
     massaged_tag = {
-      'id': tag.id,
-      'name': tag.tag,
-      'isTrash': tag.id == trash.id,
-      'isHistory': tag.id == history.id
+      'id': tag['tags__id'],
+      'name': tag['tags__tag'],
+      'isTrash': tag['tags__id'] == trash.id,
+      'isHistory': tag['tags__id'] == history.id,
+      'isExample': tag['tags__tag'] == DocumentTag.EXAMPLE
     }
     ts.append(massaged_tag)