Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): added custom github stamps #668

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/account/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils.safestring import mark_safe
from django_ace import AceWidget
from rest_framework_api_key.admin import APIKeyAdmin

from scorer.scorer_admin import ScorerModelAdmin
from scorer_weighted.models import Scorer

Expand All @@ -21,6 +22,7 @@
AddressListMember,
AllowList,
Community,
CustomGithubStamp,
Customization,
IncludedChainId,
)
Expand Down Expand Up @@ -218,11 +220,16 @@ class IncludedChainIdInline(admin.TabularInline):
extra = 0


class CustomGithubStampInline(admin.TabularInline):
model = CustomGithubStamp
extra = 0


@admin.register(Customization)
class CustomizationAdmin(ScorerModelAdmin):
form = CustomizationForm
raw_id_fields = ["scorer"]
inlines = [AllowListInline, IncludedChainIdInline]
inlines = [AllowListInline, CustomGithubStampInline, IncludedChainIdInline]
fieldsets = [
(
None,
Expand Down
53 changes: 53 additions & 0 deletions api/account/migrations/0034_customgithubstamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 4.2.6 on 2024-08-30 21:00

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


class Migration(migrations.Migration):
dependencies = [
("account", "0033_accountapikey_analysis_rate_limit"),
]

operations = [
migrations.CreateModel(
name="CustomGithubStamp",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"category",
models.CharField(
choices=[("repo", "Repository"), ("org", "Organization")],
max_length=4,
),
),
(
"value",
models.CharField(
help_text="The repository (e.g. 'passportxyz/passport-scorer') or organization name (e.g. 'passportxyz')",
max_length=100,
),
),
(
"weight",
models.DecimalField(decimal_places=4, default=0.0, max_digits=7),
),
(
"customization",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="custom_github_stamps",
to="account.customization",
),
),
],
),
]
38 changes: 37 additions & 1 deletion api/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from enum import Enum
from typing import Optional, Type

import api_logging as logging
from django.conf import settings
from django.contrib import admin
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db import models
from django.utils.deconstruct import deconstructible
from rest_framework_api_key.models import AbstractAPIKey

import api_logging as logging
from scorer_weighted.models import BinaryWeightedScorer, Scorer, WeightedScorer

from .deduplication import Rules
Expand Down Expand Up @@ -547,6 +548,11 @@ def get_customization_dynamic_weights(self) -> dict:
allow_list.weight
)

for custom_github_stamp in self.custom_github_stamps.all():
weights[
f"CustomGithubStamp#{custom_github_stamp.category}#{custom_github_stamp.value}"
] = str(custom_github_stamp.weight)

return weights

async def aget_customization_dynamic_weights(self) -> dict:
Expand All @@ -555,6 +561,11 @@ async def aget_customization_dynamic_weights(self) -> dict:
address_list = await AddressList.objects.aget(pk=allow_list.address_list_id)
weights[f"AllowList#{address_list.name}"] = str(allow_list.weight)

for custom_github_stamp in self.custom_github_stamps.all():
weights[
f"CustomGithubStamp#{custom_github_stamp.category}#{custom_github_stamp.value}"
] = str(custom_github_stamp.weight)

return weights


Expand All @@ -578,3 +589,28 @@ class AllowList(models.Model):
)

weight = models.DecimalField(default=0.0, max_digits=7, decimal_places=4)


class CustomGithubStamp(models.Model):
class Category(models.TextChoices):
REPOSITORY = "repo"
ORGANIZATION = "org"

customization = models.ForeignKey(
Customization, on_delete=models.PROTECT, related_name="custom_github_stamps"
)

category = models.CharField(
max_length=4,
choices=Category.choices,
blank=False,
)

value = models.CharField(
max_length=100,
blank=False,
null=False,
help_text="The repository (e.g. 'passportxyz/passport-scorer') or organization name (e.g. 'passportxyz')",
)

weight = models.DecimalField(default=0.0, max_digits=7, decimal_places=4)
Loading