Skip to content

Commit

Permalink
attach application to the banner (#607)
Browse files Browse the repository at this point in the history
* attach application to the banner

* add max length

* add db index & modify admin display
  • Loading branch information
larisa17 authored May 29, 2024
1 parent dc3d731 commit 4e37253
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
13 changes: 12 additions & 1 deletion api/passport_admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@

from .models import DismissedBanners, PassportBanner

admin.site.register(PassportBanner)
# admin.site.register(PassportBanner)
admin.site.register(DismissedBanners)


@admin.register(PassportBanner)
class PassportAdmin(admin.ModelAdmin):
"""
Admin class for PassportBanner.
"""

list_display = ("content", "link", "is_active", "application")
search_fields = ("content", "is_active", "link")
list_filter = ("is_active", "application")
16 changes: 13 additions & 3 deletions api/passport_admin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ class Banner(Schema):
content: str
link: Optional[str] = None
banner_id: int
application: str = "passport"


@router.get(
"/banners",
response=List[Banner],
auth=JWTDidAuth(),
)
def get_banners(request):
def get_banners(request, application: Optional[str] = "passport"):
"""
Get all banners
By default, it will return all banners for the Passport application.
"""
try:
address = get_address(request.auth.did)
banners = (
PassportBanner.objects.filter(is_active=True)
PassportBanner.objects.filter(is_active=True, application=application)
.exclude(
pk__in=Subquery(
DismissedBanners.objects.filter(address=address).values("banner_id")
Expand All @@ -41,7 +43,15 @@ def get_banners(request):
.all()
)

return [Banner(content=b.content, link=b.link, banner_id=b.pk) for b in banners]
return [
Banner(
content=b.content,
link=b.link,
banner_id=b.pk,
application=b.application,
)
for b in banners
]
except:
return {
"status": "failed",
Expand Down
22 changes: 22 additions & 0 deletions api/passport_admin/migrations/0004_passportbanner_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.6 on 2024-05-28 18:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("passport_admin", "0001_squashed_0003_remove_passportbanner_name"),
]

operations = [
migrations.AddField(
model_name="passportbanner",
name="application",
field=models.CharField(
choices=[("passport", "Passport"), ("id_staking_v2", "ID Staking V2")],
default="passport",
max_length=50,
),
),
]
8 changes: 8 additions & 0 deletions api/passport_admin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

from django.db import models

APPLICATION_CHOICES = [
("passport", "Passport"),
("id_staking_v2", "ID Staking V2"),
]


class PassportBanner(models.Model):
"""
Expand All @@ -14,6 +19,9 @@ class PassportBanner(models.Model):
content = models.TextField()
link = models.URLField(blank=True, null=True)
is_active = models.BooleanField(default=True)
application = models.CharField(
max_length=50, choices=APPLICATION_CHOICES, default="passport", db_index=True
)


class DismissedBanners(models.Model):
Expand Down

0 comments on commit 4e37253

Please sign in to comment.