Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
jkueloc authored Aug 9, 2024
2 parents 2ad61b5 + c93ec4c commit 0d1b6f0
Show file tree
Hide file tree
Showing 40 changed files with 1,671 additions and 1,377 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rules:
'unicorn/prefer-query-selector': off # See https://github.com/sindresorhus/eslint-plugin-unicorn/issues/276
'unicorn/prefer-node-append': off
'unicorn/prefer-ternary': off
'unicorn/no-lonely-if': off
env:
browser: true
es2024: true
Expand Down
43 changes: 32 additions & 11 deletions concordia/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.models import User
from django.db.models import Count
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.template.defaultfilters import truncatechars
Expand Down Expand Up @@ -80,6 +79,7 @@
SubmittedFilter,
TagCampaignListFilter,
TagCampaignStatusListFilter,
TopicListFilter,
TranscriptionCampaignListFilter,
TranscriptionCampaignStatusListFilter,
TranscriptionProjectListFilter,
Expand All @@ -93,8 +93,9 @@
CampaignAdminForm,
CardAdminForm,
GuideAdminForm,
ItemAdminForm,
ProjectAdminForm,
SanitizedDescriptionAdminForm,
TopicAdminForm,
)

logger = logging.getLogger(__name__)
Expand All @@ -110,13 +111,18 @@ class ConcordiaUserAdmin(UserAdmin):
)

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(Count("transcription"))
qs = super().get_queryset(request).select_related("profile")
return qs

@admin.display(ordering="transcription__count")
@admin.display(
description="Transcription Count", ordering="profile__transcribe_count"
)
def transcription_count(self, obj):
return obj.transcription__count
return obj.profile.transcribe_count

@admin.display(description="Review Count", ordering="profile__review_count")
def review_count(self, obj):
return obj.profile.review_count

EXPORT_FIELDS = (
"username",
Expand All @@ -128,17 +134,31 @@ def transcription_count(self, obj):
"is_superuser",
"date_joined",
"last_login",
"transcription__count",
"profile__transcribe_count",
"profile__review_count",
)

EXTRA_VERBOSE_NAMES = {
"profile__transcribe_count": "transcription count",
"profile__review_count": "review count",
}

def export_users_as_csv(self, request, queryset):
return export_to_csv_action(
self, request, queryset, field_names=self.EXPORT_FIELDS
self,
request,
queryset,
field_names=self.EXPORT_FIELDS,
extra_verbose_names=self.EXTRA_VERBOSE_NAMES,
)

def export_users_as_excel(self, request, queryset):
return export_to_excel_action(
self, request, queryset, field_names=self.EXPORT_FIELDS
self,
request,
queryset,
field_names=self.EXPORT_FIELDS,
extra_verbose_names=self.EXTRA_VERBOSE_NAMES,
)

actions = (anonymize_action, export_users_as_csv, export_users_as_excel)
Expand Down Expand Up @@ -358,8 +378,8 @@ class ResourceAdmin(admin.ModelAdmin, CustomListDisplayFieldsMixin):
list_filter = (
"resource_type",
ResourceCampaignStatusListFilter,
TopicListFilter,
ResourceCampaignListFilter,
"title",
)

def formfield_for_foreignkey(self, db_field, request, **kwargs):
Expand Down Expand Up @@ -396,7 +416,7 @@ def get_fields(self, request, obj=None):

@admin.register(Topic)
class TopicAdmin(admin.ModelAdmin):
form = SanitizedDescriptionAdminForm
form = TopicAdminForm

