import logging

from django.core.management.base import NoArgsCommand
from django.contrib.auth import get_user_model
from django.conf import settings
from django.db import IntegrityError
from django.db import transaction
from django.contrib.auth.models import Group

import requests

from voting.models import VoteToken

UserModel = get_user_model()


class Command(NoArgsCommand):
    help = 'Loads Ticket Holders from Entrio API and creates django users.'

    def handle_noargs(self, **options):
        try:
            with transaction.atomic():
                group = Group.objects.create(name=settings.TICKET_HOLDER_GROUP_NAME)
        except IntegrityError:
            group = Group.objects.get(name=settings.TICKET_HOLDER_GROUP_NAME)

        url = "https://www.entrio.hr/api/get_visitors?key=%s&format=json" % settings.ENTRIO_API_KEY
        try:
            r = requests.get(url)
            if r.status_code != 200:
                logging.error(
                    'Unexpected response from Entrio API. Status code: {0}'.format(r.status_code)
                )
            else:
                ticket_holders = r.json()
                for user in ticket_holders:
                    email = user['E-mail'].strip()
                    ticket_code = user['ticket_code'].strip()
                    try:
                        with transaction.atomic():
                            u = UserModel.objects.create_user(
                                email=email,
                                first_name=user['First name'],
                                last_name=user['Last name'],
                                password=ticket_code)
                            group.user_set.add(u)

                    except IntegrityError as e:
                        u = UserModel.objects.get(email=email)
                        if not u.is_ticket_holder:
                            group.user_set.add(u)

                    token, created = VoteToken.objects.get_or_create(
                        user=u,
                        defaults={
                            'ticket_code': ticket_code,
                            'user': u,
                        }
                    )

        except requests.exceptions.RequestException as e:
            logging.error('Requests error when talking to the Entrio API: {0}'.format(e))

@@ -7,16 +7,89 @@
  logging
+%0Aimport sys%0A%0Afrom json import loads%0Afrom urllib2 import urlopen, URLError
 %0A%0Afrom d
@@ -942,23 +942,25 @@
    r
- = requests.get
+esponse = urlopen
 (url
@@ -977,253 +977,307 @@
     
-if r.status_code != 200:%0A                logging.error(%0A                    'Unexpected response from Entrio API. Status code: %7B0%7D'.format(r.status_code)%0A                )%0A            else:%0A                ticket_holders = r.json()%0A     
+ticket_holders = loads(response.read())%0A            self.process_ticket_holders(group, ticket_holders)%0A        except URLError as e:%0A            logging.error(e)%0A            sys.stderr.write(%22Failed loading entrio data: %25r%22 %25 e)%0A%0A    def process_ticket_holders(self, group, ticket_holders):%0A
         
    f
@@ -1272,19 +1272,16 @@
         
-   
 for user
@@ -1308,32 +1308,24 @@
             
-       
- 
 email = user
@@ -1339,32 +1339,24 @@
 l'%5D.strip()%0A
-        
             
@@ -1409,37 +1409,21 @@
         
- 
-       try:%0A        
+try:%0A
         
@@ -1473,32 +1473,24 @@
             
-     
-   
 u = UserMode
@@ -1532,32 +1532,24 @@
             
-       
- 
 email=email,
@@ -1549,24 +1549,16 @@
 =email,%0A
-        
         
@@ -1628,24 +1628,16 @@
         
- 
-       
 last_nam
@@ -1681,24 +1681,16 @@
         
-        
 password
@@ -1719,32 +1719,24 @@
             
-     
-   
 group.user_s
@@ -1758,24 +1758,16 @@
         
-       
- 
 except I
@@ -1782,32 +1782,24 @@
 Error as e:%0A
-        
             
@@ -1857,24 +1857,16 @@
         
- 
-       
 if not u
@@ -1900,32 +1900,24 @@
             
-        
 group.user_s
@@ -1939,24 +1939,16 @@
         
-     
-   
 token, c
@@ -2009,32 +2009,16 @@
     
-        user=u,%0A        
+user=u,%0A
     
@@ -2060,24 +2060,16 @@
         
- 
-       
 'ticket_
@@ -2108,24 +2108,16 @@
         
-        
 'user': 
@@ -2135,24 +2135,16 @@
         
-     
-   
 %7D%0A      
@@ -2153,163 +2153,6 @@
     
-        )%0A%0A        except requests.exceptions.RequestException as e:%0A            logging.error('Requests error when talking to the Entrio API: %7B0%7D'.format(e)
 )%0A

Use urllib instead of requests to fetch entrio data