<repo_name>mohdirshad/FacebookPage<file_sep>/requirements.txt
appdirs==1.4.0
Django==1.10.5
django-braces==1.10.0
django-oauth-toolkit==0.11.0
django-rest-framework-social-oauth2==1.0.5
djangorestframework==3.5.3
oauthlib==1.1.2
packaging==16.8
PyJWT==1.4.2
pyparsing==2.1.10
python-openid==2.2.5
requests==2.13.0
requests-oauthlib==0.7.0
six==1.10.0
social-auth-app-django==1.0.1
social-auth-core==1.0.1
gunicorn
<file_sep>/accounts/rest_views.py

class FacebookRegistration(APIView):
    """
    Facebook user registration is done here
    """

    serializer_class = FbRegisterSerializer
    permission_classes = (AllowAny,)
    model = User

    def get_serializer_class(self):
        return self.serializer_class

    def post(self, request):

        serializer = self.serializer_class(data=request.data,)
        if serializer.is_valid():
            try:
                user = User.objects.get(facebook_id=request.data['facebook_id'])
            except:
                try:
                    user = User.objects.get(email=serializer.validated_data.get('contact_no'))
                    for k, v in serializer.validated_data.iteritems():
                        setattr(user,k,v)
                    user.save()
                except:
                    user = serializer.save()

            token, created = Token.objects.get_or_create(user=user)
            return Response({'token': token.key,
                        'image': user.image_url, 
                       'name' : user.name,
                       'email': user.email,
                       },
                        status=status.HTTP_201_CREATED)

        else:
            return Response({'error': serializer.errors},
                            status=status.HTTP_400_BAD_REQUEST)

<file_sep>/accounts/models.py
from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from django.conf import settings
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser, PermissionsMixin
)


class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(
            email,
            password=<PASSWORD>,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    gen_choices = (
                 ('Male', 'm'),
                 ('Female', 'f')
               )

    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        null=True,
    )
    name = models.CharField(_("Name"),max_length=150,)
    dob = models.DateField(_("Date of Birth"),blank=True,null=True)
    is_active = models.BooleanField(_("Is Active"),default=True)
    is_admin = models.BooleanField(_("Is Admin"),default=False)
    facebook_id = models.CharField(_("Facebook Id"),max_length=100,blank=True,db_index=True)
    date_joined = models.DateTimeField(_("Date Joined"), auto_now_add=True)
    image_url = models.URLField(_("Image Url"),blank=True,null=True)
    gender = models.CharField(_("Gender"),choices=gen_choices,max_length=5,default="")
    language = models.CharField(max_length=10, default='en',choices=settings.LANGUAGES)
    objects = UserManager()

    USERNAME_FIELD = 'email'

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')
        ordering = ('email',)

    
    def __unicode__(self):
        return unicode(self.name or self.contact_no,)



    def get_image_url(self):
    	if self.image:
    		return self.image.url
    	return self.image_url

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin


<file_sep>/accounts/urls.py
from django.conf.urls import url, include
from django.contrib import admin
import views

urlpatterns = [
    url(r'^',views.Home.as_view(),),
    url(r'update/^',views.UpdatePage.as_view(),),

]
<file_sep>/accounts/serializers.py
from rest_framework import serializers

from .models import User


class FbRegisterSerializer(serializers.ModelSerializer):
    email = serializers.EmailField(required=True)
    image_url = serializers.URLField(required=False)
    facebook_id = serializers.CharField(required=True)

    class Meta:
        model = User
        fields = ('name','email', 'image_url', 'facebook_id', )

<file_sep>/accounts/views.py
from django.shortcuts import render
from django.views import View
from django.views.generic import TemplateView
# Create your views here.

class Home(TemplateView):
    template_name = "login.html"


class UpdatePage(View):
    def post(self, request):
    	data = request.data
        return render(request, 'updatepage.html',data)
