Skip to content

Commit

Permalink
Merge branch 'master' into warningSerialize
Browse files Browse the repository at this point in the history
  • Loading branch information
uyw4687 authored Dec 9, 2019
2 parents 01765b1 + 8f176d4 commit 1d5faa2
Show file tree
Hide file tree
Showing 19 changed files with 338 additions and 194 deletions.
101 changes: 62 additions & 39 deletions back/matchmaker/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ def test_http_response_405(self):
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 405)

response = client.post('/api/match/3/',
json.dumps({'title': 'TEST_TITLE', }),
content_type='application/json',
HTTP_X_CSRFTOKEN=csrftoken)
response = client.put('/api/match/3/',
json.dumps({'title': 'TEST_TITLE', }),
content_type='application/json',
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 405)

response = client.delete(f'/api/match/{test_match.id}/join/',
Expand Down Expand Up @@ -322,27 +322,27 @@ def test_http_response_400(self):
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 400)

response = client.put(f'/api/match/{test_match.id}/',
json.dumps({'title': 'TEST_TITLE',
'category': '0,0',
'capacity': 'TEST_ERR_STR',
'locationText': 'TEST_LOCATION_TEXT',
'period': 'TEST_ERR_STR',
'additionalInfo': 'TEST_ADDITIONAL_INFO',
'isAgeRestricted': 'TEST_ERR_STR',
'restrictAgeFrom': 'TEST_ERR_STR',
'restrictAgeTo': 'TEST_ERR_STR',
'isGenderRestricted': 'TEST_ERR_STR',
'restrictedGender': settings.MALE,
'timeBegin': '2019-11-03T08:07:46+09:00',
'timeEnd': '2019-11-03T08:07:46+09:00', }),
content_type='application/json', HTTP_X_CSRFTOKEN=csrftoken)
response = client.post(f'/api/match/{test_match.id}/',
json.dumps({'title': 'TEST_TITLE',
'category': '0,0',
'capacity': 'TEST_ERR_STR',
'locationText': 'TEST_LOCATION_TEXT',
'period': 'TEST_ERR_STR',
'additionalInfo': 'TEST_ADDITIONAL_INFO',
'isAgeRestricted': 'TEST_ERR_STR',
'restrictAgeFrom': 'TEST_ERR_STR',
'restrictAgeTo': 'TEST_ERR_STR',
'isGenderRestricted': 'TEST_ERR_STR',
'restrictedGender': settings.MALE,
'timeBegin': '2019-11-03T08:07:46+09:00',
'timeEnd': '2019-11-03T08:07:46+09:00', }),
content_type='application/json', HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 400)

response = client.put(f'/api/match/{test_match.id}/',
json.dumps({'x': 'hello', 'y': 'match'}),
content_type='application/json',
HTTP_X_CSRFTOKEN=csrftoken)
response = client.post(f'/api/match/{test_match.id}/',
json.dumps({'x': 'hello', 'y': 'match'}),
content_type='application/json',
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 400)
response = client.post('/api/match/nlp/',
json.dumps({'x': 'hello'}),
Expand Down Expand Up @@ -403,24 +403,47 @@ def test_match(self):
f'/api/match/{match_id}/', HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(test_match.view_count, 1)

response = client.put(f'/api/match/{match_id}/',
json.dumps({'title': 'TEST_PUT_TITLE',
'category': '0,0',
'capacity': 4,
'locationText': 'TEST_PUT_LOCATION_TEXT',
'period': 3,
'additionalInfo': 'TEST_PUT_ADDITIONAL_INFO',
'isAgeRestricted': True,
'restrictAgeFrom': 4,
'restrictAgeTo': 7,
'isGenderRestricted': True,
'restrictedGender': settings.MALE,
'timeBegin': '2019-11-03T08:07:46+09:00',
'timeEnd': '2019-11-03T08:07:46+09:00', }),
content_type='application/json',
HTTP_X_CSRFTOKEN=csrftoken)
with tempfile.NamedTemporaryFile(suffix='.jpg') as temp_file:
test_image = Image.new('RGB', (100, 100))
test_image.save(temp_file)
temp_file.seek(0)

response = client.post(f'/api/match/{match_id}/',
data=form,
content_type=MULTIPART_CONTENT,
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 200)

with tempfile.NamedTemporaryFile() as temp_file:
form = encode_multipart(BOUNDARY, {
'matchThumbnail': temp_file,
'title': 'TEST_TITLE',
'category': '0,0',
'capacity': 5,
'locationText': 'TEST_LOCATION_TEXT',
'period': 3,
'additionalInfo': 'TEST_ADDITIONAL_INFO',
'isAgeRestricted': True,
'restrictAgeFrom': 4,
'restrictAgeTo': 7,
'isGenderRestricted': True,
'restrictedGender': settings.MALE,
'timeBegin': '2019-11-03T08:07:46+09:00',
'timeEnd': '2019-11-03T08:07:46+09:00',
})
response = client.post(f'/api/match/{match_id}/',
data=form,
content_type=MULTIPART_CONTENT,
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 400)

client2 = Client()
response = client2.post(f'/api/match/{match_id}/',
data=form,
content_type=MULTIPART_CONTENT,
HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 403)

@patch.object(nlp, 'enums', mock.Mock(side_effect=GoogleCloudLanguageMock.enums))
@patch.object(nlp, 'types', mock.Mock(side_effect=GoogleCloudLanguageMock.types))
@patch.object(nlp, 'LanguageServiceClient',
Expand Down
70 changes: 30 additions & 40 deletions back/matchmaker/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
'''
import json
from django.http import HttpResponse, HttpResponseBadRequest, \
HttpResponseNotAllowed, JsonResponse
HttpResponseNotAllowed, HttpResponseForbidden, JsonResponse
from django.shortcuts import get_object_or_404
from django.contrib.auth import get_user_model
import arrow
from djangorestframework_camel_case.parser import CamelCaseJSONParser
from djangorestframework_camel_case.util import underscoreize
from djangorestframework_camel_case.render import CamelCaseJSONRenderer

Expand All @@ -30,29 +29,32 @@ def match_simple_serializer(match_object):
'numParticipants': match_object.participation_match.all().count(),
}

def process_post_request(request):
'''Process POST requests with match information'''
try:
data = underscoreize(request.POST)
category_idx = data['category']
time_begin = arrow.get(data['time_begin']).datetime
time_end = arrow.get(data['time_end']).datetime
data['time_begin'] = time_begin
data['time_end'] = time_end
except (KeyError, arrow.parser.ParserError):
return HttpResponseBadRequest()
try:
data['match_thumbnail'] = request.FILES['matchThumbnail']
except KeyError:
pass
category = get_object_or_404(Category, indexes=category_idx)
data['category'] = category
data['host_user_id'] = request.user.id
data = data.dict()
return data

def match(request):
'''Makes and returns a new match.'''
if request.method == 'POST':
try:
data = underscoreize(request.POST)
category_idx = data['category']
time_begin = arrow.get(data['time_begin']).datetime
time_end = arrow.get(data['time_end']).datetime
data['time_begin'] = time_begin
data['time_end'] = time_end
except (KeyError, arrow.parser.ParserError):
return HttpResponseBadRequest()
try:
data['match_thumbnail'] = request.FILES['matchThumbnail']
except KeyError:
pass
category = get_object_or_404(Category, indexes=category_idx)
data['category'] = category
data['host_user_id'] = request.user.id
data = data.dict()
data = process_post_request(request)
match_serializer = MatchSerializer(data=data)

if match_serializer.is_valid():
new_match_obj = match_serializer.create(data)
participation = Participation(user=USER.objects.get(
Expand All @@ -64,10 +66,8 @@ def match(request):
response = json.loads(
CamelCaseJSONRenderer().render(new_match_data))
return JsonResponse(response, status=201)
# 400
# print(match_serializer.errors) need to return error info
return HttpResponseBadRequest()
# 405
return HttpResponseNotAllowed(['POST'])


Expand Down Expand Up @@ -118,34 +118,24 @@ def match_detail(request, match_id):
CamelCaseJSONRenderer().render(match_json))
return JsonResponse(match_json, safe=False, status=200)

if request.method == 'PUT':
if request.method == 'POST':
match_obj = get_object_or_404(Match, pk=match_id)
# add author check below after implementing login
# and putting author in the match model
try:
data = CamelCaseJSONParser().parse(request)
category_idx = data['category']
time_begin = arrow.get(data['time_begin']).datetime
time_end = arrow.get(data['time_end']).datetime
data['time_begin'] = time_begin
data['time_end'] = time_end
except (KeyError, arrow.parser.ParserError):
# 400
return HttpResponseBadRequest()
category = get_object_or_404(Category, indexes=category_idx)
data['category'] = category
if match_obj.host_user != request.user:
return HttpResponseForbidden()

data = process_post_request(request)
match_serializer = MatchSerializer(
match_obj, data=data, partial=True)
if match_serializer.is_valid():
match_obj = match_serializer.save()
match_data = match_serializer.validated_data
match_data.update({"pk": match_obj.pk})
match_data.update({"id": match_obj.pk,
'num_participants': match_obj.participation_match.all().count()})
response = json.loads(
CamelCaseJSONRenderer().render(match_data))
return JsonResponse(response, status=200)
# 400
return HttpResponseBadRequest()
return HttpResponseNotAllowed(['GET', 'PUT', 'PATCH', 'DELETE'])
return HttpResponseNotAllowed(['GET', 'POST', 'PATCH', 'DELETE'])


def match_join(request, match_id):
Expand Down
2 changes: 1 addition & 1 deletion back/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ colorama==0.4.1
coverage==4.5.4
coveralls==1.8.2
decorator==4.4.1
Django==2.2.5
Django==2.2.8
django-cors-headers==3.1.1
django-mysql==3.2.0
django-rest-framework==0.1.0
Expand Down
6 changes: 2 additions & 4 deletions back/userapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

USER = get_user_model()


@ensure_csrf_cookie
def token(request):
''' get csrf token '''
if request.method == 'GET':
return HttpResponse(status=204)
return HttpResponseNotAllowed(['GET'])


# @csrf_exempt
def sign_up(request):
''' sign up '''
if request.method == 'POST':
Expand All @@ -40,7 +39,6 @@ def sign_up(request):
# 405
return HttpResponseNotAllowed(['POST'])


@csrf_exempt
@ensure_csrf_cookie
def sign_in(request):
Expand All @@ -57,7 +55,7 @@ def sign_in(request):
# 405
return HttpResponseNotAllowed(['POST'])


@csrf_exempt
def sign_out(request):
''' sign out '''
if request.method == 'POST':
Expand Down
4 changes: 3 additions & 1 deletion front/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@

.App {
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-weight: bold;
}
footer {
margin-top: 40px;
}
2 changes: 1 addition & 1 deletion front/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import UserProfile from './containers/User/UserProfile/UserProfile';
function App(props) {
const { history } = props;
return (
<div className="App" style={{ backgroundColor: '#ffdddd' }}>
<div className="App" style={{ backgroundColor: 'powderblue' }}>
<ConnectedRouter history={history}>
<Header />
<Switch>
Expand Down
64 changes: 61 additions & 3 deletions front/src/components/Match/MatchForm/MatchForm.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
.MatchForm {
max-width: 430px;
max-width: 800px;
margin: 20px auto 0;
padding: 20px;
border: 1px solid lightgrey;
padding: 10px 20px 20px 20px;
border: 1px solid rgb(1, 121, 151);
border-width: 4px;
border-radius: 5px;
font-size: 20px;
height: 1680px;
}
.Buttons {
display: flex;
Expand All @@ -13,3 +16,58 @@
.ant-form-item {
margin-bottom: 5px !important;
}
.ant-form label {
font-size: 25px;
}
.ant-input {
height: 40px;
margin-bottom: 10px;
font-size: 20px;
}

.ant-form-item-label {
margin-top: 15px;
}
.ant-form-item-label > label::after {
content: '';
}
.ant-cascader-picker {
height: 40px;
}
.CreateButton {
width: 170px;
height: 60px;
font-size: 25px;
}
.EditButton {
width: 170px;
height: 60px;
font-size: 25px;
}
#cancel-button {
width: 170px;
height: 60px;
font-size: 25px;
}

.ant-form-explain {
font-size: 20px;
}
.ant-form-item-children {
font-size: 20px;
}
.ant-input-number {
margin: 0px 10px;
}
.ant-input-number-input {
font-size: 15px;
}
#ImageUpload {
width: 300px;
height: 300px;
font-size: 30px;
padding-top: 85px;
}
li {
font-size: 17px;
}
4 changes: 2 additions & 2 deletions front/src/components/Match/MatchForm/MatchForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const MatchForm = ({
}}
/>
</Form.Item>
<FormItem name="capacity" label="Capacity">
<FormItem name="capacity" label="Max Capacity">
Up to
<InputNumber name="capacity" min={2} />
people
Expand Down Expand Up @@ -144,7 +144,7 @@ const MatchForm = ({
</div>
<div className="Buttons">
{submitButton}
<Button id="cancel-button" onClick={clickCancel}>
<Button id="cancel-button" type="danger" onClick={clickCancel}>
Cancel
</Button>
</div>
Expand Down
Loading

0 comments on commit 1d5faa2

Please sign in to comment.