<commit_before>from django.shortcuts import render, redirect
from django.contrib.auth import authenticate
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
from entree.models import *
import requests

# Global site context
context_dict = {
    'version': 'v0.1'
}


def login(request):

    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                auth_login(request, user)

                # check if user has Instagram auth_token already
                profile = UserProfile.objects.get(user=user)

                if profile.instagram_auth_token:
                    # redirect to homepage
                    return redirect('/entree/', context_dict)

                else:
                    # authenticate with Instagram to obtain auth_token
                    instagram = InstagramClient.objects.get(pk=1)
                    client_id = instagram.client_id
                    redirect_uri = instagram.redirect_uri
                    uri = 'https://api.instagram.com/oauth/authorize/?'
                    uri += 'response_type=code'
                    uri += '&scope=public_content'
                    uri += '&client_id=' + requests.utils.quote(client_id, safe='')
                    uri += '&redirect_uri=' + requests.utils.quote(redirect_uri, safe='')
                    return redirect(uri, context_dict)

            return render(request, 'entree/login.html', context_dict)
        return render(request, 'entree/login.html', context_dict)

    return render(request, 'entree/login.html', context_dict)


@login_required
def insta_auth(request):
    # request format:
    # http://entree.noip.me/insta/auth/?code=586e03f5daa841119ced64e0ff971b90
    code = request.GET['code']
    instagram = InstagramClient.objects.get(pk=1)
    uri = 'https://api.instagram.com/oauth/access_token'
    post_data = {
        'client_id': instagram.client_id,
        'client_secret': instagram.client_secret,
        'grant_type': 'authorization_code',
        'redirect_uri': instagram.redirect_uri,
        'code': code,
    }
    response = requests.post(uri, data=post_data)
    data = response.json()  # convert response body to json object
    access_token = data['access_token']

    # add access token to user profile
    profile = UserProfile.objects.get(user=request.user)
    profile.instagram_auth_token = access_token
    profile.save()

    return redirect('/entree/', context_dict)


@login_required
def logout(request):
    auth_logout(request)
    return render(request, 'entree/login.html', context_dict)


def about(request):
    return render(request, 'entree/about.html', context_dict)


@login_required
def index(request):
    return render(request, 'entree/index.html', context_dict)


@login_required
def posts(request):

    if request.method == 'POST':
        city = request.POST['city']
        context_dict['city'] = city
    else:
        print('Cannot GET this page')

    return render(request, 'entree/posts.html', context_dict)

<commit_msg>Removed Instagram oAuth for Now...<commit_after>from django.shortcuts import render, redirect
from django.contrib.auth import authenticate
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
from entree.models import *
import requests

# Global site context
context_dict = {
    'version': 'v0.1'
}


def login(request):

    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                auth_login(request, user)
                '''
                # check if user has Instagram auth_token already
                profile = UserProfile.objects.get(user=user)

                if profile.instagram_auth_token:
                    # redirect to homepage
                    return redirect('/entree/', context_dict)

                else:
                    # authenticate with Instagram to obtain auth_token
                    instagram = InstagramClient.objects.get(pk=1)
                    client_id = instagram.client_id
                    redirect_uri = instagram.redirect_uri
                    uri = 'https://api.instagram.com/oauth/authorize/?'
                    uri += 'response_type=code'
                    uri += '&scope=public_content'
                    uri += '&client_id=' + requests.utils.quote(client_id, safe='')
                    uri += '&redirect_uri=' + requests.utils.quote(redirect_uri, safe='')
                    return redirect(uri, context_dict)
                '''
                return redirect('/entree/', context_dict)
            return render(request, 'entree/login.html', context_dict)
        return render(request, 'entree/login.html', context_dict)

    return render(request, 'entree/login.html', context_dict)


@login_required
def insta_auth(request):
    # request format:
    # http://entree.noip.me/insta/auth/?code=586e03f5daa841119ced64e0ff971b90
    code = request.GET['code']
    instagram = InstagramClient.objects.get(pk=1)
    uri = 'https://api.instagram.com/oauth/access_token'
    post_data = {
        'client_id': instagram.client_id,
        'client_secret': instagram.client_secret,
        'grant_type': 'authorization_code',
        'redirect_uri': instagram.redirect_uri,
        'code': code,
    }
    response = requests.post(uri, data=post_data)
    data = response.json()  # convert response body to json object
    access_token = data['access_token']

    # add access token to user profile
    profile = UserProfile.objects.get(user=request.user)
    profile.instagram_auth_token = access_token
    profile.save()

    return redirect('/entree/', context_dict)


@login_required
def logout(request):
    auth_logout(request)
    return render(request, 'entree/login.html', context_dict)


def about(request):
    return render(request, 'entree/about.html', context_dict)


@login_required
def index(request):
    return render(request, 'entree/index.html', context_dict)


@login_required
def posts(request):

    if request.method == 'POST':
        city = request.POST['city']
        context_dict['city'] = city
    else:
        print('Cannot GET this page')

    return render(request, 'entree/posts.html', context_dict)


{'fraction_non_alphanumeric': 0.10421319041238648, 'fraction_numerical': 0.007146047342563644, 'mean_word_length': 2.6060118089103597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '752270', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1855, 'n_words': 419}