|
| 1 | +### |
| 2 | +# Copyright 2015-2020, Institute for Systems Biology |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +### |
| 16 | + |
| 17 | +import json |
| 18 | +from json import JSONEncoder |
| 19 | +import logging |
| 20 | +import sys |
| 21 | + |
| 22 | + |
| 23 | +from django.conf import settings |
| 24 | +from django.contrib.auth.models import User |
| 25 | +from allauth.socialaccount.models import SocialAccount |
| 26 | +from django.http import HttpResponse, JsonResponse |
| 27 | + |
| 28 | +debug = settings.DEBUG |
| 29 | +logger = logging.getLogger('main_logger') |
| 30 | +from datetime import datetime |
| 31 | + |
| 32 | +from django.views.decorators.csrf import csrf_exempt |
| 33 | +from django.views.decorators.http import require_http_methods |
| 34 | +from cohorts.decorators import api_auth |
| 35 | + |
| 36 | + |
| 37 | +class DateTimeEncoder(JSONEncoder): |
| 38 | + # Override the default method |
| 39 | + def default(self, obj): |
| 40 | + if isinstance(obj, datetime): |
| 41 | + encoded_object = obj.strftime('%s') |
| 42 | + else: |
| 43 | + encoded_object = super(self, obj) |
| 44 | + return encoded_object |
| 45 | + # if isinstance(obj, (datetime.date, datetime.datetime)): |
| 46 | + # return obj.isoformat() |
| 47 | + |
| 48 | +# class JsonResponse(HttpResponse): |
| 49 | +# def __init__(self, content, mimetype='application/json', status=None, content_type='application/json'): |
| 50 | +# json_text = json.dumps(content, cls=DateTimeEncoder) |
| 51 | +# super(JsonResponse, self).__init__( |
| 52 | +# content=json_text, |
| 53 | +# status=status, |
| 54 | +# content_type=content_type) |
| 55 | + |
| 56 | +@csrf_exempt |
| 57 | +@api_auth |
| 58 | +@require_http_methods(["GET"]) |
| 59 | +def user_detail(request): |
| 60 | + if debug: logger.debug('Called ' + sys._getframe().f_code.co_name) |
| 61 | + |
| 62 | + try: |
| 63 | + user = User.objects.get(email=request.GET.get('email', '')) |
| 64 | + except Exception as e: |
| 65 | + logger.error("[ERROR] {} is not a registered IDC web app user".format(request.GET.get('email', ''))) |
| 66 | + logger.exception(e) |
| 67 | + user_details = { |
| 68 | + "message": "Not a registered IDC web app user", |
| 69 | + "code": 404 |
| 70 | + } |
| 71 | + return JsonResponse(user_details) |
| 72 | + |
| 73 | + try: |
| 74 | + social_account = SocialAccount.objects.get(user=user, provider='google') |
| 75 | + except Exception as e: |
| 76 | + # This is a local account |
| 77 | + social_account = None |
| 78 | + user_details = { |
| 79 | + 'date_joined': user.date_joined, |
| 80 | + 'email': user.email, |
| 81 | + 'id': user.id, |
| 82 | + 'last_login': user.last_login |
| 83 | + } |
| 84 | + |
| 85 | + if social_account: |
| 86 | + user_details['extra_data'] = social_account.extra_data if social_account else None |
| 87 | + user_details['first_name'] = user.first_name |
| 88 | + user_details['last_name'] = user.last_name |
| 89 | + else: |
| 90 | + user_details['username'] = user.username |
| 91 | + |
| 92 | + results = {"user_details": user_details} |
| 93 | + |
| 94 | + return JsonResponse(results, encoder=DateTimeEncoder) |
| 95 | + |
| 96 | + |
| 97 | + |
0 commit comments