From ee2d63f7844ee5df87f6fb6706515e7d7ffad81e Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 30 Apr 2024 14:34:45 -0400 Subject: [PATCH] membershipworks: Add support for scraping event registration data --- membershipworks/membershipworks_api.py | 16 ++++++++++++++ ...xt_registrations_alter_eventinvoice_pdf.py | 22 +++++++++++++++++++ membershipworks/models.py | 1 + membershipworks/tasks/scrape.py | 2 ++ 4 files changed, 41 insertions(+) create mode 100644 membershipworks/migrations/0017_eventext_registrations_alter_eventinvoice_pdf.py diff --git a/membershipworks/membershipworks_api.py b/membershipworks/membershipworks_api.py index aa1f550..b8ab1fa 100644 --- a/membershipworks/membershipworks_api.py +++ b/membershipworks/membershipworks_api.py @@ -269,6 +269,22 @@ class MembershipWorks: ) return r.json() + def get_event_registrations(self, event_id: str): + r = self._get_v1( + BASE_URL + "/v1/csv", + params={ + "_rt": "946702800", # unknown + "evt": event_id, + }, + ) + if r.status_code != requests.codes.ok: + raise MembershipWorksRemoteError("csv generation", r) + + if r.text[0] == "\ufeff": + r.encoding = r.encoding + "-sig" + + return list(csv.DictReader(StringIO(r.text))) + def get_event_by_url(self, url: str): """Retrieve a specific event by its url""" r = self.sess.get( diff --git a/membershipworks/migrations/0017_eventext_registrations_alter_eventinvoice_pdf.py b/membershipworks/migrations/0017_eventext_registrations_alter_eventinvoice_pdf.py new file mode 100644 index 0000000..b06790d --- /dev/null +++ b/membershipworks/migrations/0017_eventext_registrations_alter_eventinvoice_pdf.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.4 on 2024-04-30 05:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("membershipworks", "0016_eventinvoice"), + ] + + operations = [ + migrations.AddField( + model_name="eventext", + name="registrations", + field=models.JSONField(blank=True, null=True), + ), + migrations.AlterField( + model_name="eventinvoice", + name="pdf", + field=models.FileField(upload_to="protected/invoices/%Y/%m/%d/"), + ), + ] diff --git a/membershipworks/models.py b/membershipworks/models.py index 70c2b5e..d1ea11e 100644 --- a/membershipworks/models.py +++ b/membershipworks/models.py @@ -551,6 +551,7 @@ class EventExt(Event): max_digits=13, decimal_places=4, default=0 ) details = models.JSONField(null=True, blank=True) + registrations = models.JSONField(null=True, blank=True) def get_absolute_url(self) -> str: return reverse("membershipworks:event-detail", kwargs={"eid": self.eid}) diff --git a/membershipworks/tasks/scrape.py b/membershipworks/tasks/scrape.py index 38b0ed1..7bdad2a 100644 --- a/membershipworks/tasks/scrape.py +++ b/membershipworks/tasks/scrape.py @@ -110,6 +110,7 @@ def scrape_event_details(queryset: QuerySet[EventExt]): for event in queryset: event.details = membershipworks.get_event_by_eid(event.eid) + event.registrations = membershipworks.get_event_registrations(event.eid) event.save() @@ -171,4 +172,5 @@ def scrape_events(): event_ext.end or event_ext.start ): event_ext.details = membershipworks.get_event_by_eid(event.eid) + event_ext.registrations = membershipworks.get_event_registrations(event.eid) event_ext.save()