from django.conf import settings
from django.core.management.base import BaseCommand
from instagram.client import InstagramAPI

from quickphotos.utils import update_photos


def update_users(users, download):
    api = InstagramAPI(
        access_token=settings.INSTAGRAM_ACCESS_TOKEN,
        client_secret=settings.INSTAGRAM_CLIENT_SECRET)

    for user in users:
        recent_media, next = api.user_recent_media(user_id=user)
        update_photos(photos=recent_media, download=download)


class Command(BaseCommand):
    help = 'Download and store the latest photos of followed users'

    def add_arguments(self, parser):
        parser.add_argument(
            'users', metavar='user', nargs='+',
            help='Instagram user ID to update')
        parser.add_argument(
            '--download-photos', action='store_true',
            help='Download images from photos and store locally')

    def handle(self, **options):
        update_users(users=options['users'], download=options['download_photos'])

@@ -78,16 +78,50 @@
 Command%0A
+from django.db import transaction%0A
 from ins
@@ -201,16 +201,36 @@
 hotos%0A%0A%0A
+@transaction.atomic%0A
 def upda
@@ -236,23 +236,21 @@
 ate_user
-s
 (user
-s
 , downlo
@@ -393,35 +393,8 @@
 T)%0A%0A
-    for user in users:%0A    
     
@@ -450,20 +450,16 @@
 d=user)%0A
-    
     upda
@@ -964,43 +964,71 @@
     
-update_users(users=options%5B'users'%5D
+for user in options%5B'users'%5D:%0A            update_user(user=user
 , do

Update each user individually