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

Python refactoring: need tests

parent c5c9bfaa
Pipeline #170346 passed with stage
in 1 minute and 9 seconds
......@@ -32,11 +32,10 @@ class CommentList(
def get_queryset(self):
workout_pk = self.kwargs.get("pk")
qs = Comment.objects.none()
user = self.request.user
if workout_pk:
qs = Comment.objects.filter(workout=workout_pk)
elif user:
elif self.request.user:
"""A comment should be visible to the requesting user if any of the following hold:
- The comment is on a public visibility workout
- The comment was written by the user
......@@ -46,9 +45,7 @@ class CommentList(
# The code below is kind of duplicate of the one in ./permissions.py
# We should replace it with a better solution.
# Or maybe not.
# qs = Comment.objects.filter(IsCommentVisibleToUser.has_object_permission(self, user, None, object)) check?
"""
qs = Comment.objects.filter(
Q(workout__visibility="PU")
| Q(owner=self.request.user)
......@@ -58,8 +55,14 @@ class CommentList(
)
| Q(workout__owner=self.request.user)
).distinct()
"""
for obj in Comment.objects:
if IsCommentVisibleToUser.has_object_permission(self, self.request, None, obj):
qs.append(obj)
return qs
return qs.distinct()
# Details of comment
class CommentDetail(
......
......@@ -10,8 +10,7 @@ from .forms import CustomUserChangeForm, CustomUserCreationForm
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
# model = get_user_model() (THIS IS COMMENTED OUT!)
# list_display = UserAdmin.list_display + ('coach',)
model = get_user_model()
fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("coach",)}),)
add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("coach",)}),)
......
......@@ -39,7 +39,6 @@ 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)
......@@ -99,9 +98,8 @@ class OfferList(
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
#Change this to switch/case, removed the last else-statement!!!
# Nested if-statement and no use of the first queryset varible
def get_queryset(self):
qs = Offer.objects.none()
result = Offer.objects.none()
if self.request.user:
......@@ -109,23 +107,22 @@ class OfferList(
Q(owner=self.request.user) | Q(recipient=self.request.user)
).distinct()
qp = self.request.query_params
u = self.request.user
user = self.request.user
# 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)
status = qp.get("status", None)
category = qp.get("category", None)
if status is not None and self.request is not None:
qs = qs.filter(status=status)
if qp.get("status", None) is None:
qs = Offer.objects.filter(Q(owner=u)).distinct()
qs = Offer.objects.filter(Q(owner=user)).distinct()
# filtering by category (sent or received)
c = qp.get("category", None)
if c is not None and qp is not None:
if c == "sent":
qs = qs.filter(owner=u)
elif c == "received":
qs = qs.filter(recipient=u)
if category is not None and qp is not None:
if category == "sent":
qs = qs.filter(owner=user)
elif category == "received":
qs = qs.filter(recipient=user)
return qs
return result
......
......@@ -152,7 +152,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
# 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."""
"""New function to split the long method, manually."""
difficulty = 0
for exercise_instance, exercise_instance_data in zip(
......@@ -192,7 +192,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
# Handle WorkoutFiles
def handle_workout_files(self, validated_data, instance):
"""Split long method into smaller function, could be a function instead of method"""
"""Split long method into smaller method, manually """
if "files" in validated_data:
files_data = validated_data.pop("files")
files = instance.files
......
......@@ -70,11 +70,12 @@ class RememberMe(
raise PermissionDenied
return Response({"remember_me": self.rememberme()})
# Change in name "cookieObjects"
def post(self, request):
cookieObject = namedtuple("Cookies", request.COOKIES.keys())(
cookie_object = namedtuple("Cookies", request.COOKIES.keys())(
*request.COOKIES.values()
)
user = self.get_user(cookieObject)
user = self.get_user(cookie_object)
refresh = RefreshToken.for_user(user)
return Response(
{
......@@ -84,25 +85,25 @@ class RememberMe(
)
# Needs change of return statement
def get_user(self, cookieObject):
decode = base64.b64decode(cookieObject.remember_me)
def get_user(self, cookie_object):
decode = base64.b64decode(cookie_object.remember_me)
user, sign = pickle.loads(decode)
# Validate signature
if sign == sign_user(user):
if sign == self.sign_user(user):
return user
# return None
def rememberme(self):
"""Remember the user"""
creds = [self.request.user, sign_user(str(self.request.user))]
creds = [self.request.user, self.sign_user(str(self.request.user))]
return base64.b64encode(pickle.dumps(creds))
#Could be a function
def sign_user(username):
"Makes a signer"
signer = Signer()
signed_user = signer.sign(username)
return signed_user
#Changed this!
def sign_user(self, username):
"Makes a signer"
signer = Signer()
return signer.sign(username)
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