Skip to content

Commit

Permalink
add avatar for user
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuanshu committed Sep 20, 2016
1 parent a8a5e7e commit ccafc4f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions checkinsystem/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
Expand Down
9 changes: 8 additions & 1 deletion checkinsystem/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url,include
from django.conf.urls import url, include, patterns
from django.conf import settings

urlpatterns = [
url(r'^$', 'center.views.home', name='home'),
Expand All @@ -26,3 +27,9 @@
url(r'^rbac/', include('rbac.urls', namespace="rbac")),
url(r'^seat$', 'center.views.seat', name='seat'),
]

if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
1 change: 1 addition & 0 deletions media/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
upload dir
File renamed without changes
8 changes: 4 additions & 4 deletions templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ <h4>
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- The user image in the navbar-->
<img src="{% static 'img/avatar5.png' %}" class="user-image" alt="User Image">
<img src="{{ request.user.avatar.url }}" class="user-image" alt="User Image">
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<span class="hidden-xs">{{ request.user.username }}</span>
</a>
<ul class="dropdown-menu">
<!-- The user image in the menu -->
<li class="user-header">
<img src="{% static 'img/avatar5.png' %}" class="img-circle" alt="User Image">
<img src="{{ request.user.avatar.url }}" class="img-circle" alt="User Image">

<p>
1234213421342134123
<small>1234213412412342134</small>
{{ request.user.username }}
<small>{{ request.user.email }}</small>
</p>
</li>
<!-- Menu Body -->
Expand Down
14 changes: 14 additions & 0 deletions user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class User(models.Model):
email = models.CharField(max_length=40, blank=True, null=True)
verify = models.NullBooleanField()
role = models.ManyToManyField(Role, through='Usertorole', through_fields=('user', 'role'))
avatar = models.ImageField(upload_to='avatar', default='avatar/default.png')

@classmethod_cache
def isteacher(self):
Expand All @@ -43,6 +44,19 @@ def hasresourcejurisdiction(self, jurisdiction):
from rbac.auth import is_user_has_resourcejurisdiction
return is_user_has_resourcejurisdiction(self, jurisdiction)

def updateavatarfromwechat(self):
if self.openid is not None:
from wechat.api import wechat
userinfo = wechat.get_user_info(self.openid, lang='zh_CN')
avatar_url = userinfo['headimgurl']
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
import urllib2
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(urllib2.urlopen(avatar_url).read())
img_temp.flush()
self.avatar.save('%s.jpeg' % self.openid, File(img_temp))

def __unicode__(self):
return u"%s" % (self.username)

Expand Down
16 changes: 16 additions & 0 deletions wechat/management/commands/updateavatarforalluser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError
from user.models import User
from wechat_sdk.exceptions import OfficialAPIError

class Command(BaseCommand):
def handle(self, *args, **options):
counter=0
users=User.objects.exclude(openid=None).all()
for u in users:
try:
u.updateavatarfromwechat()
except OfficialAPIError:
pass
counter+=1
print "update %d users"%counter

0 comments on commit ccafc4f

Please sign in to comment.