Skip to content

Instantly share code, notes, and snippets.

@c17r
Last active August 4, 2016 04:54
Show Gist options
  • Save c17r/1d7d33a0e597faf03dde9e19f055dbb0 to your computer and use it in GitHub Desktop.
Save c17r/1d7d33a0e597faf03dde9e19f055dbb0 to your computer and use it in GitHub Desktop.
simple billing strategy
import arrow
class Org(models.Model):
last_bill = models.DateTimeField(auto_now_add=True)
class User(models.Model):
org = models.ForeignKey('Org')
last_login = models.DateTimeField() # needs to be updated as part of login process
import arrow
@app.task
def run_billing():
now = arrow.utcnow()
orgs_to_bill = Org.objects.filter(last_bill__lte=now.replace(days=-30).datetime)
for org in orgs_to_bill:
users = Users.objects.filter(org=org, last_login__gte=org.last_bill)
# generate invoice based on len(users)
org.last_bill = now.datetime
org.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment