Commit 33262020 by Kálmán Viktor

dashboard: use gravatar avatar option

parent 8e6400ed
...@@ -26,7 +26,7 @@ from django.contrib.auth.signals import user_logged_in ...@@ -26,7 +26,7 @@ from django.contrib.auth.signals import user_logged_in
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db.models import ( from django.db.models import (
Model, ForeignKey, OneToOneField, CharField, IntegerField, TextField, Model, ForeignKey, OneToOneField, CharField, IntegerField, TextField,
DateTimeField, permalink, DateTimeField, permalink, BooleanField
) )
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _, override, ugettext from django.utils.translation import ugettext_lazy as _, override, ugettext
...@@ -83,6 +83,7 @@ class Profile(Model): ...@@ -83,6 +83,7 @@ class Profile(Model):
unique=True, blank=True, null=True, max_length=64, unique=True, blank=True, null=True, max_length=64,
help_text=_('Unique identifier of the person, e.g. a student number.')) help_text=_('Unique identifier of the person, e.g. a student number.'))
instance_limit = IntegerField(default=5) instance_limit = IntegerField(default=5)
use_gravatar = BooleanField(default=False)
def notify(self, subject, template, context={}, valid_until=None): def notify(self, subject, template, context={}, valid_until=None):
return Notification.send(self.user, subject, template, context, return Notification.send(self.user, subject, template, context,
......
...@@ -672,3 +672,7 @@ textarea[name="list-new-namelist"] { ...@@ -672,3 +672,7 @@ textarea[name="list-new-namelist"] {
color: #555 !important; color: #555 !important;
text-decoration: none; text-decoration: none;
} }
#dashboard-profile-avatar {
max-width: 200px;
}
$(function() {
// change user avatar
$("#dashboard-profile-use-gravatar").click(function() {
var checked = $(this).prop("checked");
var user = $(this).data("user");
$.ajax({
type: 'POST',
url:"/dashboard/profile/" + user + "/use_gravatar/",
headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(re) {
if(re.new_avatar_url) {
$("#dashboard-profile-avatar").prop("src", re.new_avatar_url);
}
},
error: function(xhr, textStatus, error) {
if(xhr.status == 403) {
addMessage(gettext("You have no permission to change this profile."), "danger");
} else {
addMessage(gettext("Unknown error."), "danger");
}
}
});
});
});
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<div class="panel-body"> <div class="panel-body">
<div> <div>
<div class="" style="float: left"> <div class="" style="float: left">
<img src="{{ avatar_url }}?s=200" class="img-rounded"/> <img id="dashboard-profile-avatar" src="{{ avatar_url }}" class="img-rounded"/>
</div> </div>
<div class="" style="padding-left: 215px;"> <div class="" style="padding-left: 215px;">
<p>Username: {{ profile.username }}</p> <p>Username: {{ profile.username }}</p>
...@@ -27,10 +27,14 @@ ...@@ -27,10 +27,14 @@
<p>First name: {{ profile.first_name|default:"-" }}</p> <p>First name: {{ profile.first_name|default:"-" }}</p>
<p>Last name: {{ profile.last_name|default:"-" }}</p> <p>Last name: {{ profile.last_name|default:"-" }}</p>
<p>Email address: {{ profile.email }}</p> <p>Email address: {{ profile.email }}</p>
{% if request.user == profile %}
<p> <p>
Use email address as Gravatar profile image: Use email address as Gravatar profile image:
<input type="checkbox"/> <a href="https://gravatar.com">What's Gravatar?</a> <input id="dashboard-profile-use-gravatar" data-user="{{ profile.pk }}"
{% if profile.profile.use_gravatar %}checked="checked"{% endif %}
type="checkbox"/> <a href="https://gravatar.com">What's Gravatar?</a>
</p> </p>
{% endif %}
</div> </div>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
...@@ -96,3 +100,7 @@ ...@@ -96,3 +100,7 @@
</div> </div>
{% endblock %} {% endblock %}
{% block extra_js %}
<script src="{{ STATIC_URL }}dashboard/profile.js"></script>
{% endblock %}
...@@ -34,7 +34,7 @@ from .views import ( ...@@ -34,7 +34,7 @@ from .views import (
GroupCreate, GroupCreate,
TemplateChoose, TemplateChoose,
UserCreationView, UserCreationView,
ProfileView, ProfileView, toggle_use_gravatar,
) )
urlpatterns = patterns( urlpatterns = patterns(
...@@ -133,8 +133,9 @@ urlpatterns = patterns( ...@@ -133,8 +133,9 @@ urlpatterns = patterns(
url(r'^interface/(?P<pk>\d+)/delete/$', InterfaceDeleteView.as_view(), url(r'^interface/(?P<pk>\d+)/delete/$', InterfaceDeleteView.as_view(),
name="dashboard.views.interface-delete"), name="dashboard.views.interface-delete"),
url(r'^profile/(?P<pk>\d+)$', ProfileView.as_view(), url(r'^profile/(?P<pk>\d+)/$', ProfileView.as_view(),
name="dashboard.views.profile"), name="dashboard.views.profile"),
url(r'^profile/(?P<pk>\d+)/use_gravatar/$', toggle_use_gravatar),
url(r'^group/(?P<group_pk>\d+)/remove/acl/user/(?P<member_pk>\d+)/$', url(r'^group/(?P<group_pk>\d+)/remove/acl/user/(?P<member_pk>\d+)/$',
GroupRemoveAclUserView.as_view(), GroupRemoveAclUserView.as_view(),
......
...@@ -36,7 +36,7 @@ from django.core.urlresolvers import reverse, reverse_lazy ...@@ -36,7 +36,7 @@ from django.core.urlresolvers import reverse, reverse_lazy
from django.db.models import Count from django.db.models import Count
from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import redirect, render, get_object_or_404 from django.shortcuts import redirect, render, get_object_or_404
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET, require_POST
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
from django.views.generic import (TemplateView, DetailView, View, DeleteView, from django.views.generic import (TemplateView, DetailView, View, DeleteView,
UpdateView, CreateView, ListView) UpdateView, CreateView, ListView)
...@@ -46,6 +46,7 @@ from django.utils.translation import ungettext as __ ...@@ -46,6 +46,7 @@ from django.utils.translation import ungettext as __
from django.template.defaultfilters import title as title_filter from django.template.defaultfilters import title as title_filter
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.template import RequestContext from django.template import RequestContext
from django.templatetags.static import static
from django.forms.models import inlineformset_factory from django.forms.models import inlineformset_factory
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
...@@ -2652,14 +2653,14 @@ class InterfaceDeleteView(DeleteView): ...@@ -2652,14 +2653,14 @@ class InterfaceDeleteView(DeleteView):
self.object.instance.get_absolute_url() self.object.instance.get_absolute_url()
class ProfileView(DetailView): class ProfileView(LoginRequiredMixin, DetailView):
template_name = "dashboard/profile.html" template_name = "dashboard/profile.html"
model = User model = User
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(ProfileView, self).get_context_data(**kwargs) context = super(ProfileView, self).get_context_data(**kwargs)
context['profile'] = self.get_object() context['profile'] = self.get_object()
context['avatar_url'] = self.get_avatar_url() context['avatar_url'] = get_user_avatar_url(context['profile'])
context['instances_owned'] = Instance.get_objects_with_level( context['instances_owned'] = Instance.get_objects_with_level(
"owner", self.get_object(), disregard_superuser=True "owner", self.get_object(), disregard_superuser=True
).filter(destroyed_at=None) ).filter(destroyed_at=None)
...@@ -2694,7 +2695,27 @@ class ProfileView(DetailView): ...@@ -2694,7 +2695,27 @@ class ProfileView(DetailView):
'instances_with_access'].filter(template__in=it) 'instances_with_access'].filter(template__in=it)
return context return context
def get_avatar_url(self):
user = self.get_object() @require_POST
def toggle_use_gravatar(request, **kwargs):
user = get_object_or_404(User, pk=kwargs['pk'])
if not request.user == user:
raise PermissionDenied()
profile = user.profile
profile.use_gravatar = not profile.use_gravatar
profile.save()
new_avatar_url = get_user_avatar_url(user)
return HttpResponse(
json.dumps({'new_avatar_url': new_avatar_url}),
content_type="application/json",
)
def get_user_avatar_url(user):
if user.profile.use_gravatar:
gravatar_hash = md5(user.email).hexdigest() gravatar_hash = md5(user.email).hexdigest()
return "https://gravatar.com/avatar/%s" % gravatar_hash return "https://secure.gravatar.com/avatar/%s?s=200" % gravatar_hash
else:
return static("dashboard/img/avatar.png")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment