- add tag support to user
authorraph
Thu, 10 Dec 2009 14:27:12 +0100
changeset 38 be1807d390d4
parent 37 d62454bf4402
child 39 1b939dcb0b05
- add tag support to user - add tag interface to user / text list
src/cm/media/css/base.css
src/cm/models.py
src/cm/templates/site/macros/user_actions.html
src/cm/templates/site/text_list.html
src/cm/templates/site/user_list.html
src/cm/views/texts.py
src/cm/views/user.py
--- a/src/cm/media/css/base.css	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/media/css/base.css	Thu Dec 10 14:27:12 2009 +0100
@@ -796,4 +796,9 @@
 
 #paginator {
 	font-weight: bold;
-}
\ No newline at end of file
+}
+
+a.tag {
+	color: #474747;
+}
+
--- a/src/cm/models.py	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/models.py	Thu Dec 10 14:27:12 2009 +0100
@@ -569,6 +569,8 @@
     is_email_error = models.BooleanField(default=False)
     is_suspended = models.BooleanField(ugettext_lazy(u'Suspended access'), default=False) # used to disable access or to wait for approval when registering
 
+    tags = TagField(ugettext_lazy("Tags"), max_length=1000)
+
     objects = RegistrationManager()
 
     class Meta:
--- a/src/cm/templates/site/macros/user_actions.html	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/templates/site/macros/user_actions.html	Thu Dec 10 14:27:12 2009 +0100
@@ -1,5 +1,6 @@
 {% load com %}
 {% load i18n %}
+{% load tagging_tags %}
 <b>
 	{% ifequal user request.user %}
     <a title="{% blocktrans %}Your profile{% endblocktrans %}" class="main_object_title" href="{% url profile %}">{{ user.get_profile.admin_print }}</a>
@@ -7,6 +8,10 @@
     <a title="{% blocktrans with user.get_profile.admin_print as userprint %}Edit user {{ userprint }}{% endblocktrans %}" class="main_object_title" href="{% url user-edit user.get_profile.key %}">{{ user.get_profile.admin_print }}</a>
     {% endifequal %}
 </b>
+
+{% tags_for_object user.get_profile as tag_list %}
+{% if tag_list %}<div class="tag_list>">{% blocktrans %}tags:{% endblocktrans %}&nbsp;{% for tag in tag_list %}<a class="tag" title="{% blocktrans with tag.name as tag_name %}Filter by tag: {{ tag_name }}{% endblocktrans %}" href="?{% newparams 'tag_selected' tag.name %}"">{{ tag.name }}</a>&nbsp;{% endfor %}</div>{% endif %}
+
 <div class="hidden-user-actions user-actions-{{ user.get_profile.key }}" style="visibility:hidden;">
 {% ifequal user request.user %}
   <a title="Your profile" href="{% url profile %}">{% blocktrans %}Your profile{% endblocktrans %}</a> 
--- a/src/cm/templates/site/text_list.html	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/templates/site/text_list.html	Thu Dec 10 14:27:12 2009 +0100
@@ -3,6 +3,7 @@
 {% load i18n %}
 {% load local_perms %}
 {% load activity %}
+{% load tagging_tags %}
 
 {% block head %}
 {% endblock %}
@@ -42,6 +43,34 @@
     {% endif %}
 </ul>
 
+<form id="filter_form" action="." method="get">
+<table summary="{% blocktrans %}People' list{% endblocktrans %}" class="large_table">
+	<tr>
+		<td>
+		{% blocktrans %}Filter by tag:{% endblocktrans %} 
+		<select id="tag_selected" name="tag_selected">
+		<option {% if not tag_selected %}selected="selected"{% endif %} value="0">- {% blocktrans %}All{% endblocktrans %} -</option>
+		
+		{% for tag in tag_list %}
+		<option  {% ifequal tag_selected tag.name %}selected="selected"{% endifequal %} value="{{ tag.name }}">{{ tag.name }}</option>
+		{% endfor %}
+		</select>
+		</td>
+		
+	</tr>
+</table>
+</form>
+
+<script type="text/javascript">
+<!--
+$(function() {
+    $("#tag_selected").change(function () {
+    	$("#filter_form").submit();
+      });
+}) ;
+-->
+</script>
+
 {% if object_list %}
 <form id="texts_form" action="." method="post">
 
@@ -122,6 +151,10 @@
             {% if can_manage_workspace %}<td><input type="checkbox" class="text_check" name="check-{{ text.key }}"/></td>{% endif %}
             <td>
                 <a class="main_object_title" href="{% url text-view text.key %}">{{ text.title }}</a>
