project/scipycon/user/utils.py
author primal primal007@gmail.com
Mon, 05 Dec 2011 10:40:37 +0530
branch2011
changeset 510 a7e85cdc6ed0
parent 143 ea9477690acd
permissions -rw-r--r--
day 1 blog and schedlue correcte
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
import os
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
from django.conf import settings
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
from django.core.exceptions import ObjectDoesNotExist
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
from django.contrib.auth.models import User
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
from PIL import Image
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
143
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
     9
from project.scipycon.base.models import Event
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    10
from project.scipycon.user.models import UserProfile
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    11
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
143
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    13
def scipycon_createregistrant(request, data, scope):
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    14
    """Create user
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    15
    """
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    17
    email = data.get('email')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    18
    name = data.get('name')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    19
    username = data.get('username')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    20
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    21
    n = name.split(' ')
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
    if len(n) > 1:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
        first_name = ' '.join(n[:-1])
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
        last_name = n[-1]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
    else:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
        first_name = ''
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
        last_name = n[0]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
    # Create user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
    user = User.objects.create_user(username=username, email=email)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
    user.first_name = first_name
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
    user.last_name = last_name
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
    user.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
143
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    36
    scope_entity = Event.objects.get(scope=scope)
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    37
    try:
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    38
        profile = user.get_profile()
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    39
    except:
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    40
        profile, new = UserProfile.objects.get_or_create(
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    41
            user=user, scope=scope_entity)
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    42
        profile.save()
ea9477690acd Added creation of profile to user creation since signals were removed.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 138
diff changeset
    43
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
    return user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    46
def scipycon_createuser(request, data, scope):
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    47
    """Create user
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    48
    """
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    49
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    50
    from django.contrib.auth import authenticate
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    51
    from django.contrib.auth import login
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    52
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    53
    email = data.get('email')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    54
    username = data.get('username')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    55
    password = data.get('password_1')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    56
    password = data.get('password_1')
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
    # Create user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
    user = User.objects.create_user(
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
        username=username, email=email, password=password)
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    61
    user.first_name = data.get('first_name')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    62
    user.last_name = data.get('last_name')
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
    user.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
    # Log in user
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    66
    
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
    user = authenticate(username=username, password=password)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
    login(request, user)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    71
    scope_entity = Event.objects.get(scope=scope)
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    72
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    73
    try:
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    74
        profile = user.get_profile()
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    75
    except:
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    76
        profile, new = UserProfile.objects.get_or_create(
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    77
            user=user, scope=scope_entity)
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    78
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
    photo = request.FILES.get('photo', None)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
    filename= None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
    if photo:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
        filename = handle_uploaded_photo(user, request.FILES['photo'])
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
    if filename:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
        profile.photo = filename
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
128
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    86
    profile.url = data.get('url')
d804be977989 Add scope as a parameter to all view functions for user app and adjusted the URLs to accommodate scope.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 96
diff changeset
    87
    profile.about = data.get('about')
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
    profile.save()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
    return user
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
def handle_uploaded_photo(user, ufile):
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    93
    """Handles the upload and gives the file path to be saved.
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    94
    """
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    95
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
    usermedia = settings.USER_MEDIA_ROOT
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
    filename = ufile.name
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
    ext = filename.split('.')[-1]
138
250e2731039d Reformatted the file and imports.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 128
diff changeset
    99
94
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
    filecontent = ufile.read()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
    userfilename = 'user-%d.%s' % (user.id, ext)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
    if not filecontent:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
        return None
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
    #save
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
    foutname = os.path.join(usermedia, userfilename)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
    fout = file(foutname, 'wb')
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
    fout.write(filecontent)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   110
    fout.close()
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   112
    # crop and resize
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
    image = Image.open(foutname)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
    pw = image.size[0]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   115
    ph = image.size[1]
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
    nw = nh = 80
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
    if (pw, ph) != (nw, nh):
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
        pr = float(pw) / float(ph)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
        nr = float(nw) / float(nh)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
        if pr > nr:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
            # photo aspect is wider than destination ratio
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
            tw = int(round(nh * pr))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
            image = image.resize((tw, nh), Image.ANTIALIAS)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
            l = int(round(( tw - nw ) / 2.0))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   126
            image = image.crop((l, 0, l + nw, nh))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   127
        elif pr < nr:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   128
            # photo aspect is taller than destination ratio
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
            th = int(round(nw / pr))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   130
            image = image.resize((nw, th), Image.ANTIALIAS)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
            t = int(round(( th - nh ) / 2.0))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   132
            image = image.crop((0, t, nw, t + nh))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   133
        else:
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
            # photo aspect matches the destination ratio
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
            image = image.resize((nw, nh), Image.ANTIALIAS)
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   137
        image.save(str(foutname))
87e77aa18610 Moved the files to new Django app named scipycon and modified settings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   138
    return userfilename