Skip to content

Commit

Permalink
Merge pull request #149 from unicef/office-programme
Browse files Browse the repository at this point in the history
office
  • Loading branch information
domdinicola authored Feb 20, 2025
2 parents 9180178 + c8e7727 commit 62f6dae
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/hope_payment_gateway/api/fsp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
PaymentInstruction,
PaymentInstructionState,
PaymentRecord,
Office,
)


Expand Down Expand Up @@ -75,9 +76,16 @@ class PaymentInstructionViewSet(ProtectedMixin, LoggingAPIViewSet):
search_fields = ["external_code", "remote_id"]

def perform_create(self, serializer) -> None:
owner = get_user_model().objects.get(apitoken=self.request.auth)
owner = get_user_model().objects.filter(apitoken=self.request.auth).first()
system = System.objects.get(owner=owner)
serializer.save(system=system)
obj = serializer.save(system=system)
config_key = obj.extra.get("config_key", None)
if config_key:
office, _ = Office.objects.get_or_create(
code=config_key, defaults={"name": config_key, "slug": config_key, "supervised": True}
)
obj.office = office
obj.save()

def _change_status(self, status):
instruction = self.get_object()
Expand Down
14 changes: 12 additions & 2 deletions src/hope_payment_gateway/apps/gateway/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
FinancialServiceProviderConfig,
PaymentInstruction,
PaymentRecord,
Office,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -386,13 +387,13 @@ def handle_error(self, resp) -> tuple:

@admin.register(PaymentInstruction)
class PaymentInstructionAdmin(ExtraButtonsMixin, admin.ModelAdmin):
list_display = ("external_code", "remote_id", "fsp", "status", "tag")
list_display = ("external_code", "office", "remote_id", "fsp", "status", "tag")
list_filter = ("fsp", "status")
search_fields = ("external_code", "remote_id", "fsp__name", "tag")
formfield_overrides = {
JSONField: {"widget": JSONEditor},
}
raw_id_fields = ("fsp", "system")
raw_id_fields = ("fsp", "system", "office")

@button(permission="gateway.can_export_records")
def export_records(self, request: HttpRequest, pk: int) -> TemplateResponse:
Expand Down Expand Up @@ -484,6 +485,15 @@ class FinancialServiceProviderConfigInline(TabularInline):
extra = 1


@admin.register(Office)
class OfficeAdmin(ExtraButtonsMixin, admin.ModelAdmin):
list_display = ("name", "long_name", "slug", "code", "active")
search_fields = ("name", "slug", "code")
list_filter = ("active",)
readonly_fields = ("remote_id", "slug")
ordering = ("name",)


@admin.register(FinancialServiceProvider)
class FinancialServiceProviderAdmin(ExtraButtonsMixin, admin.ModelAdmin):
list_display = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Generated by Django 5.1.6 on 2025-02-19 15:49

import django.db.models.deletion
import django.utils.timezone
import model_utils.fields
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("gateway", "0030_remove_paymentrecord_marked_for_payment"),
]

operations = [
migrations.CreateModel(
name="Office",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
model_utils.fields.AutoCreatedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="created",
),
),
(
"modified",
model_utils.fields.AutoLastModifiedField(
default=django.utils.timezone.now,
editable=False,
verbose_name="modified",
),
),
(
"remote_id",
models.CharField(blank=True, max_length=100, null=True, unique=True),
),
(
"long_name",
models.CharField(blank=True, db_index=True, max_length=100, null=True),
),
(
"name",
models.CharField(blank=True, db_index=True, max_length=100, null=True),
),
(
"code",
models.CharField(
blank=True,
db_index=True,
max_length=100,
null=True,
unique=True,
),
),
(
"slug",
models.SlugField(blank=True, max_length=100, null=True, unique=True),
),
("active", models.BooleanField(default=False)),
(
"supervised",
models.BooleanField(
default=False,
help_text="Flag to enable/disable offices, which need manual check",
),
),
("extra_fields", models.JSONField(blank=True, default=dict)),
],
options={
"abstract": False,
},
),
migrations.AddField(
model_name="paymentinstruction",
name="office",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="gateway.office",
),
),
]
16 changes: 16 additions & 0 deletions src/hope_payment_gateway/apps/gateway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def __str__(self) -> str:
return f"{self.name} [{self.code}]"


class Office(TimeStampedModel):
remote_id = models.CharField(max_length=100, blank=True, null=True, unique=True)
long_name = models.CharField(max_length=100, blank=True, null=True, db_index=True)
name = models.CharField(max_length=100, blank=True, null=True, db_index=True)
code = models.CharField(max_length=100, blank=True, null=True, db_index=True, unique=True)
slug = models.SlugField(max_length=100, blank=True, null=True, db_index=True, unique=True)
active = models.BooleanField(default=False)
supervised = models.BooleanField(default=False, help_text="Flag to enable/disable offices, which need manual check")

extra_fields = models.JSONField(default=dict, blank=True, null=False)

def __str__(self) -> str:
return str(self.name)


class FinancialServiceProvider(TimeStampedModel):
remote_id = models.CharField(max_length=255, db_index=True, null=True, blank=True)
name = models.CharField(max_length=64, unique=True)
Expand Down Expand Up @@ -85,6 +100,7 @@ class PaymentInstruction(TimeStampedModel):
choices=PaymentInstructionState.choices,
db_index=True,
)
office = models.ForeignKey(Office, on_delete=models.SET_NULL, null=True, blank=True)

tag = models.CharField(null=True, blank=True)
payload = models.JSONField(default=dict, null=True, blank=True)
Expand Down

0 comments on commit 62f6dae

Please sign in to comment.