-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
models.py and documents.py expand #56
Changes from 2 commits
ebf5bc6
ea6c19a
34dbef5
95fd72a
48ac18e
78c1a93
064aeff
28ea78a
7a0e3ce
430d245
6b55057
34c0039
754c89a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這邊可以參考目前 Master branch 的寫法,先製作 Serializer(DTO 的概念),就不需要自行定義輸入資料 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 這些 low moderate high 已經說明在 Model 裡面 Django Model 是否會對於這些不相對應的值進行處理? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still a problem |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,19 @@ class PetCreateAPIView(APIView): | |
'content': openapi.Schema( | ||
type=openapi.TYPE_STRING | ||
), | ||
'size': openapi.Schema( | ||
type=openapi.TYPE_STRING | ||
), | ||
'weight': openapi.Schema( | ||
type=openapi.TYPE_NUMBER | ||
), | ||
'gender': openapi.Schema( | ||
type=openapi.TYPE_STRING | ||
), | ||
'is_neutered': openapi.Schema( | ||
type=openapi.TYPE_BOOLEAN | ||
), | ||
|
||
} | ||
) | ||
) | ||
|
@@ -78,16 +91,28 @@ def post(self, request, *args, **kwargs): | |
petTypeId = request.data.get("type", 0) | ||
birthday = request.data.get("birthday", datetime.date.today()) | ||
content = request.data.get("content", "") | ||
size = request.data.get("size", "") # 取得新增欄位 size 的值 | ||
weight = request.data.get("weight", 0) # 取得新增欄位 weight 的值 | ||
gender = request.data.get("gender", "") # 取得新增欄位 gender 的值 | ||
is_neutered = request.data.get("is_neutered", False) # 取得新增欄位 is_neutered 的值 | ||
|
||
keeper = User.objects.get(pk=keeperId) | ||
petType = models.PetType.objects.get(pk=petTypeId) | ||
|
||
# 計算每日熱量需求DER | ||
der = calculate_daily_energy_requirement(weight) | ||
|
||
pet = models.Pet.objects.create( | ||
name=name, | ||
keeper=keeper, | ||
type=petType, | ||
birthday=birthday, | ||
content=content | ||
content=content, | ||
size=size, # 使用新增欄位 size 的值 | ||
weight=weight, # 使用新增欄位 weight 的值 | ||
gender=gender, # 使用新增欄位 gender 的值 | ||
is_neutered=is_neutered, # 使用新增欄位 is_neutered 的值 | ||
der=der, # 儲存計算得到的 DER | ||
) | ||
return Response(data=petResponseConverter(pet), status=status.HTTP_201_CREATED) | ||
except Exception as e: | ||
|
@@ -99,8 +124,8 @@ class PetCountAPIView(APIView): | |
def get(self, request, *args, **kwargs): | ||
petDict = dict() | ||
for i in models.PetType.objects.all(): | ||
petDict[i.typename]= models.Pet.objects.filter(type=i.id).count() | ||
return Response(data=petDict,status=status.HTTP_200_OK) | ||
petDict[i.typename] = models.Pet.objects.filter(type=i.id).count() | ||
return Response(data=petDict, status=status.HTTP_200_OK) | ||
|
||
|
||
def petResponseConverter(pet: models.Pet): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不需要這個 改用 Serializer 處理 |
||
|
@@ -128,3 +153,18 @@ def petTypeResponseConverter(petType: models.PetType): | |
else: | ||
result = "" | ||
return result | ||
|
||
|
||
def calculate_resting_energy_requirement(weight): | ||
fan9704 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 70 * (weight ** 0.75) | ||
|
||
|
||
def calculate_daily_energy_requirement(weight, activity_level): | ||
if activity_level == 'low': | ||
return 1.2 * calculate_resting_energy_requirement(weight) | ||
elif activity_level == 'moderate': | ||
return 1.4 * calculate_resting_energy_requirement(weight) | ||
elif activity_level == 'high': | ||
return 1.6 * calculate_resting_energy_requirement(weight) | ||
else: | ||
raise ValueError("Invalid activity level") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
這裡就不需要寫入欄位
僅會放入需要關聯的欄位