Skip to content

Commit

Permalink
1. 음식 데이터 추가
Browse files Browse the repository at this point in the history
2. 추천 로직 작동
3. 음식 데이터베이스 스키마 변경
  • Loading branch information
mksoo committed Feb 16, 2024
1 parent 10f5c5e commit 2c26c59
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 40 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,8 @@ fabric.properties
.idea/caches/build_file_checksums.ser

# erd mksoo ..
.erd
.erd

# mksoo images
/static/asset/food_images/
foods.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 5.0 on 2024-02-16 09:02

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("diet", "0003_remove_detailofdiet_image_x_and_more"),
]

operations = [
migrations.RemoveField(
model_name="food",
name="created_at",
),
migrations.RemoveField(
model_name="food",
name="deleted",
),
migrations.RemoveField(
model_name="food",
name="deleted_at",
),
migrations.RemoveField(
model_name="food",
name="image_path",
),
migrations.RemoveField(
model_name="food",
name="updated_at",
),
]
5 changes: 0 additions & 5 deletions diet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,8 @@ class DetailOfDiet(models.Model):
class Food(models.Model):
name = models.CharField(max_length=50, db_comment="음식 이름")
description = models.CharField(max_length=200, db_comment="음식에 대한 설명")
image_path = models.CharField(max_length=100, db_comment="음식 이미지 경로")
carbohydrate = models.FloatField(db_comment="1인분당 탄수화물")
protein = models.FloatField(db_comment="1인분당 단백질")
fat = models.FloatField(db_comment="1인분당 지방")
calorie = models.FloatField(db_comment="1인분당 칼로리")
is_main = models.BooleanField(default=False, db_comment="메인 음식 여부")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
deleted = models.BooleanField(default=False)
deleted_at = models.DateTimeField(null=True, blank=True)
42 changes: 42 additions & 0 deletions diet/static/diet/js/recommend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// .expanded 클래스를 가진 모든 요소에 대한 클릭 이벤트 리스너 추가
document.querySelectorAll('.expand').forEach(function(expandedElement) {
expandedElement.addEventListener('click', function() {
var nutritionElement = expandedElement.nextElementSibling;

// this의 자식 요소를 선택한다.
var icon = this.querySelector('.bi');


if (this.classList.contains('expanded')) {
// expanded 클래스 삭제
this.classList.remove('expanded');
// icon의 클래스를 변경
icon.classList.replace('bi-chevron-up', 'bi-chevron-down');

while(nutritionElement){
// nutritionElement를 안보이게 변경
nutritionElement.classList.replace('d-flex','d-none');
break;
nutritionElement = nutritionElement.nextElementSibling;
}
} else {
// 클릭된 .expanded 클래스 추가
this.classList.add('expanded');
// 아이콘 변경
icon.classList.replace('bi-chevron-down', 'bi-chevron-up');

while (nutritionElement) {
nutritionElement.classList.replace('d-none', 'd-flex');
break; // .nutrition 클래스를 가진 첫 번째 요소를 숨기고 반복 종료
nutritionElement = nutritionElement.nextElementSibling;
}
}


});
});

function pageReload() {
// 현재 페이지 새로고침
location.reload();
}
65 changes: 32 additions & 33 deletions diet/templates/diet/recommend.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,61 @@
<img src="{% static 'asset/banner_recommend.jpeg' %}" alt="배너" class="banner"/>
</div>



<!-- 콘텐츠 영역 -->
<div class="container">
<!-- 새로 추천 버튼-->
<div class="row mb-3">
<div class="column d-flex justify-content-end">
<button class="btn btn-outline-primary">
<button onclick="pageReload()" class="btn btn-outline-primary">
<i class="bi bi-arrow-clockwise"></i>
새로 추천
</button>
</div>
</div>

<!-- 리스트 시작-->
<div class="row border border-bottom-0 border-start-0 border-end-0 justify-content-center">
<!-- 아이템 반복문 시작 1-->
<div class="row border border-top-0 border-start-0 border-end-0 justify-content-center py-3">
<div class="row py-3">
<div class="col-5 text-center">
<img src="{% static 'asset/food_image.png' %}" class="recom-food-img rounded-circle"/>
</div>
<div class="col-7">
<div class="row p-0">
<h2>보리밥</h2>
{% for food in food_list %}
<div class="row border border-top-0 border-start-0 border-end-0 justify-content-center py-3">
<div role="button" class="row py-3 expand">
<div class="col-5 text-center">
<img src="/static/asset/food_images/{{food.name}}.jpg" class="recom-food-img rounded-circle"/>
</div>
<div class="row grayscale-02 mb-1 border border-start-0 border-end-0 border-top-0">
<span>쌀에 보리를 섞어 식감이 좋고 영양이 풍부한 식단이에요.</span>
</div>
<div class="row d-flex justify-content-between">
<div class="col-7">
영양성분 보기 <i class="bi bi-chevron-up"></i>
<div class="col-7">
<div class="row p-0">
<h2>{{food.name}}</h2>
</div>
<div class="row grayscale-02 mb-1 border border-start-0 border-end-0 border-top-0">
<span>{{food.description}}</span>
</div>
<div class="col-5">
316 Kcal
<div class="row d-flex justify-content-between">
<div class="col-7">
영양성분 보기 <i class="bi bi-chevron-down"></i>
</div>
<div class="col-5">
{{food.calorie}} Kcal
</div>
</div>
</div>
</div>
</div>
<!-- 숨김 처리되는 영역-->
<div class="row justify-content-center">
<div class="row mb-3">
<div class="d-flex align-items-center mt-2 summary-box py-3">
<span class="nutrition-status badge bg-warning me-5"> 부족</span>
<span class="grayscale-02">탄수화물 118g · 단백질 31g · 지방 22g</span>
<!-- 숨김 처리되는 영역-->
<div class="row justify-content-center nutrition d-none">
<div class="row mb-3">
<div class="d-flex align-items-center mt-2 summary-box py-3">
<span class="nutrition-status badge bg-warning me-5"> 부족</span>
<span class="grayscale-02">탄수화물 {{food.carbohydrate}}g · 단백질 {{food.protein}}g · 지방 {{food.fat}}g</span>
</div>
</div>
</div>

<div class="row">
<input type="button" class="btn btn-primary py-2" value="맞춤 식단 기록하기"/>
<div class="row">
<input type="button" class="btn btn-primary py-2" value="맞춤 식단 기록하기"/>
</div>
</div>
</div>
</div>
</div>
{% endfor %}

</div>
</div>

</div>
{% endblock main %}
{% block script %}
Expand Down
22 changes: 21 additions & 1 deletion diet/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
from django.shortcuts import render

from .models import Food

# for test
CALORIE_LIMIT = 2000
MORNING = 1000
LUNCH = 500
DINNER = 0


# Create your views here.
def recommend(request):
return render(request, "diet/recommend.html", {})
# 잔여 칼로리
remaining_calorie = CALORIE_LIMIT - MORNING - LUNCH - DINNER

# 잔여 칼로리 이하의 음식 리스트, 랜덤 정렬해서 상위 3개 추출
food_list = Food.objects.filter(
calorie__lte=remaining_calorie, is_main=True
).order_by("?")[:3]

# food의 name, description, image_path, calorie, carbohydrate, protein, fat 정보를 추출해서 프린트
for food in food_list:
food.calorie = int(round(food.calorie, 0))

return render(request, "diet/recommend.html", {"food_list": food_list})


def record_list(request):
Expand Down

0 comments on commit 2c26c59

Please sign in to comment.