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
    )

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))

Revert "Add support for server side authentication."
Revert "Add support for server side authentication."

This reverts commit 10ae930f6f14c2840d0b87cbec17054b4cc318d2.

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