def user_view(request, username):
    if request.method == 'POST':
        user, is_new = User.objects.get_or_create(username=username)

        if is_new:
            api_r = requests.get('https://api.github.com/users/%s?client_id=%s&client_secret=%s' % (username, settings.SOCIAL_AUTH_GITHUB_KEY, settings.SOCIAL_AUTH_GITHUB_SECRET))

            # Log how many requests are remaining
            print(api_r.headers['X-RateLimit-Remaining'])

            response_json = api_r.json()

            try:
                user.name = response_json['name']
            except:
                pass
            user.avatar_url = response_json['avatar_url']
            user.last_modified = datetime.now()
            user.save()

            return HttpResponse(status=201)
        else:
            return HttpResponseForbidden()

    elif request.method =='GET':
        try:
            user = User.objects.get(username=username)
            result = user.getDict()

            if request.user.username == username:
                repo_r = requests.get('https://api.github.com/users/%s/repos?client_id=%s&client_secret=%s&sort=pushed' % (username, settings.SOCIAL_AUTH_GITHUB_KEY, settings.SOCIAL_AUTH_GITHUB_SECRET))
                repo_r_json = repo_r.json()

                result['repos'] = [{
                    'title': repo['name'],
                    'description': repo['description']
                } for repo in repo_r_json]

            result['tutorials'] = [{
                'id': tutorial.id,
                'title': tutorial.title,
                'description': tutorial.description,
                'repo_url': tutorial.repo_url
            } for tutorial in Tutorial.objects.filter(owner=user)]

            return HttpResponse(json.dumps(result),
                    content_type="application/json")
        except Exception as e:
            print(e)
            return HttpResponseNotFound()

    elif request.method == 'DELETE':
        try:
            User.objects.get(username=username).delete()
            return HttpResponse()
        except Exception as e:
            print(e)
            return HttpResponseNotFound()

    elif request.method == 'PATCH':
        try:
            user = User.objects.get(username=username)

            if (datetime.now() - user.last_modified) > timedelta(hours=1):
                api_r = requests.get('https://api.github.com/users/%s?client_id=%s&client_secret=%s' % (username, settings.SOCIAL_AUTH_GITHUB_KEY, settings.SOCIAL_AUTH_GITHUB_SECRET))

                # Log how many requests are remaining
                print(api_r.headers['X-RateLimit-Remaining'])

                response_json = api_r.json()

                try:
                    user.name = response_json['name']
                except:
                    pass
                user.avatar_url = response_json['avatar_url']
                user.last_modified = datetime.now()
                user.save()

                return HttpResponse()
            else:
                return HttpResponseNotModified()
        except Exception as e:
            print(e)
            return HttpResponseNotFound()

    else:
        return HttpResponseNotAllowed(['POST', 'GET', 'DELETE'])
dataset/ETHPy150Open bezi/gitorial/gitorial/views.py/user_view/original