Skip to content

Commit bbee69b

Browse files
committed
[refactory] use decorator to reduce duplicate code
1 parent d5065df commit bbee69b

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

segmentoj/decorator.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ def _wrapped_view(request, *args, **kwargs):
99
r = data.get(syllable_id)
1010
if r == None:
1111
return Response(
12-
{"detail": "syllable {sid} is required".format(sid=syllable_id)},
12+
{'detail': 'syllable {sid} is required'.format(sid=syllable_id)},
1313
status=status.HTTP_400_BAD_REQUEST,
1414
)
1515

1616
if syllable_type != None and type(r) != syllable_type:
1717
return Response(
18-
{"detail": "syllable {sid} has wrong type".format(sid=syllable_id)},
18+
{'detail': 'syllable {sid} has wrong type'.format(sid=syllable_id)},
1919
status=status.HTTP_400_BAD_REQUEST,
2020
)
2121

@@ -31,7 +31,7 @@ def _wrapped_view(request, *args, **kwargs):
3131
target = kwargs.get(parameter_id)
3232
if target == None:
3333
return Response(
34-
{"detail": "{pid} in URI is required".format(pid=parameter_id)},
34+
{'detail': '{pid} in URI is required'.format(pid=parameter_id)},
3535
status=status.HTTP_404_NOT_FOUND,
3636
)
3737

@@ -47,11 +47,29 @@ def decorator(func):
4747
def _wrapped_view(request, *args, **kwargs):
4848
if not request.user.is_authenticated:
4949
return Response(
50-
{"detail": "Login Required"}, status=status.HTTP_401_UNAUTHORIZED
50+
{'detail': 'Login Required'}, status=status.HTTP_401_UNAUTHORIZED
5151
)
5252

5353
return func(request, *args, **kwargs)
5454

5555
return _wrapped_view
5656

5757
return decorator
58+
59+
def parse_as_integer(sid):
60+
def decorator(func):
61+
def _wrapped_view(request, *args, **kwargs):
62+
data = request.GET.copy()
63+
if data.get(sid) is not None:
64+
if data[sid].isdecimal():
65+
data[sid] = int(data[sid])
66+
else:
67+
del data[sid]
68+
69+
request.GET = data
70+
71+
return func(request, *args, **kwargs)
72+
73+
return _wrapped_view
74+
75+
return decorator

segmentoj/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
'rest_framework.throttling.ScopedRateThrottle', # use throttle_scope = 'xxx'
145145
),
146146
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
147-
'PAGE_SIZE': 1,
147+
'PAGE_SIZE': 5,
148148
}
149149

150150
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

status/views.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .models import Status
1111
from .serializers import StatusSerializer, StatusListSerializer
1212
from problem.models import Problem
13-
from segmentoj.decorator import login_required, syllable_required, parameter_required
13+
from segmentoj.decorator import login_required, syllable_required, parameter_required, parse_as_integer
1414
from . import JudgeLang as JLang
1515
from . import JudgeState as JState
1616

@@ -90,6 +90,10 @@ def cannot_judge(reason, state=JState.JUDGE_STATUS_CFGE):
9090

9191

9292
class StatusListView(APIView):
93+
94+
@method_decorator(parse_as_integer('problem'))
95+
@method_decorator(parse_as_integer('lang'))
96+
@method_decorator(parse_as_integer('owner'))
9397
def get(self, request):
9498
# Status List
9599

@@ -102,18 +106,15 @@ def process(x):
102106
data = request.GET
103107

104108
if data.get('problem') is not None:
105-
if data['problem'].isdecimal():
106-
status_filter['problem'] = get_object_or_404(Problem, pid=int(data['problem'])).id
109+
status_filter['problem'] = get_object_or_404(Problem, pid=data['problem']).id
107110

108111
if data.get('lang') is not None:
109-
if data['lang'].isdecimal():
110-
status_filter['lang'] = int(data['lang'])
112+
status_filter['lang'] = data['lang']
111113

112114
if data.get('owner') is not None:
113-
if data['owner'].isdecimal():
114-
status_filter['owner'] = int(data['owner'])
115+
status_filter['owner'] = data['owner']
115116

116-
queryset = Status.objects.filter(**status_filter).order_by('-add_time')
117+
queryset = Status.objects.filter(**status_filter).order_by('-id')
117118

118119
pg = LimitOffsetPagination()
119120
statuses = pg.paginate_queryset(queryset=queryset, request=request, view=self)

0 commit comments

Comments
 (0)