Skip to content

Commit

Permalink
関数ベースビューをクラスベースビューにリファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
duri0214 committed Jan 6, 2024
1 parent 8e44905 commit 044e996
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion vietnam_research/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""フォーム集積場所"""
from django import forms

from .models import Articles, Watchlist, FinancialResultWatch


Expand Down Expand Up @@ -27,7 +28,6 @@ class WatchlistCreateForm(forms.ModelForm):
class Meta:
model = Watchlist
fields = ('symbol', 'bought_day', 'stocks_price', 'stocks_count')
exclude = ('already_has',)


class ArticleForm(forms.ModelForm):
Expand Down
4 changes: 2 additions & 2 deletions vietnam_research/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.urls import path

from vietnam_research.views import ArticleCreateView, index, WatchlistRegister, WatchlistEdit, \
from vietnam_research.views import ArticleCreateView, IndexView, WatchlistRegister, WatchlistEdit, \
FinancialResultsListView, FinancialResultsDetailListView, FinancialResultsCreateView, LikesView

app_name = 'vnm'
urlpatterns = [
path('', index, name='index'),
path('', IndexView.as_view(), name='index'),
path('likes/<int:article_id>/<int:user_id>/', LikesView.as_view(), name='likes'),
path('article/create/', ArticleCreateView.as_view(), name="article_create"),
path('watchlist/register/', WatchlistRegister.as_view(), name="watchlist_register"),
Expand Down
88 changes: 38 additions & 50 deletions vietnam_research/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
from datetime import datetime

from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Count, Sum, F
Expand All @@ -9,34 +8,51 @@
from django.urls import reverse_lazy
from django.utils.http import urlencode
from django.views import View
from django.views.generic import CreateView, ListView, UpdateView
from django.views.generic import CreateView, ListView, UpdateView, TemplateView

from register.models import User
from vietnam_research.forms import ArticleForm, WatchlistCreateForm, ExchangeForm, FinancialResultsForm
from vietnam_research.models import Watchlist, Likes, Articles, FinancialResultWatch, BasicInformation
from vietnam_research.service.market_vietnam import MarketVietnam


def index(request):
"""いわばhtmlのページ単位の構成物です"""
class IndexView(TemplateView):
template_name = 'vietnam_research/index.html'

# GETだったら MarketVietnam(), nasdaqが選ばれたらMarketNasdaq()
mkt = MarketVietnam()
def get(self, request, *args, **kwargs):
mkt = MarketVietnam()
exchanged = {}

exchanged = {}
if request.method == 'POST':
# ウォッチリスト登録処理
watchlist_form = WatchlistCreateForm(request.POST)
if watchlist_form.is_valid():
watchlist = Watchlist()
watchlist.symbol = watchlist_form.cleaned_data['buy_symbol']
watchlist.already_has = True
watchlist.bought_day = watchlist_form.cleaned_data['buy_date']
watchlist.stocks_price = watchlist_form.cleaned_data['buy_cost']
watchlist.stocks_count = watchlist_form.cleaned_data['buy_stocks']
watchlist.bikou = watchlist_form.cleaned_data['buy_bikou']
watchlist.save()
return redirect('vnm:index')
exchange_form = ExchangeForm()
params = ['current_balance', 'unit_price', 'quantity', 'price_no_fee', 'fee', 'price_in_fee', 'deduction_price']

for param in params:
if param in request.GET:
exchanged[param] = request.GET.get(param)

login_user = self.request.user
login_id = login_user.id if login_user.is_authenticated else None

# TODO: articlesは試作のため3投稿のみ
context = {
'industry_count': json.dumps(mkt.radar_chart_count()),
'industry_cap': json.dumps(mkt.radar_chart_cap()),
'vnindex_timeline': json.dumps(mkt.vnindex_timeline()),
'vnindex_layers': json.dumps(mkt.vnindex_annual_layers()),
'articles': Articles.with_state(login_id).annotate(user_name=F('user__email')).order_by('-created_at')[:3],
'basicinfo': BasicInformation.objects.order_by('id').values('item', 'description'),
'watchlist': mkt.watchlist(),
'sbi_topics': mkt.sbi_topics(),
'uptrends': json.dumps(mkt.uptrends()),
'exchange_form': exchange_form,
'exchanged': exchanged
}

return render(request, self.template_name, context)

def post(self, request, *args, **kwargs):
mkt = MarketVietnam()
exchanged = {}

# 為替計算処理
exchange_form = ExchangeForm(request.POST)
Expand All @@ -52,36 +68,7 @@ def index(request):
response['location'] += '?' + urlencode(exchanged)
return response

else:
exchange_form = ExchangeForm()
params = ['current_balance', 'unit_price', 'quantity', 'price_no_fee', 'fee', 'price_in_fee', 'deduction_price']
for param in params:
if param in request.GET:
exchanged[param] = request.GET.get(param)
watchlist_form = WatchlistCreateForm()
watchlist_form.buy_date = datetime.today().strftime("%Y/%m/%d")

login_user = User.objects.filter(email=request.user).first()
login_id = None
if login_user:
login_id = login_user.id

# TODO: articlesは試作のため3投稿のみ
context = {
'industry_count': json.dumps(mkt.radar_chart_count()),
'industry_cap': json.dumps(mkt.radar_chart_cap()),
'vnindex_timeline': json.dumps(mkt.vnindex_timeline()),
'vnindex_layers': json.dumps(mkt.vnindex_annual_layers()),
'articles': Articles.with_state(login_id).annotate(user_name=F('user__email')).order_by('-created_at')[:3],
'basicinfo': BasicInformation.objects.order_by('id').values('item', 'description'),
'watchlist': mkt.watchlist(),
'sbi_topics': mkt.sbi_topics(),
'uptrends': json.dumps(mkt.uptrends()),
'exchange_form': exchange_form,
'exchanged': exchanged
}

return render(request, 'vietnam_research/index.html', context)
return render(request, self.template_name, {'exchanged': exchanged})


class LikesView(LoginRequiredMixin, View):
Expand Down Expand Up @@ -133,6 +120,7 @@ class WatchlistRegister(CreateView):

def form_valid(self, form):
form.instance.already_has = 1
form.instance.user_id = self.request.user.id
return super().form_valid(form)


Expand Down

0 comments on commit 044e996

Please sign in to comment.