<commit_before>from django.shortcuts import render
from django.template import Context, RequestContext
from django.contrib.auth.models import User
from private_messages.models import Message, ConversationLog
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.contrib.auth import authenticate, login
from django.core.context_processors import csrf
from private_messages.forms import SendMessage, ConversationForm
from django.http import HttpResponse, HttpResponseRedirect
import re

@login_required
def index(request):
	allconversations = ConversationLog.objects.all()
	conversations = []
	for conversation in allconversations:
		if request.user in conversation.participants.all():
			conversations.append(conversation)
	c = Context({'conversations': conversations})
	if request.method == 'POST':
		form = SendMessage(request.POST)
		if form.is_valid():
			send_message = SendMessage(request.POST)
			message_content = send_message.data['content']
			message_sender = request.user
			message = Message(sender=message_sender, content=message_content)
			message.save()
	else:
		form = SendMessage()
	c['form'] = form
	c = RequestContext(request, c)
	return render(request, 'private_messages.html', c)

def getConversation(request, conversationID):
	allconversations = ConversationLog.objects.all()
	conversations = []
	for conversation in allconversations:
		if request.user in conversation.participants.all():
			conversations.append(conversation)
	
	active_conversation = ConversationLog.objects.get(pk=conversationID)
	if request.user not in active_conversation.participants.all():
		return HttpResponse("You are not authorized to access that location.")
	c = Context({'conversations': conversations, 'active_conversation': active_conversation})
	if request.method == 'POST':
		form = SendMessage(request.POST)
		if form.is_valid():
			send_message = SendMessage(request.POST)
			message_content = send_message.data['content']
			message_sender = request.user
			message = Message(sender=message_sender, content=message_content)
			message.save()
			active_conversation.log.add(message)
	else:
		form = SendMessage()
	c['form'] = form
	c = RequestContext(request, c)
	return render(request, 'private_messages.html', c)

#@csrf_exempt
def newConversation(request):
	#if request.method == 'POST':
	#	form = 
	#active_conversation = ConversationLog.objects.get(pk=conversationID)
	#c = Context({'conversations': conversations, 'active_conversation':active_conversation})
	if request.method == 'POST':
		conversationForm = ConversationForm()
		#if conversationForm.is_valid():
		new_conversation = ConversationForm(request.POST)
		participants = new_conversation.data['participants']
		log = new_conversation.data['message']
		pattern = re.compile("^\s+|\s*,\s*|\s+$")
		if (participants != "") and (log != ""):
			users = []
			nonexistentusers = []
			for x in pattern.split(participants):
				if x:
					if x in User.objects.all().values_list('username', flat=True):
						users.append(x)
					else:
						nonexistentusers.append(x)
			if len(nonexistentusers) > 0:
				printout = "The following users do not exist: "
				for y in nonexistentusers:
					printout += y
					printout += " "
				printout += ". Please send the message again."
				return HttpResponse(printout)
			else:
				active_conversation = ConversationLog()
				active_conversation.save()
				#userobjects = []
				for x in users:
					active_conversation.participants.add(User.objects.get(username=x))
					#userobjects.append(User.objects.get(username=x))
					#p = "HELLO "
					#p+=str(currentUser)
					#return HttpResponse(p)
					#conversation.participants.add(currentUser)
				
				
				message_sender = request.user
				message = Message(sender=message_sender, content=log)
				message.save()<commit_msg>Fixed one bug in private messages.<commit_after>from django.shortcuts import render
from django.template import Context, RequestContext
from django.contrib.auth.models import User
from private_messages.models import Message, ConversationLog
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.contrib.auth import authenticate, login
from django.core.context_processors import csrf
from private_messages.forms import SendMessage, ConversationForm
from django.http import HttpResponse, HttpResponseRedirect
import re

@login_required
def index(request):
	allconversations = ConversationLog.objects.all()
	conversations = []
	for conversation in allconversations:
		if request.user in conversation.participants.all():
			conversations.append(conversation)
	c = Context({'conversations': conversations})
	if request.method == 'POST':
		form = SendMessage(request.POST)
		if form.is_valid():
			send_message = SendMessage(request.POST)
			message_content = send_message.data['content']
			message_sender = request.user
			message = Message(sender=message_sender, content=message_content)
			message.save()
	else:
		form = SendMessage()
	c['form'] = form
	c = RequestContext(request, c)
	return render(request, 'private_messages.html', c)

def getConversation(request, conversationID):
	allconversations = ConversationLog.objects.all()
	conversations = []
	for conversation in allconversations:
		if request.user in conversation.participants.all():
			conversations.append(conversation)
	
	active_conversation = ConversationLog.objects.get(pk=conversationID)
	if request.user not in active_conversation.participants.all():
		return HttpResponse("You are not authorized to access that location.")
	c = Context({'conversations': conversations, 'active_conversation': active_conversation})
	if request.method == 'POST':
		form = SendMessage(request.POST)
		if form.is_valid():
			send_message = SendMessage(request.POST)
			message_content = send_message.data['content']
			message_sender = request.user
			message = Message(sender=message_sender, content=message_content)
			message.save()
			active_conversation.log.add(message)
	else:
		form = SendMessage()
	c['form'] = form
	c = RequestContext(request, c)
	return render(request, 'private_messages.html', c)

#@csrf_exempt
def newConversation(request):
	#if request.method == 'POST':
	#	form = 
	#active_conversation = ConversationLog.objects.get(pk=conversationID)
	#c = Context({'conversations': conversations, 'active_conversation':active_conversation})
	if request.method == 'POST':
		conversationForm = ConversationForm()
		#if conversationForm.is_valid():
		new_conversation = ConversationForm(request.POST)
		participants = new_conversation.data['participants']
		log = new_conversation.data['message']
		pattern = re.compile("^\s+|\s*,\s*|\s+$")
		if (participants != "") and (log != ""):
			users = []
			nonexistentusers = []
			for x in pattern.split(participants):
				if x:
					if x in User.objects.all().values_list('username', flat=True):
						users.append(x)
					else:
						nonexistentusers.append(x)
			if len(nonexistentusers) > 0:
				printout = "The following users do not exist: "
				for y in nonexistentusers:
					printout += y
					printout += " "
				printout += ". Please send the message again."
				return HttpResponse(printout)
			else:
				active_conversation = ConversationLog()
				active_conversation.save()
				#userobjects = []
				for x in users:
					active_conversation.participants.add(User.objects.get(username=x))
					#userobjects.append(User.objects.get(username=x))
					#p = "HELLO "
					#p+=str(currentUser)
					#return HttpResponse(p)
					#conversation.participants.add(currentUser)
				
				active_conversation.participants.add(User.objects.get(username=request.user))
				message_sender = request.user
				message = Message(sender=message_sender, content=log)
				message.save()
{'fraction_non_alphanumeric': 0.11061608961303462, 'fraction_numerical': 0.0002545824847250509, 'mean_word_length': 5.0253067484662575, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2871232', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2308, 'n_words': 525}