Commit be71832f authored by Sondre Westby Liestøl's avatar Sondre Westby Liestøl
Browse files

Added comment and refactored some parts of the code in comments, users and...

Added comment and refactored some parts of the code in comments, users and workout. Needs to run tests
parent 45da54de
Pipeline #170111 passed with stage
in 1 minute and 11 seconds
......@@ -10,7 +10,9 @@ from rest_framework.filters import OrderingFilter
# Create your views here.
class CommentList(
mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView
mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView
):
# queryset = Comment.objects.all()
serializer_class = CommentSerializer
......
......@@ -6,11 +6,11 @@ from .forms import CustomUserChangeForm, CustomUserCreationForm
# Register your models here.
#Is "model" used?
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = get_user_model()
# model = get_user_model() (THIS IS COMMENTED OUT!)
# list_display = UserAdmin.list_display + ('coach',)
fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("coach",)}),)
add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("coach",)}),)
......
......@@ -39,6 +39,7 @@ class UserList(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericA
def get_queryset(self):
qs = get_user_model().objects.all()
# second if statement off?
if self.request.user:
# Return the currently logged in user
status = self.request.query_params.get("user", None)
......@@ -59,6 +60,7 @@ class UserDetail(
queryset = get_user_model().objects.all()
permission_classes = [permissions.IsAuthenticated & (IsCurrentUser | IsReadOnly)]
# Is this unecessarily complecated? (lookup_field_options is only a list of two, could make a switch)
def get_object(self):
for field in self.lookup_field_options:
if field in self.kwargs:
......@@ -97,6 +99,7 @@ class OfferList(
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
#Change this to switch/case, removed the last else-statement!!!
def get_queryset(self):
qs = Offer.objects.none()
result = Offer.objects.none()
......@@ -110,6 +113,7 @@ class OfferList(
# filtering by status (if provided)
s = qp.get("status", None)
if s is not None and self.request is not None:
qs = qs.filter(status=s)
if qp.get("status", None) is None:
......@@ -123,8 +127,7 @@ class OfferList(
elif c == "received":
qs = qs.filter(recipient=u)
return qs
else:
return result
return result
class OfferDetail(
......
......@@ -105,7 +105,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for exercise_instance_data in exercise_instances_data:
exercise = ExerciseInstance.objects.create(workout=workout, **exercise_instance_data)
difficulty += exercise.exercise.difficulty;
difficulty += exercise.exercise.difficulty
for file_data in files_data:
......@@ -140,16 +140,21 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.visibility = validated_data.get("visibility", instance.visibility)
instance.date = validated_data.get("date", instance.date)
difficulty = 0
self.handle_exercise_instance(instance, exercise_instances_data, exercise_instances)
self.handle_workout_files(validated_data, instance)
return instance
# Handle ExerciseInstances
# This updates existing exercise instances without adding or deleting object.
# zip() will yield n 2-tuples, where n is
# min(len(exercise_instance), len(exercise_instance_data))
def handle_exercise_instance(self, instance, exercise_instances_data, exercise_instances):
"""New function to split the long method, no need for self inside."""
difficulty = 0
for exercise_instance, exercise_instance_data in zip(
exercise_instances.all(), exercise_instances_data
):
......@@ -162,7 +167,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
exercise_instance.sets = exercise_instance_data.get(
"sets", exercise_instance.sets
)
difficulty += exercise_instance.exercise.difficulty;
difficulty += exercise_instance.exercise.difficulty
exercise_instance.save()
# If new exercise instances have been added to the workout, then create them
......@@ -172,7 +177,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
exercise = ExerciseInstance.objects.create(
workout=instance, **exercise_instance_data
)
difficulty += exercise.exercise.difficulty;
difficulty += exercise.exercise.difficulty
# Else if exercise instances have been removed from the workout, then delete them
elif len(exercise_instances_data) < len(exercise_instances.all()):
for i in range(len(exercise_instances_data), len(exercise_instances.all())):
......@@ -183,10 +188,11 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.difficulty = difficulty / len(exercise_instances_data)
instance.save()
# Handle WorkoutFiles
# Handle WorkoutFiles
def handle_workout_files(self, validated_data, instance):
"""Split long method into smaller function, could be a function instead of method"""
if "files" in validated_data:
files_data = validated_data.pop("files")
files = instance.files
......@@ -207,8 +213,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for i in range(len(files_data), len(files.all())):
files.all()[i].delete()
return instance
def get_owner_username(self, obj):
"""Returns the owning user's username
......
......@@ -64,11 +64,11 @@ class RememberMe(
serializer_class = RememberMeSerializer
# Code smell - unnecessary else statement
def get(self, request):
if request.user.is_authenticated == False:
if not request.user.is_authenticated:
raise PermissionDenied
else:
return Response({"remember_me": self.rememberme()})
return Response({"remember_me": self.rememberme()})
def post(self, request):
cookieObject = namedtuple("Cookies", request.COOKIES.keys())(
......@@ -83,6 +83,7 @@ class RememberMe(
}
)
# Needs change of return statement
def get_user(self, cookieObject):
decode = base64.b64decode(cookieObject.remember_me)
user, sign = pickle.loads(decode)
......@@ -95,10 +96,12 @@ class RememberMe(
creds = [self.request.user, self.sign_user(str(self.request.user))]
return base64.b64encode(pickle.dumps(creds))
def sign_user(self, username):
signer = Signer()
signed_user = signer.sign(username)
return signed_user
#Could be a function
def sign_user(username):
"Makes a signer"
signer = Signer()
signed_user = signer.sign(username)
return signed_user
class WorkoutList(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment