@Speakermen

Я могу разделить на controller service repository Django?

Этот фреймворк же не вводит ограничения? У меня во views.py много бизнес логики не относящей к controller
хотелось бы вынести в service

Controller)

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})



?plugin=attach&refer=SpringBoot%2F%E5%90%84%E3%83%AC%E3%82%A4%E3%83%A4%E3%81%AE%E8%B2%AC%E5%8B%99&openfile=spring_layered_architecture2.png
  • Вопрос задан
  • 92 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы