Commit 1576292dc0fb1bbea8a873aad30228f171627558

Authored by Sergio Oliveira
1 parent 5e783469

Replacing user profile view by CBV

src/accounts/templates/accounts/user-profile.html
@@ -7,51 +7,44 @@ @@ -7,51 +7,44 @@
7 {% endblock %} 7 {% endblock %}
8 8
9 {% block main-content %} 9 {% block main-content %}
10 - {% if not user_profile %}  
11 - <div class="alert alert-info alert-dismissable">  
12 - <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>  
13 - <strong>{% trans "User not registered." %}</strong> {% trans "Is that you?" %}  
14 - <a href="{% url 'signup' %}">{% trans "Click here and sign up." %}</a>  
15 - </div>  
16 - {% endif %}  
17 10
18 <div id="user-profile" class="row"> 11 <div id="user-profile" class="row">
19 <div class="vcard col-lg-3 col-md-3 col-sm-4"> 12 <div class="vcard col-lg-3 col-md-3 col-sm-4">
20 <div class="thumbnail"> 13 <div class="thumbnail">
21 - {% gravatar email_address.address 200 %} 14 + {% gravatar user_.email 200 %}
22 </div> 15 </div>
23 16
24 <h1> 17 <h1>
25 - <span>{{ email_address.get_full_name }}</span>  
26 - <em>{{ email_address.user.username }}</em> 18 + <span>{{ user_.get_full_name }}</span>
  19 + <em>{{ user_.username }}</em>
27 </h1> 20 </h1>
28 21
29 - {% ifequal request.user user_profile.user %}  
30 - <a class="btn btn-info" href="{% url 'user_profile_update' request.user %}"><span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;{% trans "update your profile"|title %}</a> 22 + {% ifequal request.user user_ %}
  23 + <a class="btn btn-info" href="{% url 'user_profile_update' user_ %}"><span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;{% trans "update your profile"|title %}</a>
31 {% endifequal %} 24 {% endifequal %}
32 25
33 <div class="divider"></div> 26 <div class="divider"></div>
34 27
35 <ul class="unstyled-list"> 28 <ul class="unstyled-list">
36 - {% if user_profile.institution or user_profile.role %} 29 + {% if user_.profile.institution or user_.profile.role %}
37 <li> 30 <li>
38 <span class="icon-briefcase icon-fixed-width"></span> 31 <span class="icon-briefcase icon-fixed-width"></span>
39 - {{ user_profile.role }}  
40 - {% if user_profile.institution and user_profile.role %}-{% endif %}  
41 - {{ user_profile.institution }} 32 + {{ user_.profile.role }}
  33 + {% if user_.profile.institution and user_.profile.role %}-{% endif %}
  34 + {{ user_.profile.institution }}
42 </li> 35 </li>
43 {% endif %} 36 {% endif %}
44 - {% if user_profile.twitter %}  
45 - <li><span class="icon-twitter icon-fixed-width"></span> {{ user_profile.twitter }}</li> 37 + {% if user_.profile.twitter %}
  38 + <li><span class="icon-twitter icon-fixed-width"></span> {{ user_.profile.twitter }}</li>
46 {% endif %} 39 {% endif %}
47 - {% if user_profile.facebook %}  
48 - <li><span class="icon-facebook icon-fixed-width"></span> {{ user_profile.facebook }}</li> 40 + {% if user_.profile.facebook %}
  41 + <li><span class="icon-facebook icon-fixed-width"></span> {{ user_.profile.facebook }}</li>
49 {% endif %} 42 {% endif %}
50 - {% if user_profile.google_talk %}  
51 - <li><span class="icon-google-plus icon-fixed-width"></span> {{ user_profile.google_talk }}</li> 43 + {% if user_.profile.google_talk %}
  44 + <li><span class="icon-google-plus icon-fixed-width"></span> {{ user_.profile.google_talk }}</li>
