Skip to content

Instantly share code, notes, and snippets.

@sjsakib
Last active November 5, 2017 06:47
Show Gist options
  • Select an option

  • Save sjsakib/ce7099bc18a6e480ef3b6a1a79f7f680 to your computer and use it in GitHub Desktop.

Select an option

Save sjsakib/ce7099bc18a6e480ef3b6a1a79f7f680 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand
from myapp.models import UserProfile
class Command(BaseCommand):
help = 'Update points of all users' # help text
def add_arguments(self, parser): # it takes a `parser` as an argument
parser.add_argument('-s', action='store_true', help='silent')
def handle(self, *args, **options):
for user in UserProfile.objects.all():
points = 0
for problem in user.solved_list.all():
points += problem.type.points
user.points = points
if not options['s']: # We have all our argumets in the options dict
self.stdout.write(self.style.SUCCESS(str(user)+' new points: '+str(points)))
user.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment