Skip to content

Commit

Permalink
ADD: exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Samdaso-o committed Sep 28, 2021
1 parent 48378fb commit 672dd28
Showing 1 changed file with 66 additions and 59 deletions.
125 changes: 66 additions & 59 deletions api/exporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,82 +54,89 @@ def get(self, request):

@admin_decorator
def post(self, request):
data = json.loads(request.body)
date = data['date'].split('.')
create_at = date[0]+'-'+date[1]+'-'+date[2]

category, is_create = Category.objects.get_or_create(
name = data['category'],
create_at = create_at.replace(' ','')
)
try:
data = json.loads(request.body)
date = data['date'].split('.')
create_at = date[0]+'-'+date[1]+'-'+date[2]

category, is_create = Category.objects.get_or_create(
name = data['category'],
create_at = create_at.replace(' ','')
)

if not is_create:
return JsonResponse({'message':'EXISTING_CATEGORY'}, status=400)
if not is_create:
return JsonResponse({'message':'EXISTING_CATEGORY'}, status=400)

return JsonResponse({'message':'SUCCESS'}, status=201)
return JsonResponse({'message':'SUCCESS'}, status=201)

except KeyError:
return JsonResponse({'message': 'KEY_ERROR'}, status=400)

@admin_decorator
@transaction.atomic
def patch(self, request):
data = json.loads(request.body)
category_id = data['category_id']
feature_category_id = data['feature_category_id']
user = request.user
token = user.github_token
repo = f"{settings.ORGANIZATION}/exporterhub.io"
url = f"https://api.github.com/repos/{repo}/contents/api/exporter_list.csv"
responses = []
response = ''
try:
data = json.loads(request.body)
category_id = data['category_id']
feature_category_id = data['feature_category_id']
user = request.user
token = user.github_token
repo = f"{settings.ORGANIZATION}/exporterhub.io"
url = f"https://api.github.com/repos/{repo}/contents/api/exporter_list.csv"
responses = []
response = ''

if not Category.objects.filter(id=category_id).exists:
return JsonResponse({'message':'EXISTING_CATEGORY'}, status=400)
if not Category.objects.filter(id=feature_category_id).exists:
return JsonResponse({'message':'EXISTING_CATEGORY'}, status=400)

category = Category.objects.get(id=category_id)
feature_category = Category.objects.get(id=feature_category_id)

if not Category.objects.filter(id=category_id).exists:
return JsonResponse({'message':'EXISTING_CATEGORY'}, status=400)
if not Category.objects.filter(id=feature_category_id).exists:
return JsonResponse({'message':'EXISTING_CATEGORY'}, status=400)
file = open('exporter_list.csv', 'r')
reader = [row for row in csv.reader(file)]
file.close()

category = Category.objects.get(id=category_id)
feature_category = Category.objects.get(id=feature_category_id)
file = open('exporter_list.csv', 'w', newline='')
writer = csv.writer(file)
for i, row in enumerate(reader):
if reader[i][4] == category.name:
reader[i][4] = feature_category.name
responses.append([reader[i][0], reader[i][1], reader[i][2], reader[i][3], reader[i][4],'\n'])
writer.writerow(row)
else:
writer.writerow(row)
responses.append([reader[i][0], reader[i][1], reader[i][2], reader[i][3], reader[i][4],'\n'])
file.close()

file = open('exporter_list.csv', 'r')
reader = [row for row in csv.reader(file)]
file.close()
csv_info = self.get_contents(headers={'Authorization' : 'token ' + token})

file = open('exporter_list.csv', 'w', newline='')
writer = csv.writer(file)
for i, row in enumerate(reader):
if reader[i][4] == category.name:
reader[i][4] = feature_category.name
responses.append([reader[i][0], reader[i][1], reader[i][2], reader[i][3], reader[i][4],'\n'])
writer.writerow(row)
else:
writer.writerow(row)
responses.append([reader[i][0], reader[i][1], reader[i][2], reader[i][3], reader[i][4],'\n'])
file.close()

csv_info = self.get_contents(headers={'Authorization' : 'token ' + token})
if csv_info == 'GITHUB_GET_REPO_ERROR':
return JsonResponse({'message': 'GITHUB_API_FAIL'}, status=400)

for detail in responses:
response += ','.join(detail)

if csv_info == 'GITHUB_GET_REPO_ERROR':
return JsonResponse({'message': 'GITHUB_API_FAIL'}, status=400)

for detail in responses:
response += ','.join(detail)
contents = json.dumps({
'sha' : csv_info['sha'],
'message' : 'wip',
'content' : base64.b64encode(response.encode('utf-8')).decode('utf-8')
})

contents = json.dumps({
'sha' : csv_info['sha'],
'message' : 'wip',
'content' : base64.b64encode(response.encode('utf-8')).decode('utf-8')
})
result = requests.put(url, data=contents, headers={'Authorization': 'token ' + token, 'Content-Type':'application/vnd.github.v3+json'})

result = requests.put(url, data=contents, headers={'Authorization': 'token ' + token, 'Content-Type':'application/vnd.github.v3+json'})
if result.status_code == 200:
Exporter.objects.filter(category_id = category_id).update(category_id=feature_category_id)
Category.objects.filter(id=category_id).delete()

if result.status_code == 200:
Exporter.objects.filter(category_id = category_id).update(category_id=feature_category_id)
Category.objects.filter(id=category_id).delete()
return JsonResponse({'message':'SUCCESS'}, status=200)

return JsonResponse({'message':'SUCCESS'}, status=200)
else:
return JsonResponse({'message': 'GITHUB_REPO_API_ERROR'}, status=404)

else:
return JsonResponse({'message': 'GITHUB_REPO_API_ERROR'}, status=404)
except KeyError:
return JsonResponse({'message': 'KEY_ERROR'}, status=400)

@admin_decorator
@transaction.atomic
Expand Down

0 comments on commit 672dd28

Please sign in to comment.