52 {% endif %} 45 {% endif %}
53 - {% if user_profile.webpage %}  
54 - <li><span class="icon-link icon-fixed-width"></span> {{ user_profile.webpage }}</li> 46 + {% if user_.profile.webpage %}
  47 + <li><span class="icon-link icon-fixed-width"></span> {{ user_.profile.webpage }}</li>
55 {% endif %} 48 {% endif %}
56 </ul> 49 </ul>
57 </div> 50 </div>
src/accounts/urls.py
1 1
2 from django.conf.urls import patterns, include, url 2 from django.conf.urls import patterns, include, url
3 3
  4 +from .views import UserProfileDetailView
4 5
5 -urlpatterns = patterns('',  
6 6
  7 +urlpatterns = patterns('',
7 url(r'^$', 'accounts.views.signup', name='signup'), 8 url(r'^$', 'accounts.views.signup', name='signup'),
8 9
9 url(r'^verify/(?P<hash>[\w]{32})/$', 10 url(r'^verify/(?P<hash>[\w]{32})/$',
10 'accounts.views.verify_email', name='email_verification'), 11 'accounts.views.verify_email', name='email_verification'),
11 12
12 - # TODO: review and redo those weird views from  
13 - # colab.deprecated.views.userprofile moving them to accounts.views  
14 - url(r'^user/(?P<username>[\w@+.-]+)/?$',  
15 - 'colab.deprecated.views.userprofile.by_username', name='user_profile'),  
16 -  
17 - url(r'^user/$', 'colab.deprecated.views.userprofile.by_request_user',  
18 - name='user_profile_by_request_user'),  
19 -  
20 - url(r'^user/hash/(?P<emailhash>[\w]+)$',  
21 - 'colab.deprecated.views.userprofile.by_emailhash'), 13 + url(r'^(?P<username>[\w@+.-]+)/?$',
  14 + UserProfileDetailView.as_view(), name='user_profile'),
22 15
23 - url(r'^user/(?P<username>[\w@+.-]+)/edit/?$',  
24 - 'colab.deprecated.views.userprofile.update', name='user_profile_update'), 16 + #url(r'^user/(?P<username>[\w@+.-]+)/edit/?$',
  17 + # 'colab.deprecated.views.userprofile.update', name='user_profile_update'),
25 ) 18 )
src/accounts/views.py
@@ -6,11 +6,15 @@ from colab.deprecated import signup as signup_ @@ -6,11 +6,15 @@ from colab.deprecated import signup as signup_
6 6
7 from django.template import RequestContext 7 from django.template import RequestContext
8 from django.contrib.auth.models import User 8 from django.contrib.auth.models import User
  9 +from django.views.generic import DetailView
9 from django.utils.translation import ugettext as _ 10 from django.utils.translation import ugettext as _
10 from django.shortcuts import render, get_object_or_404 11 from django.shortcuts import render, get_object_or_404
11 12
  13 +from colab.deprecated import solrutils
  14 +
12 from .forms import UserCreationForm 15 from .forms import UserCreationForm
13 -from super_archives.models import UserProfile, EmailAddress 16 +from super_archives.models import UserProfile, EmailAddress, Message
  17 +
14 18
15 # helper 19 # helper
16 def get_field_set(form): 20 def get_field_set(form):
@@ -109,3 +113,33 @@ def verify_email(request, hash): @@ -109,3 +113,33 @@ def verify_email(request, hash):
109 } 113 }
110 114
111 return render(request, 'accounts/account_message.html', template_data) 115 return render(request, 'accounts/account_message.html', template_data)
  116 +
  117 +
  118 +class UserProfileDetailView(DetailView):
  119 + model = User
  120 + slug_field = 'username'
  121 + slug_url_kwarg = 'username'
  122 + context_object_name = 'user_'
  123 + template_name = 'accounts/user-profile.html'
  124 +
  125 + def get_context_data(self, **kwargs):
  126 + user = self.object
  127 + context = {}
  128 +
  129 + # TODO: Use haystack instead of solrutils
  130 + context['type_count'] = solrutils.count_types(
  131 + filters={'collaborator': user.username}
  132 + )
  133 +
  134 + # TODO: Use haystack instead of solrutils
  135 + context['docs'] = solrutils.get_latest_collaborations(
  136 + username=user.username
  137 + )
  138 +
  139 + email_pks = [addr.pk for addr in user.emails.iterator()]
  140 + query = Message.objects.filter(from_address__in=email_pks)
  141 + query = query.order_by('-received_time')
  142 + context['emails'] = query[:10]
  143 +
  144 + return super(UserProfileDetailView, self).get_context_data(**context)
  145 +
