<commit_before>#! /usr/bin/env python
from django.core.context_processors import csrf
from django.shortcuts import render_to_response, redirect, render
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from mocha_models.models import *
from mocha_models.forms import *
from django.views.generic.edit import FormView
from django.db import connections, transaction
from django.http import Http404
from django.db import connection
import socket
HOST = "localhost"
PORT = 8080

def loginview(request):
	c = {}
	c.update(csrf(request))
	return render_to_response('login.html', c)

def auth_and_login(request, onsuccess='/', onfail='/login/'):
	user = authenticate(username=request.POST['email'], password=request.POST['password'])
	if user is not None:
	    login(request, user)
	    return redirect(onsuccess)
	else:
	    return redirect(onfail)  

def create_user(username, email, password):
	user = User(username=username, email=email)
	#user.set_password(password)
	#user.save()
	global HOST
	global PORT
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
	sock.sendall("+UP: " + username + " " + password)
	sock.close()
	return user


def user_exists(username):
    user_count = User.objects.filter(username=username).count()
    if user_count == 0:
        return False
    return True

def sign_up_in(request):
    post = request.POST
    if not user_exists(post['email']): 
        user = create_user(username=post['email'], email=post['email'], password=post['password'])
    	return auth_and_login(request)
    else:
    	return redirect("/login/")

#def tabs(request):
#     return render_to_response('index.html')

@login_required(login_url='/login/')
def create_topic(request):
    if request.method == 'POST': # If the form has been submitted...
        form = TopicContentForm(request.POST) # A form bound to the POST data
	#obj = form.save(commit=False)
	#obj.userid = request.user
	#obj.save()
	global HOST
    	global PORT
    	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
    	sock.sendall("+UT:" + str(request.user) + " " + str(request.POST['topic']))
	sock.close()
	return redirect("/home/")
    else:
        form = TopicContentForm() # An unbound form
	return render(request, 'createTopic.html', {'form': form,})

@login_required(login_url='/login/')
def subscribe_topic(request):
    if request.method == 'POST': # If the form has been submitted...
        form = UserTopicForm(request.POST) # A form bound to the POST data
	#obj = form.save(commit=False)
	#obj.userid = request.user
	#obj.save()
	global HOST
    	global PORT
    	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
 	sock.sendall("+TC : " + str(request.POST['topic']) + " " + #str(request.POST['content']))	
	sock.close()
	return redirect("/home/")
    else:
        form = UserTopicForm() # An unbound form
	return render(request, 'subscribeTopic.html', {'form': form,})

    
#@login_required(login_url='/login/')
#def secured(request):
#    return render_to_response("display.html")


DBNAME = 'default'
#allll results of a SQL as a list of dicts
def dbfetchall(sql, params=[]):
    #"Returns all rows from SQL query as a dict"
    global DBNAME
    cur = connections[DBNAME].cursor()
    cur.execute(sql, params)
    desc = cur.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cur.fetchall()
    ]

def dbfetchcontent(topic):
   rows = dbfetchall('select content from topic_content where topic = \'' + str(topic) + '\' ORDER BY timestamp') 
   return [c['content'] for c in rows] 


def dbfetchtopics(username):
   rows = dbfetchall('select topic from user_topic where userid = \'' + str(username) + '\' ORDER BY timestamp') 
   return [c['topic'] for c in rows] 


@login_required(login_url='/login/')<commit_msg>Fixed content, wrote socket for reading as well<commit_after>#! /usr/bin/env python
from django.core.context_processors import csrf
from django.shortcuts import render_to_response, redirect, render
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from mocha_models.models import *
from mocha_models.forms import *
from django.views.generic.edit import FormView
from django.db import connections, transaction
from django.http import Http404
from django.db import connection
import socket
from pprint import pprint

HOST = "localhost"
PORT = 8080
CONTENT_SIZE = 10000

def loginview(request):
	c = {}
	c.update(csrf(request))
	return render_to_response('login.html', c)

def auth_and_login(request, onsuccess='/', onfail='/login/'):
	user = authenticate(username=request.POST['email'], password=request.POST['password'])
	if user is not None:
	    login(request, user)
	    return redirect(onsuccess)
	else:
	    return redirect(onfail)  

def create_user(username, email, password):
	user = User(username=username, email=email)
	#user.set_password(password)
	#user.save()
	global HOST
	global PORT
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
	sock.sendall("+SUP: " + username + " " + password)
	sock.close()
	return user


def user_exists(username):
    user_count = User.objects.filter(username=username).count()
    if user_count == 0:
        return False
    return True

def sign_up_in(request):
    post = request.POST
    if not user_exists(post['email']): 
        user = create_user(username=post['email'], email=post['email'], password=post['password'])
    	return auth_and_login(request)
    else:
    	return redirect("/login/")

#def tabs(request):
#     return render_to_response('index.html')

@login_required(login_url='/login/')
def create_topic(request):
    if request.method == 'POST': # If the form has been submitted...
        form = TopicContentForm(request.POST) # A form bound to the POST data
	#obj = form.save(commit=False)
	#obj.userid = request.user
	#obj.save()
	global HOST
    	global PORT
    	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
    	sock.sendall("+SUT:" + str(request.user) + " " + str(request.POST['topic']) + " " + str(request.POST['content']))
	print repr(request.POST)	
	sock.close()
	return redirect("/home/")
    else:
        form = TopicContentForm() # An unbound form
	return render(request, 'createTopic.html', {'form': form,})

@login_required(login_url='/login/')
def subscribe_topic(request):
    if request.method == 'POST': # If the form has been submitted...
        form = UserTopicForm(request.POST) # A form bound to the POST data
	#obj = form.save(commit=False)
	#obj.userid = request.user
	#obj.save()
	global HOST
    	global PORT
    	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((HOST, PORT))
 	sock.sendall("+STC : " + str(request.POST['topic'])) 
	print repr(request.POST)
	sock.close()
	return redirect("/home/")
    else:
        form = UserTopicForm() # An unbound form
	return render(request, 'subscribeTopic.html', {'form': form,})

    
#@login_required(login_url='/login/')
#def secured(request):
#    return render_to_response("display.html")


DBNAME = 'default'
#allll results of a SQL as a list of dicts
def dbfetchall(sql, params=[]):
    #"Returns all rows from SQL query as a dict"
    global DBNAME
    cur = connections[DBNAME].cursor()
    cur.execute(sql, params)
    desc = cur.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cur.fetchall()
    ]

def dbfetchcontent(topic):
   #rows = dbfetchall('select content from topic_content where topic = \'' + str(topic) + '\' ORDER BY timestamp')
   #return [c['content'] for c in rows]
   global CONTENT_SIZE 
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   sock.connect((HOST, PORT))
   sock.sendall("+RTC : " + str(topic))
   data = sock.recv(CONTENT_SIZE)
   sock.close(); 
   #return [c['content'] for c in rows] 
   return data


def dbfetchtopics(username):
   #rows = dbfetchall('select topic from user_topic where userid = \'' + str(username) + '\' ORDER BY timestamp') 
   #return [c['topic'] for c in rows]
   global TOPIC_SIZE
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   sock.connect((HOST, PORT))
   sock.sendall("+RUT : " + str(username))
   data = sock.recv(TOPIC_SIZE)
   sock.close(); 
   return data


@login_required(login_url='/login/')
{'fraction_non_alphanumeric': 0.1327464380864126, 'fraction_numerical': 0.002664195528784895, 'mean_word_length': 4.204339963833634, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2616020', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2742, 'n_words': 712}