vendors = vendors_file.xpath('//a[@class="otg-vendor-name-link"]/text()') vendors_list = set() #Parse the vendor list for duplicate items of the form 'x (1)', 'x (20)' #by stripping the values and adding it to a set which will not allow duplicates. for vendor in vendors: if "(" in vendor: index = vendor.index('(') vendor = vendor[0:index] vendors_list.add(vendor) #Iterate through the vendor set and if the object isn't already in the #database save the object. for vendor in vendors_list: if not Vendor.objects.filter(name=vendor): vendor_obj = Vendor() vendor_obj.name = vendor vendor_obj.number_of_occurences = 0 vendor_obj.save() #Check all events from the past month and compare the event description string #with the list of vendors. If it matches then add the event to the respective vendor. for event in Event.objects.all(): for vendor in Vendor.objects.all(): if vendor.name.lower() in event.description.encode('ascii', 'ignore').lower(): vendor.events.add(event) #Iterate through vendor objects and calculate number of occurences by looking at #related events for vendor in Vendor.objects.all():
vendors = vendors_file.xpath('//a[@class="otg-vendor-name-link"]/text()') vendors_list = set() #Parse the vendor list for duplicate items of the form 'x (1)', 'x (20)' #by stripping the values and adding it to a set which will not allow duplicates. for vendor in vendors: if "(" in vendor: index = vendor.index('(') vendor = vendor[0:index] vendors_list.add(vendor) #Iterate through the vendor set and if the object isn't already in the #database save the object. for vendor in vendors_list: if not Vendor.objects.filter(name=vendor): vendor_obj = Vendor() vendor_obj.name = vendor vendor_obj.number_of_occurences = 0 vendor_obj.save() #Check all events from the past month and compare the event description string #with the list of vendors. If it matches then add the event to the respective vendor. for event in Event.objects.all(): for vendor in Vendor.objects.all(): if vendor.name.lower() in event.description.encode('ascii','ignore').lower(): vendor.events.add(event) #Iterate through vendor objects and calculate number of occurences by looking at #related events for vendor in Vendor.objects.all(): vendor.number_of_occurences = vendor.events.filter(date__gte=timezone.now() - timedelta(days=30)).count()