django-vkontakte-iframe / vk_iframe /

#coding: utf-8
from django.contrib.auth.backends import ModelBackend
from vk_iframe.models import City, Country

    from django.contrib.auth import get_user_model
except ImportError: # django < 1.5
    from django.contrib.auth.models import User
    User = get_user_model()

class VkontakteUserBackend(ModelBackend):
    """ Использовать вместе с vk.middleware.AuthenticationMiddleware """

    def authenticate(self, vk_form):

        if not vk_form:

        if not vk_form.is_valid():

        defaults = {}
        username = str(vk_form.vk_user_id())
        vk_profile = vk_form.profile_api_result()
        if vk_profile:
            defaults = dict(

        user, created = User.objects.get_or_create(username=username, defaults=defaults)
        if created:
            user = self.configure_user(vk_profile, user)
        return user

    def configure_user(self, vk_profile, user):
        user_profile = user.vk_profile
        for key in vk_profile:
            if key not in ['city', 'country']:
                setattr(user_profile, key, vk_profile[key])

        country_id = vk_profile.get('country', None)
        if country_id:
            country, created = Country.objects.get_or_create(id=country_id)
   = country

        city_id = vk_profile.get('city', None)
        if city_id and city_id!=u'0':
            city, created = City.objects.get_or_create(id=city_id, defaults={'country_id': country_id})
   = city
        return user
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.