+                
+				{% tags_for_object text.last_text_version as tag_list %}
+				{% if tag_list %}<div class="tag_list>">{% blocktrans %}tags:{% endblocktrans %}&nbsp;{% for tag in tag_list %}<a class="tag" title="{% blocktrans with tag.name as tag_name %}Filter by tag: {{ tag_name }}{% endblocktrans %}" href="?{% newparams 'tag_selected' tag.name %}"">{{ tag.name }}</a>&nbsp;{% endfor %}</div>{% endif %}
+                
                 <div class="hidden-text-actions text-actions-{{ text.key }}">
                     <a href="{% url text-view text.key %}">{% blocktrans %}View{% endblocktrans %}</a> | 
                     {% if can_edit_text %}<a href="{% url text-edit text.key %}">{% blocktrans %}Edit{% endblocktrans %}</a> | {% endif %}
--- a/src/cm/templates/site/user_list.html	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/templates/site/user_list.html	Thu Dec 10 14:27:12 2009 +0100
@@ -28,8 +28,6 @@
 
 <div id="user" class="tab-meta">
 
-<form id="user_form" action="." method="post">
-
 <ul class="sub_list">
     <li class="active_sub">{% blocktrans %}People' list{% endblocktrans %}</li>
     <li> / </li>
@@ -38,8 +36,40 @@
     <li><a href="{% url user-mass-add %}">{% blocktrans %}Add users in bulk{% endblocktrans %}</a></li>    
 </ul>
 
+<form id="filter_form" action="." method="get">
+<table summary="{% blocktrans %}People' list{% endblocktrans %}" class="large_table">
+	<tr>
+		<td>
+		{% if display_suspended_users %}<a href="?{% newparams 'display' '0' %}" title="{% blocktrans %}Hide suspended users{% endblocktrans %}">{% blocktrans %}Hide suspended users{% endblocktrans %}</a>{% else %}<a href="?{% newparams 'display' '1' %}" title="{% blocktrans %}Display suspended users{% endblocktrans %}">{% blocktrans %}Display suspended users{% endblocktrans %}</a>{% endif %}
+		&#183;
+		{% blocktrans %}Filter by tag:{% endblocktrans %} 
+		<select id="tag_selected" name="tag_selected">
+		<option {% if not tag_selected %}selected="selected"{% endif %} value="0">- {% blocktrans %}All{% endblocktrans %} -</option>
+		
+		{% for tag in tag_list %}
+		<option  {% ifequal tag_selected tag.name %}selected="selected"{% endifequal %} value="{{ tag.name }}">{{ tag.name }}</option>
+		{% endfor %}
+		</select>
+		</td>
+		
+	</tr>
+</table>
+</form>
+
+<script type="text/javascript">
+<!--
+$(function() {
+    $("#tag_selected").change(function () {
+    	$("#filter_form").submit();
+      });
+}) ;
+-->
+</script>
+
+<form id="user_form" action="." method="post">
+
 <select id="bulk_actions" name="action">
-<option selected="selected" value="-1">{% blocktrans %}Bulk Actions{% endblocktrans %}</option>
+<option selected="selected" value="-1">- {% blocktrans %}Bulk Actions{% endblocktrans %} -</option>
 <option value="disable">{% blocktrans %}Suspend access{% endblocktrans %}</option>
 <option value="enable">{% blocktrans %}Enable access{% endblocktrans %}</option>
 {% for bulk_role in all_roles %}
@@ -50,8 +80,6 @@
 <input name="apply" type="hidden" value="apply"/>
 <input name="apply_action" id="apply" type="button" value="{% blocktrans %}Apply{% endblocktrans %}" disabled="disabled"/>
 
