Skip to content

Commit

Permalink
Chore: Make code Flake8 complaint
Browse files Browse the repository at this point in the history
  • Loading branch information
johndiginee committed Sep 16, 2023
1 parent 573064d commit 0ab4865
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 61 deletions.
10 changes: 10 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@ ignore =
F403,
B007,
B950,
F401,
E123,
E131,
F405,
F841,
E111





max-line-length = 200
4 changes: 2 additions & 2 deletions account/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.contrib import admin
# from django.contrib import admin

# Register your models here.
# # Register your models here.
11 changes: 5 additions & 6 deletions account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ class Meta:
fields = ('first_name', 'last_name', "email", "password")

extra_kwargs = {
'first_name': { 'required': True, "allow_blank": False },
'last_name': { 'required': True, "allow_blank": False },
'email': { 'required': True, "allow_blank": False },
'password': { 'required': True, "allow_blank": False, 'min_length': 6 },
'first_name': {'required': True, "allow_blank": False},
'last_name': {'required': True, "allow_blank": False},
'email': {'required': True, "allow_blank": False},
'password': {'required': True, "allow_blank": False, 'min_length': 6},
}


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('first_name', 'last_name', "email", "username")
fields = ('first_name', 'last_name', 'email', 'username')
2 changes: 1 addition & 1 deletion account/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.test import TestCase
# from django.test import TestCase

# Create your tests here.
14 changes: 7 additions & 7 deletions account/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from django.shortcuts import get_object_or_404, render
from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from django.contrib.auth.models import User
Expand Down Expand Up @@ -32,11 +32,11 @@ def register(request):
password = make_password(data['password']),
)

return Response({ 'details': 'User Registered' }, status=status.HTTP_201_CREATED)
return Response({'details': 'User Registered'}, status=status.HTTP_201_CREATED)


else:
return Response({ 'error': 'User already exists' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'User already exists'}, status=status.HTTP_400_BAD_REQUEST)

else:
return Response(user.errors)
Expand Down Expand Up @@ -102,7 +102,7 @@ def forgot_password(request):
[data['email']]
)

return Response({ 'details': 'Password rest email sent to: {email}'.format(email=data['email']) })
return Response({'details': 'Password rest email sent to: {email}'.format(email=data['email'])})


@api_view(['POST'])
Expand All @@ -113,11 +113,11 @@ def reset_password(request, token):
user = get_object_or_404(User, profile__reset_password_token=token)

if user.profile.reset_password_expire.replace(tzinfo=None) < datetime.now():
return Response({ 'error': 'Token in expired' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'Token in expired'}, status=status.HTTP_400_BAD_REQUEST)


if data['password'] != data['confirmPassword']:
return Response({ 'error': 'Passwords are not same' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'Passwords are not same'}, status=status.HTTP_400_BAD_REQUEST)

user.password = make_password(data['password'])
user.profile.reset_password_token = ""
Expand All @@ -126,4 +126,4 @@ def reset_password(request, token):
user.profile.save()
user.save()

return Response({ 'details': 'Password reset successfully'})
return Response({'details': 'Password reset successfully'})
3 changes: 2 additions & 1 deletion core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = 'Africa/Lagos'

USE_I18N = True

Expand Down Expand Up @@ -185,3 +185,4 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

ADMIN_URL = "supersecret/"
2 changes: 0 additions & 2 deletions order/filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from dataclasses import field
from pyexpat import model
from django_filters import rest_framework as filters
from .models import Order

Expand Down
1 change: 0 additions & 1 deletion order/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from dataclasses import field
from rest_framework import serializers
from .models import Order, OrderItem

Expand Down
2 changes: 1 addition & 1 deletion order/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.test import TestCase
# from django.test import TestCase

# Create your tests here.
15 changes: 7 additions & 8 deletions order/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
Expand Down Expand Up @@ -39,7 +38,7 @@ def get_orders(request):
"count": count,
"resPerPage": resPerPage,
'orders': serializer.data
})
})


@api_view(['GET'])
Expand All @@ -63,7 +62,7 @@ def new_order(request):
order_items = data['orderItems']

if order_items and len(order_items) == 0:
return Response({ 'error': 'No Order Items. Please add atleast one product' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'No Order Items. Please add atleast one product'}, status=status.HTTP_400_BAD_REQUEST)

else:

Expand Down Expand Up @@ -163,7 +162,7 @@ def create_checkout_session(request):
'product_data' : {
'name': item['name'],
"images": [item['image']],
"metadata": { "product_id": item['product'] }
"metadata": {"product_id": item['product']}
},
'unit_amount': item['price'] * 100
},
Expand All @@ -180,7 +179,7 @@ def create_checkout_session(request):
cancel_url=YOUR_DOMAIN
)

return Response({ 'session': session })
return Response({'session': session})


@api_view(['POST'])
Expand All @@ -197,9 +196,9 @@ def stripe_webhook(request):
)

except ValueError as e:
return Response({ 'error': 'Invalid Payload' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'Invalid Payload'}, status=status.HTTP_400_BAD_REQUEST)
except stripe.error.SignatureVerificationError as e:
return Response({ 'error': 'Invalid signature' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'Invalid signature'}, status=status.HTTP_400_BAD_REQUEST)


if event['type'] == 'checkout.session.completed':
Expand Down Expand Up @@ -242,4 +241,4 @@ def stripe_webhook(request):
product.save()


return Response({ 'details': 'Your payment was successful.' })
return Response({'details': 'Your payment was successful.'})
1 change: 0 additions & 1 deletion product/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from ast import keyword
from django_filters import rest_framework as filters
from .models import Product

Expand Down
10 changes: 4 additions & 6 deletions product/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


from rest_framework import serializers
from .models import *

Expand Down Expand Up @@ -27,10 +25,10 @@ class Meta:
fields = ('id', 'name', 'description', 'price', 'brand', 'ratings', 'category', 'stock', 'user', 'reviews', 'images')

extra_kwargs = {
"name": { "required": True, 'allow_blank':False },
"description": { "required": True, 'allow_blank':False },
"brand": { "required": True, 'allow_blank':False },
"category": { "required": True, 'allow_blank':False },
"name": {"required": True, 'allow_blank':False},
"description": {"required": True, 'allow_blank':False},
"brand": {"required": True, 'allow_blank':False},
"category": {"required": True, 'allow_blank':False},
}

def get_reviews(self, obj):
Expand Down
2 changes: 1 addition & 1 deletion product/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.test import TestCase
# from django.test import TestCase

# Create your tests here.
27 changes: 13 additions & 14 deletions product/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from itertools import product
from django.shortcuts import get_object_or_404
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
Expand Down Expand Up @@ -46,7 +45,7 @@ def get_product(request, pk):

serializer = ProductSerializer(product, many=False)

return Response({ "product": serializer.data })
return Response({"product": serializer.data})


@api_view(['POST'])
Expand All @@ -63,7 +62,7 @@ def new_product(request):

res = ProductSerializer(product, many=False)

return Response({ "product": res.data })
return Response({"product": res.data})

else:
return Response(serializer.errors)
Expand Down Expand Up @@ -95,7 +94,7 @@ def update_product(request, pk):
product = get_object_or_404(Product, id=pk)

if product.user != request.user:
return Response({ 'error': 'You cannot update this product' }, status=status.HTTP_403_FORBIDDEN)
return Response({'error': 'You cannot update this product'}, status=status.HTTP_403_FORBIDDEN)

product.name = request.data['name']
product.description = request.data['description']
Expand All @@ -109,7 +108,7 @@ def update_product(request, pk):

serializer = ProductSerializer(product, many=False)

return Response({ "product": serializer.data })
return Response({"product": serializer.data})


@api_view(['DELETE'])
Expand All @@ -119,16 +118,16 @@ def delete_product(request, pk):
product = get_object_or_404(Product, id=pk)

if product.user != request.user:
return Response({ 'error': 'You cannot delete this product' }, status=status.HTTP_403_FORBIDDEN)
return Response({'error': 'You cannot delete this product'}, status=status.HTTP_403_FORBIDDEN)

args = { "product": pk }
args = {"product": pk}
images = ProductImages.objects.filter(**args)
for i in images:
i.delete()

product.delete()

return Response({ 'details': 'Product deleted successfully.' }, status=status.HTTP_200_OK)
return Response({'details': 'Product deleted successfully.'}, status=status.HTTP_200_OK)


@api_view(['POST'])
Expand All @@ -143,19 +142,19 @@ def create_review(request, pk):


if data['rating'] <= 0 or data['rating'] > 5:
return Response({ 'error': 'Please select rating between 1-5' }, status=status.HTTP_400_BAD_REQUEST)
return Response({'error': 'Please select rating between 1-5'}, status=status.HTTP_400_BAD_REQUEST)

elif review.exists():

new_review = { 'rating': data['rating'], 'comment': data['comment'] }
new_review = {'rating': data['rating'], 'comment': data['comment']}
review.update(**new_review)

rating = product.reviews.aggregate(avg_ratings=Avg('rating'))

product.ratings = rating['avg_ratings']
product.save()

return Response({ 'detail': 'Your review updated successfully' })
return Response({'detail': 'Your review updated successfully'})

else:
Review.objects.create(
Expand All @@ -170,7 +169,7 @@ def create_review(request, pk):
product.ratings = rating['avg_ratings']
product.save()

return Response({ 'detail': 'Your review is live.' })
return Response({'detail': 'Your review is live.'})



Expand All @@ -196,7 +195,7 @@ def delete_review(request, pk):
product.ratings = rating['avg_ratings']
product.save()

return Response({ 'detail': 'Your review has been deleted' })
return Response({'detail': 'Your review has been deleted'})

else:
return Response({ 'error': 'Review not found' }, status=status.HTTP_404_NOT_FOUND)
return Response({'error': 'Review not found'}, status=status.HTTP_404_NOT_FOUND)
11 changes: 1 addition & 10 deletions utils/custom_exception_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import exception_handler
from http import HTTPStatus

Expand Down Expand Up @@ -28,11 +26,4 @@ def custom_exception_handler(exc, context):

response.data = error_payload

return response

# else:
# error = {
# "error": "Something went wrong."
# }

# return Response(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return response

0 comments on commit 0ab4865

Please sign in to comment.