<commit_before>from django.shortcuts import render
from django.http import Http404, HttpResponse
from django.shortcuts import redirect

from ToDoList.models import List
from ToDoList.models import Item
from ToDoList.models import ListAddForm
from ToDoList.models import ItemAddForm
from ToDoList.models import ItemEditForm
from ToDoList.models import LogInForm

from django.views.decorators.csrf import csrf_exempt

from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm

from secure import GITHUB_WEBHOOK_KEY

import hmac
import hashlib
import json
import os
import subprocess

# Create your views here.
 
def login_view(request):
	if request.user.is_authenticated():
		return redirect('ToDoList.views.index')
	else:
		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:
					login(request, user)
					# Redirect to a success page.
					return redirect('ToDoList.views.index')
				else:
					# Return a 'disabled account' error message
					return redirect('ToDoList.views.login_view')
			else:
				# Return an 'invalid login' error message.
				return redirect('ToDoList.views.login_view')
		else:
			form = AuthenticationForm()
			return render(request, 'ToDoList/login.html', { 'form': form })

def signup_view(request):
	if request.user.is_authenticated():
		return redirect('ToDoList.views.index')
	else:
		if request.method == 'POST':
			username = request.POST['username']
			password = request.POST['<PASSWORD>']
			try:
				user = User.objects.create_user(username, '', password)
				user.save()
			except:
				return redirect('ToDoList.views.signupfail_view')
			return redirect('ToDoList.views.login_view')
		else:
			form = UserCreationForm()
		return render(request, 'ToDoList/signup.html', {
			'form': form,
		})

def signupfail_view(request):
	return render(request, 'ToDoList/signup_fail.html')

def logout_view(request):
	logout(request)
	return redirect('ToDoList.views.login_view')

def validateSignature(request):
	x_hub_signature = request.META.get('HTTP_X_HUB_SIGNATURE')
	if x_hub_signature:
		sha_name, signature = x_hub_signature.split('=')
		mac = hmac.new(GITHUB_WEBHOOK_KEY, msg=request.body, digestmod=hashlib.sha1)
		return mac.hexdigest() == signature
	else:
		return False

@csrf_exempt
def github(request):
	if request.method == 'POST':
		if validateSignature(request):
			body = json.loads(request.body)
			if body.get("ref", None) and request.META.get('HTTP_X_GITHUB_EVENT', None):
				if body.get("ref") == "refs/heads/master" and request.META.get("HTTP_X_GITHUB_EVENT") == "push":
					devnull = open(os.devnull, 'wb')
					subprocess.Popen('/home/localuser/comp204p/bin/restartAndPull.bash', stdout=devnull, stderr=devnull, shell=True)
					return HttpResponse("restarting server")
				else:
					return HttpResponse("headers and body not properly set")
			else:
				return HttpResponse("headers and body not even initialized")
		else:
			return HttpResponse("authentication failed")
	else:
		return HttpResponse("wrong HTTP method")
<commit_msg>add testing code for autodeploy<commit_after>from django.shortcuts import render
from django.http import Http404, HttpResponse
from django.shortcuts import redirect

from ToDoList.models import List
from ToDoList.models import Item
from ToDoList.models import ListAddForm
from ToDoList.models import ItemAddForm
from ToDoList.models import ItemEditForm
from ToDoList.models import LogInForm

from django.views.decorators.csrf import csrf_exempt

from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm

from secure import GITHUB_WEBHOOK_KEY

import hmac
import hashlib
import json
import os
import subprocess

# Create your views here.
 
def login_view(request):
	if request.user.is_authenticated():
		return redirect('ToDoList.views.index')
	else:
		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:
					login(request, user)
					# Redirect to a success page.
					return redirect('ToDoList.views.index')
				else:
					# Return a 'disabled account' error message
					return redirect('ToDoList.views.login_view')
			else:
				# Return an 'invalid login' error message.
				return redirect('ToDoList.views.login_view')
		else:
			form = AuthenticationForm()
			return render(request, 'ToDoList/login.html', { 'form': form })

def signup_view(request):
	if request.user.is_authenticated():
		return redirect('ToDoList.views.index')
	else:
		if request.method == 'POST':
			username = request.POST['username']
			password = request.POST['<PASSWORD>']
			try:
				user = User.objects.create_user(username, '', password)
				user.save()
			except:
				return redirect('ToDoList.views.signupfail_view')
			return redirect('ToDoList.views.login_view')
		else:
			form = UserCreationForm()
		return render(request, 'ToDoList/signup.html', {
			'form': form,
		})

def signupfail_view(request):
	return render(request, 'ToDoList/signup_fail.html')

def logout_view(request):
	logout(request)
	return redirect('ToDoList.views.login_view')

def validateSignature(request):
	x_hub_signature = request.META.get('HTTP_X_HUB_SIGNATURE')
	if x_hub_signature:
		sha_name, signature = x_hub_signature.split('=')
		mac = hmac.new(GITHUB_WEBHOOK_KEY, msg=request.body, digestmod=hashlib.sha1)
		return mac.hexdigest() == signature
	else:
		return False

@csrf_exempt
def github(request):
	devnull = open(os.devnull, 'wb')
	subprocess.Popen('/home/localuser/comp204p/bin/restartAndPull.bash', stdout=devnull, stderr=devnull, shell=True)
	return HttpResponse("restarting server for realzzz")
	
	if request.method == 'POST':
		if validateSignature(request):
			body = json.loads(request.body)
			if body.get("ref", None) and request.META.get('HTTP_X_GITHUB_EVENT', None):
				if body.get("ref") == "refs/heads/master" and request.META.get("HTTP_X_GITHUB_EVENT") == "push":
					devnull = open(os.devnull, 'wb')
					subprocess.Popen('/home/localuser/comp204p/bin/restartAndPull.bash', stdout=devnull, stderr=devnull, shell=True)
					return HttpResponse("restarting server")
				else:
					return HttpResponse("headers and body not properly set")
			else:
				return HttpResponse("headers and body not even initialized")
		else:
			return HttpResponse("authentication failed")
	else:
		return HttpResponse("wrong HTTP method")

{'fraction_non_alphanumeric': 0.10865775711795468, 'fraction_numerical': 0.0024694944799535153, 'mean_word_length': 4.930232558139535, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '7018591', 'n_tokens_mistral': 2425, 'n_tokens_neox': 2152, 'n_words': 526}