Skip to content

Commit

Permalink
Merge pull request #683 from sgfost/user-models-list
Browse files Browse the repository at this point in the history
fixes for profile page models tab and curator_statistics
  • Loading branch information
alee authored Nov 16, 2023
2 parents 592588f + 6f4e6dd commit 020534e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
6 changes: 2 additions & 4 deletions django/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,13 @@ def get_queryset(self):
def get_retrieve_context(self, instance):
context = super().get_retrieve_context(instance)
accessing_user = self.request.user
# FIXME: ideally this functionality should be in the library app, though its not clear
# whether this can be done without a much more complicated workaround or a major
# re-organization of apps
logger.debug("Finding models for user %s", instance.user)
context["codebases"] = (
Codebase.objects.accessible(accessing_user)
.filter_by_contributor(instance.user)
.filter_by_contributor_or_submitter(instance.user)
.with_tags()
.with_featured_images()
.order_by("-last_modified")
)
add_change_delete_perms(instance, context, accessing_user)
return context
Expand Down
4 changes: 2 additions & 2 deletions django/library/jinja2/library/codebases/macros.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</a>
{% endif %}
{% else %}
{# FIXME: this is a degenerate case, there should always be at least 1 contributor (the submitter) #}
{# FIXME: degenerate data guard, there should always be at least 1 contributor #}
no contributors listed
{% endfor %}
|
Expand All @@ -91,7 +91,7 @@
| Last modified {{ format_date(codebase.last_published_on) }}
{% endif %}
{% else %}
Unpublished
Unpublished | Last modified {{ format_date(codebase.last_modified) }}
{% endif %}
</small>
</div>
Expand Down
66 changes: 30 additions & 36 deletions django/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,25 @@ def has_name(self):
return any([self.given_name, self.family_name]) or self.user

def get_full_name(self, family_name_first=False):
full_name = ""
# Bah. Horrid name logic
if self.type == "person":
if self.has_name:
if family_name_first:
full_name = (
f"{self.family_name}, {self.given_name} {self.middle_name}"
)
elif self.middle_name:
full_name = (
f"{self.given_name} {self.middle_name} {self.family_name}"
)
else:
full_name = f"{self.given_name} {self.family_name}"
elif self.user:
full_name = self.user.member_profile.name
else:
logger.exception("No usable name found for contributor %s", self.pk)
return self._get_person_full_name(family_name_first)
else:
# organizations only use given_name
return self.given_name

def _get_person_full_name(self, family_name_first=False):
if not self.has_name:
logger.warning("No usable name found for contributor %s", self.pk)
return ""
if self.user and not any([self.given_name, self.family_name]):
return self.user.member_profile.name
if family_name_first:
return f"{self.family_name}, {self.given_name} {self.middle_name}".strip()
else:
# organizations only have given_name
full_name = self.given_name
return " ".join(full_name.split())
return (
f"{self.given_name} {self.middle_name}".strip()
+ f" {self.family_name}".rstrip()
)

@property
def formatted_affiliations(self):
Expand Down Expand Up @@ -353,21 +350,16 @@ def accessible(self, user):
user=user, queryset=self.with_viewable_releases(user=user)
)

def get_contributor_q(self, user):
if Contributor.objects.filter(user=user).count() > 1:
logger.warning("User %s has multiple contributors", user)
return Q(releases__contributors__user=user)

def filter_by_contributor(self, user):
# FIXME: query could likely be more efficient
# find all codebase releases with this user marked as a ReleaseContributor
contributors = Contributor.objects.filter(user=user)
if contributors.exists():
if contributors.count() > 1:
logger.warning("User %s has multiple contributors", user)
releases = CodebaseRelease.objects.filter(
pk__in=ReleaseContributor.objects.filter(
contributor__in=contributors
).values_list("release", flat=True)
)
return self.filter(releases__in=releases).distinct()
else:
return self.filter(submitter=user)
return self.filter(self.get_contributor_q(user)).distinct()

def filter_by_contributor_or_submitter(self, user):
return self.filter(Q(submitter=user) | self.get_contributor_q(user)).distinct()

def with_contributors(self, release_contributor_qs=None, user=None, **kwargs):
if user is not None:
Expand Down Expand Up @@ -465,7 +457,9 @@ def updated_after(self, start_date, end_date=None, **kwargs):
updated_codebases = updated_codebases.difference(new_codebases)
releases = CodebaseRelease.objects.filter(
id__in=(
new_codebases.values("releases") | updated_codebases.values("releases")
new_codebases.values_list("releases", flat=True).union(
updated_codebases.values_list("releases", flat=True)
)
)
)
return new_codebases, updated_codebases, releases
Expand Down Expand Up @@ -2044,7 +2038,7 @@ def pending(self, **kwargs):
def candidate_reviewers(self, **kwargs):
# FIXME: fairly horribly inefficient
return MemberProfile.objects.filter(
pk__in=self.values_list("candidate_reviewer", flat=True)
id__in=self.values_list("candidate_reviewer", flat=True)
)

def with_reviewer_statistics(self):
Expand Down

0 comments on commit 020534e

Please sign in to comment.