import requests
from beer_search_v2.utils import renew_cache
from bs4 import BeautifulSoup
from beer_search_v2.models import Product
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    def get_product_data(self, atvr_id):
        """
        :param atvr_id: The ID of one of ATVR's products.
        :return: A dictionary consisting of store names as keys, and the
        stock status of the product with the given ID as values.
        """

        base_url = "http://www.vinbudin.is/Heim/v%C3%B6rur/stoek-vara.aspx/"
        params = {"productID": atvr_id}
        try:
            html_doc = requests.get(base_url, params=params).text
        except ConnectionError:
            print("Could not establish a connection to ATVR for product {}".format(atvr_id))
            # Defaults to an empty stock
            return []

        soup = BeautifulSoup(html_doc, 'html.parser')
        stock_status = soup.find(id="div-stock-status")
        if stock_status:
            store_names = stock_status.find_all(class_="store")
        else:
            return []  # If the div can't be found, assume the stock is empty.

        info = []
        for store in store_names:
            info.append({"store": store.string, "stock": int(store.next_sibling.string)})
        return info

    def handle(self, *args, **options):
        verbose = True
        products = Product.objects.all()
        for product in products:
            stock_info = self.get_product_data(product.atvr_id)
            product.available_in_atvr = not not stock_info  # Forcing it to a boolean
            if verbose:
                if product.available_in_atvr:
                    availability = "available"
                else:
                    availability = "unavailable"
                print("Updating {}, {}".format(str(product), availability))
            product.atvr_stock = stock_info
            product.save()
            product.product_type.update_availability(verbose=verbose)
        renew_cache()

@@ -126,16 +126,70 @@
 Product%0A
+from django.core.exceptions import ObjectDoesNotExist%0A
 from dja
@@ -1377,141 +1377,54 @@
 def 
-handle(self, *args, **options):%0A        verbose = True%0A        products = Product.objects.all()%0A        for product in products:%0A    
+process_product(self, product, verbose=True):%0A
     
@@ -1479,20 +1479,16 @@
 tvr_id)%0A
-    
         
@@ -1569,20 +1569,16 @@
         
-    
 if verbo
@@ -1577,28 +1577,24 @@
 if verbose:%0A
-    
             
@@ -1631,36 +1631,32 @@
                 
- 
-   
 availability = %22
@@ -1674,28 +1674,24 @@
             
-    
 else:%0A      
@@ -1692,36 +1692,32 @@
                 
-   
- 
 availability = %22
@@ -1721,36 +1721,32 @@
 = %22unavailable%22%0A
-    
             prin
@@ -1801,20 +1801,16 @@
 ility))%0A
-    
         
@@ -1841,36 +1841,32 @@
 ck_info%0A        
-    
 product.save()%0A 
@@ -1864,20 +1864,16 @@
 .save()%0A
-    
         
@@ -1930,16 +1930,1079 @@
 erbose)%0A
+%0A    def add_arguments(self, parser):%0A        parser.add_argument(%0A                %22--verbose%22,%0A                dest=%22verbose%22,%0A                help=%22specify how much the script should write%22,%0A                default=True,%0A                action=%22store_true%22%0A        )%0A        parser.add_argument(%0A                %22--item%22,%0A                dest=%22item%22,%0A                help=%22specify a particular product to update%22%0A        )%0A%0A    def handle(self, *args, **options):%0A        verbose = not not options%5B%22verbose%22%5D%0A        if options%5B%22item%22%5D:  # If an item is supplied, we only process that one%0A            try:%0A                product = Product.objects.get(atvr_id=options%5B%22item%22%5D)%0A                self.process_product(product, verbose)%0A            except ObjectDoesNotExist:%0A                print(%22Nothing found for %7B%7D.%22.format(options%5B%22item%22%5D))%0A        else:  # Otherwise, we process them all%0A            products = Product.objects.filter(atvr_id__isnull=False).all()%0A            for product in products:%0A                self.process_product(product, verbose)%0A    
         

Add optional arguments for single item updates and verbosity