Pro Yearly is on sale from $80 to $50! »

Help a cause by building a donation system

Help a cause by building a donation system

DjangoGirls Vienna April 2017 lightning talk

7874898d532b989d3a1108cade372cd2?s=128

Dražen Lučanin

April 29, 2017
Tweet

Transcript

  1. Help a cause by building a donation system Dražen Lučanin

    @metakermit
  2. • Helping a young green party run for local elections

    in Zagreb • Inspiration for applying your Django skills :) Background
  3. 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
  4. 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')
  5. None
  6. 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 )
  7. 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)
  8. Popular payment services • Stripe • BrainTree

  9. 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()
  10. And it works… • http://www.zagrebjenas.hr/doniraj/

  11. Thanks! • Now go build your own project :)