project/scipycon/user/utils.py
changeset 94 87e77aa18610
child 96 178b89a3ca4f
equal deleted inserted replaced
93:e86755df35da 94:87e77aa18610
       
     1 # -*- coding: utf-8 -*-
       
     2 from __future__ import absolute_import
       
     3 
       
     4 #python imports
       
     5 import os
       
     6 
       
     7 #django
       
     8 from django.conf import settings
       
     9 from django.core.exceptions import ObjectDoesNotExist
       
    10 
       
    11 #django.contrib
       
    12 from django.contrib.auth.models import User
       
    13 
       
    14 #PIL
       
    15 from PIL import Image
       
    16 
       
    17 
       
    18 def kiwipycon_createregistrant(request, data):
       
    19     """Create user"""
       
    20     email = data.get("email")
       
    21     name = data.get("name")
       
    22     username = data.get("username")
       
    23 
       
    24     n = name.split(" ")
       
    25     if len(n) > 1:
       
    26         first_name = ' '.join(n[:-1])
       
    27         last_name = n[-1]
       
    28     else:
       
    29         first_name = ''
       
    30         last_name = n[0]
       
    31 
       
    32 
       
    33     # Create user
       
    34     user = User.objects.create_user(username=username, email=email)
       
    35     user.first_name = first_name
       
    36     user.last_name = last_name
       
    37     user.save()
       
    38 
       
    39     return user
       
    40 
       
    41 def kiwipycon_createuser(request, data):
       
    42     """Create user"""
       
    43     email = data.get("email")
       
    44     username = data.get("username")
       
    45     password = data.get("password_1")
       
    46     password = data.get("password_1")
       
    47 
       
    48     # Create user
       
    49     user = User.objects.create_user(
       
    50         username=username, email=email, password=password)
       
    51     user.first_name = data.get("first_name")
       
    52     user.last_name = data.get("last_name")
       
    53     user.save()
       
    54 
       
    55     # Log in user
       
    56     from django.contrib.auth import authenticate
       
    57     user = authenticate(username=username, password=password)
       
    58 
       
    59     from django.contrib.auth import login
       
    60     login(request, user)
       
    61 
       
    62     profile = user.get_profile()
       
    63     photo = request.FILES.get('photo', None)
       
    64     filename= None
       
    65     if photo:
       
    66         filename = handle_uploaded_photo(user, request.FILES['photo'])
       
    67     if filename:
       
    68         profile.photo = filename
       
    69     #print photo, filename
       
    70 
       
    71     profile.url = data.get("url")
       
    72     profile.about = data.get("about")
       
    73     profile.save()
       
    74 
       
    75     return user
       
    76 
       
    77 def handle_uploaded_photo(user, ufile):
       
    78     usermedia = settings.USER_MEDIA_ROOT
       
    79     filename = ufile.name
       
    80     ext = filename.split('.')[-1]
       
    81     filesize = ufile.size
       
    82     filecontent = ufile.read()
       
    83     userfilename = 'user-%d.%s' % (user.id, ext)
       
    84     if not filecontent:
       
    85         return None
       
    86 
       
    87     #save
       
    88     foutname = os.path.join(usermedia, userfilename)
       
    89 
       
    90     fout = file(foutname, 'wb')
       
    91     fout.write(filecontent)
       
    92     fout.close()
       
    93 
       
    94     # crop and resize
       
    95     image = Image.open(foutname)
       
    96     pw = image.size[0]
       
    97     ph = image.size[1]
       
    98     nw = nh = 80
       
    99     if (pw, ph) != (nw, nh):
       
   100         pr = float(pw) / float(ph)
       
   101         nr = float(nw) / float(nh)
       
   102 
       
   103         if pr > nr:
       
   104             # photo aspect is wider than destination ratio
       
   105             tw = int(round(nh * pr))
       
   106             image = image.resize((tw, nh), Image.ANTIALIAS)
       
   107             l = int(round(( tw - nw ) / 2.0))
       
   108             image = image.crop((l, 0, l + nw, nh))
       
   109         elif pr < nr:
       
   110             # photo aspect is taller than destination ratio
       
   111             th = int(round(nw / pr))
       
   112             image = image.resize((nw, th), Image.ANTIALIAS)
       
   113             t = int(round(( th - nh ) / 2.0))
       
   114             #print((0, t, nw, t + nh))
       
   115             image = image.crop((0, t, nw, t + nh))
       
   116         else:
       
   117             # photo aspect matches the destination ratio
       
   118             image = image.resize((nw, nh), Image.ANTIALIAS)
       
   119 
       
   120         image.save(str(foutname))
       
   121     return userfilename
       
   122