src/colab/deprecated/views/userprofile.py
@@ -6,7 +6,6 @@ userprofile.py @@ -6,7 +6,6 @@ userprofile.py
6 Created by Sergio Campos on 2012-01-10. 6 Created by Sergio Campos on 2012-01-10.
7 """ 7 """
8 8
9 -from django.template import RequestContext  
10 from django.contrib.auth.models import User 9 from django.contrib.auth.models import User
11 from django.forms.models import model_to_dict 10 from django.forms.models import model_to_dict
12 from django.contrib.auth.decorators import login_required 11 from django.contrib.auth.decorators import login_required
@@ -17,80 +16,6 @@ from accounts.forms import UserCreationForm, UserUpdateForm @@ -17,80 +16,6 @@ from accounts.forms import UserCreationForm, UserUpdateForm
17 from super_archives.models import Message, UserProfile, EmailAddress 16 from super_archives.models import Message, UserProfile, EmailAddress
18 17
19 18
20 -def read(request, user, email_address=None, editable=False, form=None):  
21 -  
22 - if form is None:  
23 - form = UserCreationForm()  
24 -  
25 - if user:  
26 - email_addresses = user.emails.all()  
27 - profile = user.profile  
28 - last_modified_docs = solrutils.get_latest_collaborations(  
29 - username=user.username  
30 - )  
31 -  
32 - type_count = solrutils.count_types(  
33 - filters={'collaborator': user.username}  
34 - )  
35 -  
36 - else:  
37 - email_addresses = [email_address]  
38 - profile = None  
39 - last_modified_docs = []  
40 - type_count = {}  
41 -  
42 - if not email_address and email_addresses:  
43 - email_address = email_addresses[0]  
44 -  
45 - email_addresses_ids = tuple([str(addr.id) for addr in email_addresses])  
46 -  
47 - query = """  
48 - SELECT  
49 - *  
50 - FROM  
51 - super_archives_message JOIN (  
52 - SELECT id  
53 - FROM super_archives_message  
54 - WHERE from_address_id IN (%(ids)s)  
55 - GROUP BY thread_id, id  
56 - ) AS subquery  
57 - ON subquery.id = super_archives_message.id  
58 - ORDER BY  
59 - received_time DESC  
60 - LIMIT 10;  
61 -  
62 - """ % {'ids': ','.join(email_addresses_ids)}  
63 -  
64 - emails = Message.objects.raw(query)  
65 - #n_sent = Message.objects.filter(from_address__in=email_addresses).count()  
66 -  
67 - template_data = {  
68 - 'user_profile': profile,  
69 - 'email_address': email_address,  
70 - 'emails': emails or [],  
71 - 'form': form,  
72 - 'editable': editable,  
73 - 'type_count': type_count,  
74 - 'docs': last_modified_docs,  
75 - }  
76 -  
77 - return render(request, 'accounts/user-profile.html', template_data)  
78 -  
79 -  
80 -@login_required  
81 -def by_request_user(request):  
82 - return read(request, request.user)  
83 -  
84 -  
85 -def by_username(request, username):  
86 - user = get_object_or_404(User, username=username)  
87 - return read(request, user)  
88 -  
89 -  
90 -def by_emailhash(request, emailhash):  
91 - email_addr = get_object_or_404(EmailAddress, md5=emailhash)  
92 - return read(request, email_addr.user, email_addr)  
93 -  
94 19
95 @login_required 20 @login_required
96 def update(request, username): 21 def update(request, username):