from django.contrib.auth import models as auth_models
from django.db import models
import facepy
import simplejson

from facebook_auth import utils

class FacebookUser(auth_models.User):
    user_id = models.BigIntegerField(unique=True)
    access_token = models.TextField(blank=True, null=True)
    app_friends = models.ManyToManyField('self')

    @property
    def graph(self):
        return facepy.GraphAPI(self.access_token)

    @property
    def js_session(self):
        return simplejson.dumps({
            'access_token': self.access_token,
            'uid': self.user_id
        })

    @property
    def friends(self):
        return utils.get_from_graph_api(self.graph, "me/friends")['data']

    def update_app_friends(self):
        friends = self.friends
        friends_ids = [f['id'] for f in friends]
        self.app_friends.clear()
        self.app_friends.add(*FacebookUser.objects.filter(user_id__in=friends_ids))

from uuid import uuid1

from django.conf import settings
from django.contrib.auth import models as auth_models
from django.db import models
import facepy
import simplejson

from facebook_auth import utils

class FacebookUser(auth_models.User):
    user_id = models.BigIntegerField(unique=True)
    access_token = models.TextField(blank=True, null=True)
    app_friends = models.ManyToManyField('self')

    @property
    def graph(self):
        return facepy.GraphAPI(self.access_token)

    @property
    def js_session(self):
        return simplejson.dumps({
            'access_token': self.access_token,
            'uid': self.user_id
        })

    @property
    def friends(self):
        return utils.get_from_graph_api(self.graph, "me/friends")['data']

    def update_app_friends(self):
        friends = self.friends
        friends_ids = [f['id'] for f in friends]
        self.app_friends.clear()
        self.app_friends.add(*FacebookUser.objects.filter(user_id__in=friends_ids))


def get_auth_address(request, redirect_to, scope=''):
    state = unicode(uuid1())
    request.session['state'] = state
    return 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=%s&state=%s' % (
        settings.FACEBOOK_APP_ID, redirect_to, scope, state
    )

Add support for server side authentication.
Add support for server side authentication.

Change-Id: Iff45fa00b5a5b389f998570827e33d9d232f5d1e
Reviewed-on: http://review.pozytywnie.pl:8080/5087
Reviewed-by: Tomasz Wysocki <f17c2dc043305c950a029074ababd304e0373387@gmail.com>
Tested-by: Tomasz Wysocki <f17c2dc043305c950a029074ababd304e0373387@gmail.com>

+ from uuid import uuid1
+ 
+ from django.conf import settings
  from django.contrib.auth import models as auth_models
  from django.db import models
  import facepy
  import simplejson
  
  from facebook_auth import utils
  
  class FacebookUser(auth_models.User):
      user_id = models.BigIntegerField(unique=True)
      access_token = models.TextField(blank=True, null=True)
      app_friends = models.ManyToManyField('self')
  
      @property
      def graph(self):
          return facepy.GraphAPI(self.access_token)
  
      @property
      def js_session(self):
          return simplejson.dumps({
              'access_token': self.access_token,
              'uid': self.user_id
          })
  
      @property
      def friends(self):
          return utils.get_from_graph_api(self.graph, "me/friends")['data']
  
      def update_app_friends(self):
          friends = self.friends
          friends_ids = [f['id'] for f in friends]
          self.app_friends.clear()
          self.app_friends.add(*FacebookUser.objects.filter(user_id__in=friends_ids))
  
+ 
+ def get_auth_address(request, redirect_to, scope=''):
+     state = unicode(uuid1())
+     request.session['state'] = state
+     return 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=%s&state=%s' % (
+         settings.FACEBOOK_APP_ID, redirect_to, scope, state
+     )
+ 
Add support for server side authentication.
## Code Before:
from django.contrib.auth import models as auth_models
from django.db import models
import facepy
import simplejson

from facebook_auth import utils

class FacebookUser(auth_models.User):
    user_id = models.BigIntegerField(unique=True)
    access_token = models.TextField(blank=True, null=True)
    app_friends = models.ManyToManyField('self')

    @property
    def graph(self):
        return facepy.GraphAPI(self.access_token)

    @property
    def js_session(self):
        return simplejson.dumps({
            'access_token': self.access_token,
            'uid': self.user_id
        })

    @property
    def friends(self):
        return utils.get_from_graph_api(self.graph, "me/friends")['data']

    def update_app_friends(self):
        friends = self.friends
        friends_ids = [f['id'] for f in friends]
        self.app_friends.clear()
        self.app_friends.add(*FacebookUser.objects.filter(user_id__in=friends_ids))

## Instruction:
Add support for server side authentication.
## Code After:
from uuid import uuid1

from django.conf import settings
from django.contrib.auth import models as auth_models
from django.db import models
import facepy
import simplejson

from facebook_auth import utils

class FacebookUser(auth_models.User):
    user_id = models.BigIntegerField(unique=True)
    access_token = models.TextField(blank=True, null=True)
    app_friends = models.ManyToManyField('self')

    @property
    def graph(self):
        return facepy.GraphAPI(self.access_token)

    @property
    def js_session(self):
        return simplejson.dumps({
            'access_token': self.access_token,
            'uid': self.user_id
        })

    @property
    def friends(self):
        return utils.get_from_graph_api(self.graph, "me/friends")['data']

    def update_app_friends(self):
        friends = self.friends
        friends_ids = [f['id'] for f in friends]
        self.app_friends.clear()
        self.app_friends.add(*FacebookUser.objects.filter(user_id__in=friends_ids))


def get_auth_address(request, redirect_to, scope=''):
    state = unicode(uuid1())
    request.session['state'] = state
    return 'https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&scope=%s&state=%s' % (
        settings.FACEBOOK_APP_ID, redirect_to, scope, state
    )
