Skip to content

Feature project list view#85

Merged
kbeker merged 10 commits intomasterfrom
feature-project-list-view
Mar 28, 2019
Merged

Feature project list view#85
kbeker merged 10 commits intomasterfrom
feature-project-list-view

Conversation

@MartynaAnnaGottschling
Copy link
Contributor

@MartynaAnnaGottschling MartynaAnnaGottschling commented Mar 8, 2019

Resolves #50
Resolves #51

@@ -0,0 +1,38 @@
from django import template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template tags with database queries should only be used when necessary. At best - never :) All data required by the template frontend should be prepared in backend, to keep template frontend logic at minimum. The same effect can be achieved by using Django custom model manager, but it has advantage that it can be used in both template and Python code, so there is no need to duplicate this functionality.

Definitely filter_terminated, filter_active and filter_completed can be moved as custom manager methods because they don't accept any extra parameters. About the rest I can't tell because they are not used in this pull request, but I think it is doable to, so please do them all as custom manager methods.

Please check https://simpleisbetterthancomplex.com/tips/2016/08/16/django-tip-11-custom-manager-with-chainable-querysets.html for an example how to do this. If you need help please contact me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return Project.objects.all().order_by('id')

def get(self, request):
projects_queryset = self.get_queryset()
Copy link
Contributor

@rwrzesien rwrzesien Mar 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole logic of this get method is actually selecting queryset, so it should be moved to get_queryset method. This way you can remove get method from here and lean on its default implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<td class="padding"><a href="{% url 'custom-users-list' %}">{% trans 'Employees' %}</a></td>
<td class="padding"><a href="{% url 'custom-projects-list' %}">{% trans 'Projects list' %}</a></td>
{% endif %}
{% if user.user_type == "MANAGER" %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good candidate for custom template tag. This way we could use "MANAGER" value directly from Python enum, in case if its value would change in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on it.

Copy link
Collaborator

@maciejSamerdak maciejSamerdak Mar 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serializer_class = ProjectSerializer


class ProjectsList(APIView):
Copy link
Contributor

@rwrzesien rwrzesien Mar 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are implement only GET method to retrieve a list, inherit from rest_framework.generics.ListAPIView (if there will be any problems after switching to this, please drop me a comment).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any more sophisticated API view than the basic one requires extra attributes and/or greatly limits view's flexibility.
Since we will be dropping REST anyway, I'll be switching it to Django's generic ListView. It causes far less problems anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

projects_queryset = self.get_queryset()
if request.user.user_type == CustomUser.UserType.MANAGER.name:
projects_queryset = Project.objects.filter(managers__id=request.user.pk)
if request.GET.get('sort'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can define sorting using pure rest framework functionality https://www.django-rest-framework.org/api-guide/filtering/#orderingfilter .

As for members and members_count it will be tricky, because defined items in ordering_fields must exists on queryset results. But on the other hand, we don't want to run annotate(members_count=Count('members') if it is not needed, as it results in decent extra SQL work. So you need to check if 'members' in request.GET.get('sort') in get_queryset method, and then add counting only then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

url(r'^admin/', admin.site.urls),
url(r'^', include('users.urls')),
url(r'^managers/', include('managers.urls')),
url(r'^', include('managers.urls')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should stay, as we discussed on project code review, we should keep endpoints separated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,104 @@
import datetime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After moving template tags as custom manager methods please remove those tests too. Also I don't think there should be extra tests for custom manager methods, as those will be just some simple querysets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@rwrzesien rwrzesien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good generally, there are some minor issues but also two bigger improvements that should be applied:

  • using custom model manager instead of template tags
  • use built in sorting in rest framework view.

I think it would be good to focus on those two first.


class ProjectsList(APIView):
renderer_classes = [renderers.TemplateHTMLRenderer]
template_name = 'managers/projects_list.html'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't saw it at first, so I have dropped comment in the following pull request, so I will link it here #86 (comment) .

Copy link
Contributor

@kbeker kbeker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small changes to do:)

self.assertEqual(response.status_code, 200)
self.assertContains(response, self.project.name)
projects_list = response.data['projects_queryset']
self.assertEqual(list(manager_project_list), list(projects_list))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have to be converted to a list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kbeker Yes, because without that the test returns
AssertionError: <QuerySet [<Project: Example Project>]> != <QuerySet [<Project: Example Project>]>
I don't know why exactly.

This was referenced Mar 11, 2019
@MartynaAnnaGottschling MartynaAnnaGottschling force-pushed the feature-project-list-view branch 2 times, most recently from 9330a49 to 2bf99e0 Compare March 28, 2019 15:37
@kbeker kbeker force-pushed the feature-project-list-view branch from 2bf99e0 to 9ff105f Compare March 28, 2019 15:52
@kbeker kbeker requested a review from rwrzesien March 28, 2019 15:52
Copy link
Contributor

@kbeker kbeker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready to merge

@kbeker kbeker dismissed rwrzesien’s stale review March 28, 2019 15:54

Because we need to merge it :)

@kbeker kbeker merged commit 9ff105f into master Mar 28, 2019
@kbeker kbeker deleted the feature-project-list-view branch March 28, 2019 15:56
@kbeker kbeker modified the milestones: v0.4.0, v0.3.0, v0.1.0 May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create tests for projects views Manager Base Interface

4 participants