Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Help a cause by building a donation system

Help a cause by building a donation system

DjangoGirls Vienna April 2017 lightning talk

Dražen Lučanin

April 29, 2017
Tweet

More Decks by Dražen Lučanin

Other Decks in Programming

Transcript

  1. • Helping a young green party run for local elections

    in Zagreb • Inspiration for applying your Django skills :) Background
  2. Functionality • Donation form with field validation • Talking to

    an external API to generate a scannable 2D barcode • Email the payment information • Admin interface ◦ confirming payments ◦ exporting reports
  3. Model class Donation(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) submitted_time =

    models.DateTimeField(auto_now_add=True) OIB = models.CharField(max_length=11, validators=[oib_validator]) name = models.CharField(max_length=30, validators=[utf8_30_validator]) email = models.EmailField() street = models.CharField(max_length=27, validators=[utf8_27_validator]) place = models.CharField(max_length=27, validators=[utf8_27_validator]) phone = models.CharField(max_length=30, blank=True) # optional physical_person = models.BooleanField(default=True) amount = models.PositiveIntegerField() paid = models.BooleanField(default=False) barcode = models.ImageField(blank=True, upload_to='barcodes/%Y/%m/%d') invoice_image = models.ImageField(blank=True, upload_to='invoices/%Y/%m/%d')
  4. Talking to an external API – requests hub3_args = {

    "renderer": "image", "data": { "amount": self.amount, "sender": { "name": self.name, }, "receiver": { "iban": settings.DONATIONS_RECIPIENT_IBAN, } } } response = requests.post( 'https://hub3.bigfish.software/api/v1/barcode', json=hub3_args )
  5. We even got a bit fancy using PIL… img =

    Image.open(blank_invoice_path) draw = ImageDraw.Draw(img) draw.text((749, 132), settings.DONATIONS_RECIPIENT_IBAN, text_color, font=font)
  6. Email the payment info – django-anymail + Mailgun msg =

    EmailMultiAlternatives( subject="Uplatnica za donaciju", body=text_content, to=[f"{donation.name} <{donation.email}>"] ) msg.attach('uplatnica.jpg', invoice_file.getvalue(), 'image/jpeg') msg.attach('barcode.png', barcode_file.getvalue(), 'image/png') # Send it: msg.send()