Last active
August 4, 2016 04:54
-
-
Save c17r/1d7d33a0e597faf03dde9e19f055dbb0 to your computer and use it in GitHub Desktop.
simple billing strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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