Skip to content

Commit

Permalink
summaries swagger api markdown info
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikJiang committed May 16, 2018
1 parent 3f14d62 commit dbb358e
Showing 1 changed file with 82 additions and 53 deletions.
135 changes: 82 additions & 53 deletions apps/summaries/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,6 @@
from apps.bills.serializers import BillSerializer
from apps.bills.models import Bills

# request: {
# date: 'YYYY or YYYY-MM'
# }

# 概要信息
# response: {
# date: 'YYYY or YYYY-MM',
# income_amount: '收入金额',
# outgo_amount: '支出金额',
# balance_amount: '结余金额'
# }

# 折线信息
# response: {
# income_data: [
# {
# date: 'YYYY-MM or YYYY-MM-DD',
# amount: '金额'
# }
# ],
# outgo_data: [
# {
# date: 'YYYY-MM or YYYY-MM-DD',
# amount: '金额'
# }
# ]
# }

# 环状信息
# response: {
# income_data: [
# {
# category: '类别',
# amount: '金额'
# }
# ],
# outgo_data: [
# {
# category: '类别',
# amount: '金额'
# }
# ]
# }


class CustomAutoSchema(AutoSchema):
def get_link(self, path, method, base_url):
link = super().get_link(path, method, base_url)
Expand Down Expand Up @@ -102,30 +57,78 @@ def get_queryset(self):
def info(self, request):
"""
概要信息
---
## request:
|参数名|描述|请求类型|参数类型|备注|
|:----:|:----:|:----:|:----:|:----:|
| time_type | 时间类型 | query | string | `YEAR` or `MONTH` |
| time_value | 时间值 | query | string | `YYYY` or `YYYY-MM`|
## response:
``` json
{
"time": "统计时间",
"income_amount": "收入数额",
"outgo_amount": "支出数额",
"balance_amount": "结余数额"
}
"""
query = self.get_queryset()

result = query.values('bill_type').annotate(amount_sum=Sum('amount')).order_by()
print(result.query)
print(result)

income_amount = result.get(bill_type=1)['amount_sum']
# outgo_amount = result.get(bill_type=0)
print(income_amount)
# print(outgo_amount)
# todo
try:
income_amount = result.get(bill_type=1)['amount_sum']
except Bills.DoesNotExist:
income_amount = 0
try:
outgo_amount = result.get(bill_type=0)['amount_sum']
except Bills.DoesNotExist:
outgo_amount = 0

res_dict = {
"time": request.query_params.get('time_value'),
"income_amount": '收入金额',
"outgo_amount": '支出金额',
"balance_amount": '结余金额'
"income_amount": income_amount,
"outgo_amount": outgo_amount,
"balance_amount": income_amount - outgo_amount
}
return Response(data=res_dict, status=status.HTTP_200_OK)

@action(methods=['GET'], detail=False)
def linechart(self, request):
"""
折线图
---
## request:
|参数名|描述|请求类型|参数类型|备注|
|:----:|:----:|:----:|:----:|:----:|
| time_type | 时间类型 | query | string | `YEAR` or `MONTH` |
| time_value | 时间值 | query | string | `YYYY` or `YYYY-MM`|
## response:
``` json
{
"income_data": [
{
"date": "YYYY-MM or YYYY-MM-DD",
"amount": "金额"
}
],
"outgo_data": [
{
"date": "YYYY-MM or YYYY-MM-DD",
"amount": "金额"
}
]
}
"""
serializer = SummariesSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
Expand All @@ -136,6 +139,32 @@ def linechart(self, request):
def ringchart(self, request):
"""
环状图
---
## request:
|参数名|描述|请求类型|参数类型|备注|
|:----:|:----:|:----:|:----:|:----:|
| time_type | 时间类型 | query | string | `YEAR` or `MONTH` |
| time_value | 时间值 | query | string | `YYYY` or `YYYY-MM`|
## response:
``` json
{
"income_data": [
{
"category": "类别",
"amount": "金额"
}
],
"outgo_data": [
{
"category": "类别",
"amount": "金额"
}
]
}
"""
serializer = SummariesSerializer(data=request.query_params)
serializer.is_valid(raise_exception=True)
Expand Down

0 comments on commit dbb358e

Please sign in to comment.