list_display = (
"id",
Expand Down Expand Up @@ -523,6 +543,7 @@ def item_import_view(self, request, object_id):

@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
form = ItemAdminForm
list_display = ("title", "item_id", "campaign_title", "project", "published")
list_display_links = ("title", "item_id")
search_fields = [
Expand Down
21 changes: 20 additions & 1 deletion concordia/admin/filters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from ..models import Campaign, Project
from ..models import Campaign, Project, Topic


class NullableTimestampFilter(admin.SimpleListFilter):
Expand Down Expand Up @@ -92,6 +92,25 @@ def queryset(self, request, queryset):
return queryset


class TopicListFilter(admin.SimpleListFilter):
"""
Base class for admin topic filters
"""

title = "Topic"
template = "admin/long_name_filter.html"
parameter_name = "topic__id__exact"

def lookups(self, request, model_admin):
queryset = Topic.objects.all()
return queryset.values_list("id", "title").order_by("title")

def queryset(self, request, queryset):
if self.value():
return queryset.filter(**{self.parameter_name: self.value()})
return queryset


class ProjectCampaignListFilter(CampaignListFilter):
parameter_name = "campaign__id__exact"
status_filter_parameter = "campaign__status"
Expand Down
18 changes: 17 additions & 1 deletion concordia/admin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.cache import caches
from tinymce.widgets import TinyMCE

from ..models import Campaign, Card, Guide, Project
from ..models import Campaign, Card, Guide, Item, Project, Topic

FRAGMENT_ALLOWED_TAGS = {
"a",
Expand Down Expand Up @@ -90,6 +90,15 @@ def clean_short_description(self):
)


class TopicAdminForm(SanitizedDescriptionAdminForm):
class Meta(SanitizedDescriptionAdminForm.Meta):
model = Topic
widgets = {
"description": TinyMCE(),
"short_description": TinyMCE(),
}


class CampaignAdminForm(SanitizedDescriptionAdminForm):
class Meta(SanitizedDescriptionAdminForm.Meta):
model = Campaign
Expand All @@ -108,6 +117,13 @@ class Meta(SanitizedDescriptionAdminForm.Meta):
}


class ItemAdminForm(forms.ModelForm):
class Meta:
model = Item
widgets = {"description": TinyMCE()}
fields = "__all__"


class CardAdminForm(forms.ModelForm):
class Meta:
model = Card
Expand Down
3 changes: 3 additions & 0 deletions concordia/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Django:

fields = [
"created_on",
"report_name",
"assets_total",
"assets_published",
"assets_not_started",
Expand All @@ -54,12 +55,14 @@ class Django:
"projects_unpublished",
"anonymous_transcriptions",
"transcriptions_saved",
"daily_review_actions",
"distinct_tags",
"tag_uses",
"campaigns_published",
"campaigns_unpublished",
"users_registered",
"users_activated",
"registered_contributors",
"daily_active_users",
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.13 on 2024-07-29 17:30

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("concordia", "0096_transcription_source"),
]

operations = [
migrations.AlterModelOptions(
name="sitereport",
options={"get_latest_by": "created_on", "ordering": ("-created_on",)},
),
migrations.AddField(
model_name="userprofile",
name="review_count",
field=models.IntegerField(
default=0, verbose_name="transcription review count"
),
),
migrations.AddField(
model_name="userprofile",
name="transcribe_count",
field=models.IntegerField(
default=0, verbose_name="transcription save/submit count"
),
),
migrations.AlterField(
model_name="userprofile",
name="user",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
related_name="profile",
to=settings.AUTH_USER_MODEL,
),
),
]
40 changes: 40 additions & 0 deletions concordia/migrations/0098_userprofile_create_and_population.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Generated by Django 4.2.13 on 2024-07-29 17:40

from django.conf import settings
from django.db import migrations


def create_and_populate_profiles(apps, schema_editor):
User = apps.get_model("auth", "User")
UserProfile = apps.get_model("concordia", "UserProfile")
db_alias = schema_editor.connection.alias
for user in User.objects.using(db_alias).all().iterator(chunk_size=10000):
profile, created = UserProfile.objects.using(db_alias).get_or_create(user=user)
for activity in user.userprofileactivity_set.all():
profile.transcribe_count += activity.transcribe_count
profile.review_count += activity.review_count
profile.save()


def revert_create_and_populate_profiles(apps, schema_editor):
# We can't actually revert the data to the state it was before,
# and there's no actual need to, but we need this function to be
# able to reverse this migration
pass


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
(
"concordia",
"0097_alter_sitereport_options_userprofile_review_count_and_more",
),
]

operations = [
migrations.RunPython(
create_and_populate_profiles, revert_create_and_populate_profiles
),
]
Loading

0 comments on commit 0d1b6f0

Please sign in to comment.