daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Sep 19th, 2016 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Login(APIView):
  2.  
  3.     @csrf_exempt
  4.     def dispatch(self, *args, **kwargs):
  5.         return super(Login, self).dispatch(*args, **kwargs)
  6.  
  7.     @staticmethod
  8.     def post(request):
  9.         request_data = JSONParser().parse(request)
  10.  
  11.         if 'email' in request_data and 'password' in request_data:
  12.             try:
  13.                 validate_email(request_data['email'])
  14.             except ValidationError:
  15.                 return JsonResponse({'result': 'E-mail is invalid'}, status=400)
  16.  
  17.             user = authenticate(email=request_data['email'], password=request_data['password'])
  18.  
  19.             if user is not None:
  20.                 if user.is_active:
  21.                     try:
  22.                         token = Token.objects.get(user=user)
  23.                     except Token.DoesNotExist:
  24.                         token = Token.objects.create(user=user)
  25.  
  26.                     return JsonResponse({'result': 'success', 'token': token.key, 'id': user.id}, status=200)
  27.  
  28.             return JsonResponse({'result': 'E-mail or password is incorrect'}, status=400)
  29.  
  30.         return JsonResponse({'result': 'E-mail or password is empty'}, status=400)
  31.    
  32. class FbLogin(APIView):
  33.     authentication_classes = (authentication.TokenAuthentication,)
  34.     permission_classes = (permissions.AllowAny,)
  35.  
  36.     @csrf_exempt
  37.     def dispatch(self, *args, **kwargs):
  38.         return super(FbLogin, self).dispatch(*args, **kwargs)
  39.  
  40.     @staticmethod
  41.     def post(request):
  42.         request_data = JSONParser().parse(request)
  43.  
  44.         if 'access_token' in request_data:
  45.             response = requests.get(
  46.                 url='https://graph.facebook.com/v2.5/me/',
  47.                 params={
  48.                     'access_token': request_data['access_token'],
  49.                     'fields': 'email,first_name,last_name',
  50.                 },
  51.             )
  52.  
  53.             json_response = json.loads(response.text)
  54.  
  55.             if 'error' not in json_response:
  56.                 response_photo = requests.get(
  57.                     url='https://graph.facebook.com/v2.5/%s/picture' % json_response['id'],
  58.                     params={
  59.                         'redirect': 'false',
  60.                         'type': 'large',
  61.                     },
  62.                 )
  63.                 response_photo_json = json.loads(response_photo.text)
  64.  
  65.                 response_friends = requests.get(
  66.                     url='https://graph.facebook.com/v2.5/me/friends/',
  67.                     params={
  68.                         'access_token': request_data['access_token'],
  69.                         'limit': 300,
  70.                     },
  71.                 )
  72.  
  73.                 generated_password = get_random_string(10, '0123456789abcdefghijklmnopqrstuvwxyz')
  74.                 try:
  75.                     json_response_email = json_response['email']
  76.                 except:
  77.                     first_name = json_response['first_name'].lower()
  78.                     last_name = json_response['last_name'].lower()
  79.                     id = json_response['id']
  80.                     json_response_email = first_name + last_name + id + '@facebook.com'
  81.                 try:
  82.                     current_user = User.objects.get(email=json_response_email)
  83.                     current_user.set_password(generated_password)
  84.                     current_user.save()
  85.                 except User.DoesNotExist:
  86.                     new_user = User.objects.create_user(email=json_response_email,
  87.                                                         password=generated_password)
  88.  
  89.                     new_user.provider_id = json_response['id']
  90.                     new_user.provider_type = 'facebook'
  91.  
  92.                     if 'first_name' in json_response:
  93.                         new_user.first_name = json_response['first_name']
  94.  
  95.                     if 'last_name' in json_response:
  96.                         new_user.last_name = json_response['last_name']
  97.  
  98.                     new_user.save()
  99.  
  100.                     photo_name = urlparse(response_photo_json['data']['url']).path.split('/')[-1].split('?')[-1]
  101.                     photo_content = urllib.request.urlretrieve(response_photo_json['data']['url'])
  102.  
  103.                     new_user.profile_photo.save(photo_name, File(open(photo_content[0], 'rb')), save=True)
  104.                 user = authenticate(email=json_response_email, password=generated_password)
  105.                 try:
  106.                     token = Token.objects.get(user=user)
  107.                 except Token.DoesNotExist:
  108.                     token = Token.objects.create(user=user)
  109.                 if user is not None:
  110.                     if user.is_active:
  111.                             fullname = json_response['first_name'] + ' ' + json_response['last_name']
  112.                             return JsonResponse({'result': 'success', 'token': token.key, 'name': fullname}, status=200)
  113.  
  114.             return JsonResponse({'result': 'User access token is incorrect'}, status=400)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand