<commit_before>from django.http import HttpResponse , StreamingHttpResponse
from django.shortcuts import render
from django.core import serializers
import time, math
from .models import Market, Item, LocalMarketData
from decimal import Decimal
import logging

# to CSRF exempt 
from django.views.decorators.csrf import csrf_exempt

try:
    from django.utils import simplejson as json
except ImportError:
	import json

def home(request):
	return HttpResponse("<h1>Home</h1>")

def get_dist(x1,y1,x2,y2):	
	return pow((pow(x1-x2,2)+pow(y1-y2,2)), 0.5)

@csrf_exempt
def market_home(request):

	if request.POST:
		if 'latitude' in request.POST.keys() and 'longitude' in request.POST.keys():
			if request.POST['latitude'] and request.POST['longitude']:
				markets = list(Market.objects.all())
				latitude = request.POST['latitude']
				longitude = request.POST['longitude']
				min=10000000

				logger = logging.getLogger(__name__)
				logging.basicConfig()
				logger.setLevel(logging.INFO)
				logger.info(Market.objects.all())


				for market in markets:
					dist = get_dist(float(latitude), float(longitude), float(market.latitude), float(market.longitude))
					if dist < min:
						min = dist
						closest_market = market	

				#print(closest_market)

				closest_market = json.dumps({'id': closest_market.market_id,
											'name': closest_market.market_name,
											'region': closest_market.region,
											'state': closest_market.state})

				return HttpResponse(closest_market, content_type = 'application/json')
			else:
				return HttpResponse("arguments aren't defined")
		else:
			return HttpResponse("latitude and longitude arguments necessary in request")

	else:
		return HttpResponse("Only POST request is accepted")

def item_home(request):

	'''
	if request.POST :
		if 'item_name' in request.POST.keys() :
			if request.POST['item_name']:
				items=Item.objects.all
				it_name=request.POST['item_name']
				if
				it_id=request.POST['']
				if x in item_name:
					it_id=item_id
				context={
					"queryset":queryset,
					"item_id":it_id,
				}
		if 'item_id' in request.POST.keys():
			if request.POST['item_id']:
				items	
	'''

	


def local_market_home(request):
	queryset=LocalMarketData.objects.all
'''return render(request,"",context)
'''
# Create your views here.
 <commit_msg>Update views for regional markets and :book: add documentation<commit_after>from django.http import HttpResponse , StreamingHttpResponse
from django.shortcuts import render
from django.core import serializers
import time, math
from .models import Market, Item, LocalMarketData
from decimal import Decimal
import sys

# to CSRF exempt 
from django.views.decorators.csrf import csrf_exempt

try:
    from django.utils import simplejson as json
except ImportError:
	import json

def get_dist(x1,y1,x2,y2):	
	return pow((pow(x1-x2,2)+pow(y1-y2,2)), 0.5)

def home(request):
	return HttpResponse("<h1>Home</h1>")

@csrf_exempt
def get_regional_markets_having_item(request):
	"""
		Returns the list of all markets which are having given item

	Args:
	    request (HttpRequest): HTTP Request coming from client's device. Expected to contain
	    POST request with following parameters -
	    region: String
	    item: String

	Returns:
	    HttpResponse: Response will be in JSON format containing list of details of Market. If
	    request is found to be malformed then appropriate response will be sent as error message.
	    """
	if request.method == "POST":
		pass
	else:
		return HttpResponse("Only POST request is accepted")

@csrf_exempt
def get_markets_in_region(request):
	"""
		Returns the list of all markets which are present in a given region
	
	Args:
	    request (HttpRequest): HTTP Request coming from client's device. Expected to contain
	    POST request with following parameters -
	    region: String
	
	Returns:
	    HttpResponse: Response will be in JSON format containing list of details of Market. If
	    request is found to be malformed then appropriate response will be sent as error message.
	"""
	if request.method == "POST":
		if 'region' in request.POST.keys():
			if request.POST['region']:
				region = request.POST['region']
				markets = list(Market.objects.filter(region = region))

				regional_markets = []

				for market in markets:
					regional_markets.append({'id': market.market_id,
											'name': market.market_name,
											'region': market.region,
											'state': market.state})

				regional_markets = json.dumps(regional_markets)
				return HttpResponse(regional_markets, content_type = 'application/json')
			else:
				return HttpResponse("arguments aren't defined")
		else:
			return HttpResponse("latitude and longitude arguments necessary in request")
	else:
		return HttpResponse("Only POST request is accepted")

@csrf_exempt
def get_nearby_markets(request):
	"""
		Returns the list of all markets which are in vicinity of current location
	
	Args:
	    request (HttpRequest): HTTP Request coming from client's device. Expected to contain
	    POST request with following parameters -
	    latitude: Decimal
	    longitude: Decimal
	
	Returns:
	    HttpResponse: Response will be in JSON format containing list of details of Market. If
	    request is found to be malformed then appropriate response will be sent as error message.
	"""
	if request.method == "POST":
		pass
	else:
		return HttpResponse("Only POST request is accepted")

@csrf_exempt
def get_closest_market(request):
	"""
		Returns the market which has minimum euclidean distance with the given latitude and longitude

	Args:
	    request (HttpRequest): HTTP Request coming from client's device. Expected to contain
	    POST request with following parameters -
	    latitude: Decimal
	    longitude: Decimal
	
	Returns:
	    HttpResponse: Response will be in JSON format containing details of Market. If
	    request is found to be malformed then appropriate response will be sent as error message.
	"""
	if request.method == "POST":
		if 'latitude' in request.POST.keys() and 'longitude' in request.POST.keys():
			if request.POST['latitude'] and request.POST['longitude']:
				markets = list(Market.objects.all())
				latitude = request.POST['latitude']
				longitude = request.POST['longitude']
				min_dist = sys.maxsize

				closest_market = min(markets, key = lambda market: 
					get_dist(float(latitude), float(longitude), float(market.latitude), float(market.longitude))
									)

				closest_market = json.dumps({'id': closest_market.market_id,
											'name': closest_market.market_name,
											'region': closest_market.region,
											'state': closest_market.state})

				return HttpResponse(closest_market, content_type = 'application/json')
			else:
				return HttpResponse("arguments aren't defined")
		else:
			return HttpResponse("latitude and longitude arguments necessary in request")

	else:
		return HttpResponse("Only POST request is accepted")
{'fraction_non_alphanumeric': 0.093835715316876, 'fraction_numerical': 0.00519705500216544, 'mean_word_length': 3.9521086490350252, 'pattern_counts': {'":': 6, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '1524008', 'n_tokens_mistral': 2372, 'n_tokens_neox': 1993, 'n_words': 641}