project/kiwipycon/user/views.py
changeset 96 3ece0de11641
parent 94 e86755df35da
child 101 fb27be005a5c
equal deleted inserted replaced
95:34e28994fec5 96:3ece0de11641
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 from __future__ import absolute_import
     2 from __future__ import absolute_import
     3 
     3 
     4 #python
     4 #python
     5 from urlparse import urlparse
     5 from urlparse import urlparse
     6 import json
       
     7 import urllib
       
     8 import os
     6 import os
     9 
     7 
    10 #django
     8 #django
    11 from django.conf import settings
     9 from django.conf import settings
    12 from django.db.models import Q
       
    13 from django.http import HttpResponse
       
    14 from django.shortcuts import render_to_response
       
    15 from django.template import RequestContext
       
    16 from django.core.urlresolvers import reverse
       
    17 from django.db.models.signals import post_save
       
    18 
       
    19 #django.contrib
       
    20 from django.contrib.auth.decorators import login_required
    10 from django.contrib.auth.decorators import login_required
    21 from django.contrib.auth.forms import AuthenticationForm
    11 from django.contrib.auth.forms import AuthenticationForm
    22 from django.contrib.auth.forms import PasswordChangeForm
    12 from django.contrib.auth.forms import PasswordChangeForm
    23 from django.contrib.auth.models import User
    13 from django.contrib.auth.models import User
    24 from django.core.exceptions import ObjectDoesNotExist
    14 from django.core.exceptions import ObjectDoesNotExist
       
    15 from django.core.urlresolvers import reverse
       
    16 from django.db.models import Q
       
    17 from django.http import HttpResponse
       
    18 from django.shortcuts import render_to_response
       
    19 from django.template import RequestContext
       
    20 from django.utils import simplejson as json
    25 
    21 
    26 #PIL
    22 #PIL
    27 from PIL import Image
    23 from PIL import Image
    28 
    24 
    29 #kiwipycon
    25 #kiwipycon
    30 from project.kiwipycon.utils import set_message_cookie
    26 from project.kiwipycon.utils import set_message_cookie
    31 from project.kiwipycon.talk.models import Talk
    27 from project.kiwipycon.talk.models import Talk
    32 from project.kiwipycon.registration.models import Registration
    28 from project.kiwipycon.registration.models import Registration
    33 from project.kiwipycon.registration.models import Wifi
    29 from project.kiwipycon.registration.models import Wifi
    34 from project.kiwipycon.registration.forms import WifiForm
    30 from project.kiwipycon.registration.forms import WifiForm
    35 from project.kiwipycon.sponsor.models import Sponsor
       
    36 
    31 
    37 from .utils import kiwipycon_createuser
    32 from .utils import kiwipycon_createuser
    38 from .utils import handle_uploaded_photo
    33 from .utils import handle_uploaded_photo
    39 from .forms import RegisterForm
    34 from .forms import RegisterForm
    40 from .forms import EditProfileForm
    35 from .forms import EditProfileForm
    59 
    54 
    60     if profile.photo:
    55     if profile.photo:
    61         photo = os.path.join(settings.USER_MEDIA_URL, profile.photo)
    56         photo = os.path.join(settings.USER_MEDIA_URL, profile.photo)
    62     else:
    57     else:
    63         photo = '/img/user-default.png'
    58         photo = '/img/user-default.png'
    64 
       
    65     qstring = ""
       
    66 
    59 
    67     wifi_comment = None
    60     wifi_comment = None
    68     if wifiobj:
    61     if wifiobj:
    69         wifi_form = False
    62         wifi_form = False
    70     else:
    63     else:
   246     """Returns in json the list of ten possible usernames
   239     """Returns in json the list of ten possible usernames
   247     starting with the last pattern in the comma separated string
   240     starting with the last pattern in the comma separated string
   248     """
   241     """
   249 
   242 
   250     get_params = request.GET
   243     get_params = request.GET
   251     authors_str = get_params.get('input')
   244     search_author = get_params.get('q')
   252 
   245 
   253     if not authors_str:
   246     if search_author:
       
   247         search_author = search_author.strip()
       
   248     else:
   254         return HttpResponse(json.dumps(''))
   249         return HttpResponse(json.dumps(''))
   255 
   250     
   256     authors = authors_str.split(',')
       
   257     search_author = authors[-1].strip()
       
   258 
   251 
   259     users = User.objects.filter(
   252     users = User.objects.filter(
   260         Q(username__istartswith=search_author) | Q(
   253         Q(username__istartswith=search_author) | Q(
   261         first_name__istartswith=search_author) | Q(
   254         first_name__istartswith=search_author) | Q(
   262         last_name__istartswith=search_author))
   255         last_name__istartswith=search_author))
   263 
   256 
   264     results = [{'id': '',
   257     results = []
   265                 'info': 'plugin_header',
       
   266                 'value': 'User Names'
       
   267               }]
       
   268     
   258     
   269     for user in users:
   259     for user in users:
   270         results.append(
   260         results.append(
   271             {'id': 'author_name',
   261             {'name': str(user.username),
   272              'info': str(user.get_full_name()),
   262              'value': '%s (%s)' % (user.get_full_name(), user.username)
   273              'value': str(user.username)
       
   274             })
   263             })
   275 
   264 
   276     json_response = {'results': results}
   265     return HttpResponse(json.dumps(results))
   277 
       
   278     return HttpResponse(json.dumps(json_response))