from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
import json
import pandas as pd
import re
from django.http import JsonResponse
from django.http import HttpResponse, HttpResponseNotFound
from json import loads
from math import ceil
def readCsv(option, search):
df = pd.read_csv(f'{option}.csv', header=None)
df.columns = ["name", "image", "listLlink",
"link", "price", "discount", "shop"]
res = df[df.name.str.contains(
search, regex=True, flags=re.IGNORECASE)]
res['name'] = df['name'].str.partition('[')[0]
return res.sort_values(by='price', ascending=True)
def toPercent(price, discount):
return ceil(((price - discount) / price) * 100)
def whiteList(res):
model = {'r3': "ryzen.3", 'r5': "ryzen.5", 'r7': "ryzen.7", 'i3': "i3", 'i5': "i5", 'i7': "i7", 'i9': "i9",
'1200': 'lga.+1200', '1700': 'lga.+1700', 'am4': 'am4', 'am5': 'am5', 'oem': 'oem', 'box': 'box'}
if model.get(res):
return model[res]
@require_http_methods(["GET"])
def cpu(request):
data = json.loads(readCsv('cpu', '.').to_json(orient='records'))
return render(request, 'home/index.html', {"data": data})
@require_http_methods(["GET"])
def modelCpu(request, model):
if (whiteList(model)):
data = json.loads(readCsv('cpu', whiteList(model)
).to_json(orient='records'))
return render(request, 'home/index.html', {"data": data})
return HttpResponseNotFound("<h1>Page not found</h1>")
@require_http_methods(["GET"])
def motherboard(request):
data = json.loads(readCsv('motherboard', '.').to_json(orient='records'))
return render(request, 'home/index.html', {"data": data})
@require_http_methods(["GET"])
def coolingPage(request):
data = json.loads(readCsv('cooling', '.').to_json(orient='records'))
return render(request, 'cooling/index.html', {"data": data})