Skip to content

Commit

Permalink
refactor Project.get_children() (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Mar 21, 2023
1 parent 83ad0e4 commit b2170f1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Changed
- Implement advanced search with POST (#712)
- Remove category project list scrolling (#1141)
- Move sidebar template tags to context processor (#969)
- Update ``Project`` model API methods (#638, #710, #1045)
- Update ``Project`` model API methods (#638, #710, #1045, #1178)
- Update permission and UI test setup (#638)
- Display roles consistently in member/owner update UI (#1027)
- Reduce site app view top margin (#866)
Expand Down
2 changes: 2 additions & 0 deletions docs/source/major_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ effected code accordingly.
method signature has also been changed.
- For ``RoleAssignment.project``, the ``related_name`` field has been renamed
from ``roles`` into ``local_roles``.
- ``Project.get_children()`` returns projects sorted by ``full_title`` with the
argument ``flat=True``.
Base Classes for Tests Updated
------------------------------
Expand Down
19 changes: 7 additions & 12 deletions projectroles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

# SODAR constants
SODAR_CONSTANTS = get_sodar_constants()
PROJECT_TYPE_PROJECT = SODAR_CONSTANTS['PROJECT_TYPE_PROJECT']
PROJECT_TYPE_CATEGORY = SODAR_CONSTANTS['PROJECT_TYPE_CATEGORY']
PROJECT_ROLE_OWNER = SODAR_CONSTANTS['PROJECT_ROLE_OWNER']
PROJECT_ROLE_DELEGATE = SODAR_CONSTANTS['PROJECT_ROLE_DELEGATE']
PROJECT_ROLE_CONTRIBUTOR = SODAR_CONSTANTS['PROJECT_ROLE_CONTRIBUTOR']
Expand Down Expand Up @@ -189,7 +191,7 @@ def save(self, *args, **kwargs):
self._validate_archive()
# Update full title of self and children
self.full_title = self._get_full_title()
for child in self.get_children():
for child in self.children.all():
child.save()
# Update public children
# NOTE: Parents will be updated in ProjectModifyMixin.modify_project()
Expand Down Expand Up @@ -302,22 +304,15 @@ def get_parents(self):

def get_children(self, flat=False):
"""
Return child objects for the Project sorted by title.
Return child objects for a Category, sorted by full title.
:param flat: Return all children recursively as a flat list (bool)
:return: QuerySet
"""

def _get(obj, ret=None):
if ret is None:
ret = []
ret += list(obj.get_children())
for child in obj.get_children():
ret = _get(child, ret)
return ret

if flat:
return _get(self)
return Project.objects.filter(
full_title__startswith=self.full_title + CAT_DELIMITER
).order_by('full_title')
return self.children.all().order_by('title')

def get_depth(self):
Expand Down
6 changes: 1 addition & 5 deletions projectroles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
RemoteProject,
SODAR_CONSTANTS,
ROLE_RANKING,
CAT_DELIMITER,
)
from projectroles.plugins import (
get_active_plugins,
Expand Down Expand Up @@ -1710,10 +1709,7 @@ def _update_app_alerts(self, app_alerts, project, user, inh_as):
AppAlert = app_alerts.get_model()
dis_projects = [project]
if project.type == PROJECT_TYPE_CATEGORY:
# TODO: Use get_children() once refactored
for c in Project.objects.filter(
full_title__startswith=project.full_title + CAT_DELIMITER
):
for c in project.get_children(flat=True):
c_role_as = RoleAssignment.objects.filter(
project=c, user=user
).first()
Expand Down

0 comments on commit b2170f1

Please sign in to comment.