-&nbsp;{% if display_suspended_users %}<a href="?{% newparams 'display' '0' %}" title="{% blocktrans %}Hide suspended users{% endblocktrans %}">{% blocktrans %}Hide suspended users{% endblocktrans %}</a>{% else %}<a href="?{% newparams 'display' '1' %}" title="{% blocktrans %}Display suspended users{% endblocktrans %}">{% blocktrans %}Display suspended users{% endblocktrans %}</a>{% endif %}
-
 <script type="text/javascript">
 <!--
 function check_activate_apply() {
--- a/src/cm/views/texts.py	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/views/texts.py	Thu Dec 10 14:27:12 2009 +0100
@@ -35,6 +35,7 @@
 from django.template.loader import render_to_string
 from django.utils.translation import ugettext as _, ugettext_lazy
 from django.views.generic.list_detail import object_list
+from tagging.models import Tag
 import difflib
 import logging
 import mimetypes
@@ -146,7 +147,8 @@
 # TODO: set global access perm
 def text_list(request):
     paginate_by = get_int(request.GET,'paginate',TEXT_PAGINATION)
-    
+    tag_selected = request.GET.get('tag_selected', 0)
+        
     order_by = get_among(request.GET,'order',('title','author','modified','-title','-author','-modified'),'-modified')
 
     if request.method == 'POST':
@@ -163,9 +165,27 @@
             return HttpResponseRedirect(reverse('text'))
 
     texts = get_texts_with_perm(request, 'can_view_text').order_by(order_by)
+
+    context = {    
+               'tag_list' : Tag.objects.usage_for_queryset(TextVersion.objects.filter(id__in = [t.last_text_version_id for t in get_texts_with_perm(request, 'can_view_text')])),
+               'tag_selected': tag_selected,
+               }
+
+    if tag_selected:     
+        tag_ids = Tag.objects.filter(name=tag_selected)
+        if tag_ids:   
+            content_type_id = ContentType.objects.get_for_model(TextVersion).pk
+            # table cm_userprofile is not present if display_suspended_users: fix this 
+            texts = texts.extra(where=['tagging_taggeditem.object_id = cm_text.last_text_version_id', 
+                                       'tagging_taggeditem.content_type_id = %i' %content_type_id,
+                                       'tagging_taggeditem.tag_id = %i' %tag_ids[0].id],
+                                tables=['tagging_taggeditem'],
+                                )
+    
     return object_list(request, texts,
                        template_name = 'site/text_list.html',
                        paginate_by = paginate_by,
+                       extra_context=context,
                        )
     
 @has_perm_on_text('can_view_text')
--- a/src/cm/views/user.py	Thu Dec 10 13:44:01 2009 +0100
+++ b/src/cm/views/user.py	Thu Dec 10 14:27:12 2009 +0100
@@ -22,6 +22,7 @@
 from cm.views import get_keys_from_dict
 from cm.security import has_global_perm
 from cm.exception import UnauthorizedException
+from tagging.models import Tag
 import sys
 import re
 
@@ -30,6 +31,7 @@
 @has_global_perm('can_manage_workspace')
 def user_list(request):    
     display_suspended_users = get_int(request.GET, 'display', 0)
+    tag_selected = request.GET.get('tag_selected', 0)
     paginate_by = get_int(request.GET, 'paginate', USER_PAGINATION)
     order_by = get_among(request.GET, 'order', ('user__username',
                                               'user__email',
@@ -105,12 +107,26 @@
                'all_roles' : Role.objects.all(),
                'anon_roles' : Role.objects.filter(anon=True),
                'display_suspended_users' : display_suspended_users,
+               'tag_list' : Tag.objects.usage_for_model(UserProfile),
+               'tag_selected': tag_selected,
                }
     
-    query = UserRole.objects.filter(text=None).filter(~Q(user=None)).order_by(order_by)
+    query = UserRole.objects.select_related().filter(text=None).filter(~Q(user=None)).order_by(order_by)
     if not display_suspended_users:
         query = query.exclude(Q(user__userprofile__is_suspended=True) & Q(user__is_active=True))
-        
+
+    if tag_selected:     
+        tag_ids = Tag.objects.filter(name=tag_selected)
+        if tag_ids:   
+            content_type_id = ContentType.objects.get_for_model(UserProfile).pk
+            # table cm_userprofile is not present if display_suspended_users: fix this 
+            tables = ['tagging_taggeditem', 'cm_userprofile'] if display_suspended_users else ['tagging_taggeditem']  
+            query = query.extra(where=['tagging_taggeditem.object_id = cm_userprofile.id', 
+                                       'tagging_taggeditem.content_type_id = %i' %content_type_id,
+                                       'tagging_taggeditem.tag_id = %i' %tag_ids[0].id],
+                                tables=tables,
+                                )
+
     return object_list(request, query,
                        template_name='site/user_list.html',
                        paginate_by=paginate_by,
@@ -183,7 +199,7 @@
 class UserProfileForm(ModelForm):
     class Meta:
         model = UserProfile
-        fields = ('allow_contact', 'preferred_language', 'is_suspended')
+        fields = ('allow_contact', 'preferred_language', 'is_suspended', 'tags')
 
 class MyUserProfileForm(ModelForm):
     class Meta:
@@ -193,7 +209,7 @@
 class UserProfileAddForm(ModelForm):
     class Meta:
         model = UserProfile
-        fields = ('preferred_language',)
+        fields = ('preferred_language', 'tags')
 
 class UserAddForm(forms.Form):
     note = forms.CharField(label=ugettext_lazy(u'Note'),