def __init__(self, record, sku_placeholder): """ Initialize a new order with all the base information plus one line item. Parameters ---------- record: A CSV record row (see 'csv' module) """ self._error_list = list() self._sku_placeholder = sku_placeholder self._order = record[fields.name].replace("#", "") self._status = "completed" # Only bring in completed orders self._created_at = record[fields.created_at] self._total = record[fields.total] self._cart_tax = record[fields.tax_1_value] self._total_shipping = record[fields.shipping] self._total_discount = record[fields.discount_amount] self._payment_method = record[fields.payment_method] self._currency = record[fields.currency] self._billing_first_name, self._billing_last_name = split_name( record[fields.billing_name]) self._billing_email = record[fields.email] self._billing_address_1 = record[fields.billing_address1] self._billing_address_2 = record[fields.billing_address2] self._billing_phone = record[fields.billing_phone] # Note, odd shopify bug in zipcodes, some start with a ' mark for some reason, so remove that in both # postalcode fields. self._billing_postcode = record[fields.billing_zip].replace("'", "") self._billing_city = record[fields.billing_city] self._billing_state = record[fields.billing_province] self._billing_country = record[fields.billing_country] self._billing_company = record[fields.billing_company] self._shipping_first_name, self._shipping_last_name = split_name( record[fields.shipping_name]) self._shipping_address_1 = record[fields.shipping_address1] self._shipping_address_2 = record[fields.shipping_address2] self._shipping_postcode = record[fields.billing_zip].replace("'", "") self._shipping_city = record[fields.shipping_city] self._shipping_state = record[fields.shipping_province] self._shipping_country = record[fields.shipping_country] self._shipping_company = record[fields.shipping_company] self._note = record[fields.notes] self._shipping_lines = [{ "method_title": record[fields.shipping_method], "total": record[fields.shipping] }] # Add initial item from this record (more can follow) self._item_list = list() self.add_item(record) # If the billing email is empty the import to WooCommerce will fail as it's basically # your 'key' to the customer. if self._billing_email == "": self._add_error( f"% Warning: There is an empty billing email on order {self._order}" )
def delaware_scraper(name): print('Retrieving ' + name + "'s email...") first_name, last_name = split_name(name) cache = Cache() try: email = cache[name] return email except KeyError: pass query_link = DIRECTORIES.get('delaware') driver = get_driver() driver.get(query_link) driver.implicitly_wait(5) driver.find_element_by_id('lastName').send_keys( last_name) driver.find_element_by_id('firstName').send_keys( first_name) driver.find_element_by_css_selector('form button[type="submit"]').click() driver.implicitly_wait(5) time.sleep(3) try: email = driver.find_element_by_xpath('//div[contains(@role, "main")]//a[contains(@href, "mailto")]') email = email.text print(email) except selenium.common.exceptions.NoSuchElementException: email = None driver.quit() email = email if email else None cache[name] = email return email
def upenn_scraper(name): print('Retrieving ' + name + "'s email...") cache = Cache() try: email = cache[name] return email except KeyError: pass first_name, last_name = split_name(name) query_link = DIRECTORIES.get('upenn') driver = get_driver() driver.delete_all_cookies() driver.get(query_link) driver.implicitly_wait(5) driver.find_element_by_css_selector('input[name="lastName"]').send_keys( last_name) driver.find_element_by_xpath('//tr[4]//td//input').send_keys(first_name) driver.find_element_by_css_selector('form a.submitButton').click() driver.implicitly_wait(5) try: email = driver.find_element_by_xpath( '//tr[contains(@class, "lookupbody")]//a[contains(@href, "mailto")]' ) email = email.text print(email) except selenium.common.exceptions.NoSuchElementException: email = None driver.delete_all_cookies() driver.quit() if email is not None: cache[name] = email return email
def temple_scraper(name): print('Retrieving ' + name + "'s email...") cache = Cache() try: email = cache[name] return email except KeyError: pass first_name, last_name = split_name(name) query_link = DIRECTORIES.get('temple') driver = get_driver() driver.delete_all_cookies() driver.get(query_link) driver.implicitly_wait(5) driver.find_element_by_id('templeedusn').send_keys( last_name) driver.find_element_by_id('templeedugivenname').send_keys( first_name) driver.find_element_by_css_selector('form input.Search').click() driver.implicitly_wait(3) try: email = driver.find_element_by_xpath('//div[contains(@id, "Div_Column_02")]//a[contains(@href, "mailto")]') email = email.text print(email) except selenium.common.exceptions.NoSuchElementException: email = None driver.delete_all_cookies() driver.quit() if email is not None: cache[name] = email return email
def add_mp(obj): party, occupation, name, dob, votes, image, marital_status, religion, hometown, last_employment, constituency, region, education, gender, summary, term_start, term_end = [ obj.get(k, "") for k in MP_FIELDS ] try: last, first, middle, title = split_name(name) except: print ">>> Error splitting name:", name, "=> ", title, first, middle, last return person = add_person( name=legal_name(last, first, middle), gender=gender, title=title, dob=dob, image=image, summary=summary ) print constituency, slugify(constituency), constituency_kind, region, slugify(region), region_kind region, _ = Place.objects.get_or_create(slug=slugify(region), defaults={"name": region, "kind": region_kind}) constituency, _ = Place.objects.get_or_create( slug=slugify(constituency), defaults={"name": constituency, "kind": constituency_kind} ) party, _ = Organisation.objects.get_or_create(slug=slugify(party), defaults={"name": party, "kind": party_kind}) # add to party party_position, _ = Position.objects.get_or_create(person=person, title=member_job_title, organisation=party) # add to parliament (as mp) if term_start: term_start = datetime.strptime(term_start, "%B %d %Y").strftime("%Y-%m-%d") if term_end: term_end = datetime.strptime(term_end, "%B %d %Y").strftime("%Y-%m-%d") mp_position, _ = Position.objects.get_or_create( person=person, title=mp_job_title, organisation=parliament, place=constituency, start_date=term_start, end_date=term_end, category=str("political"), ) mp, _ = MP.objects.get_or_create(person=person, party_position=party_position, parliament_position=mp_position) mp.first_name = first mp.middle_name = middle mp.last_name = last mp.occupation = occupation mp.marital_status = marital_status mp.hometown = hometown mp.education = education mp.religion = religion mp.last_employment = last_employment[:150] mp.votes_obtained = votes[:150] mp.save()
def add_person_object(obj): name, dob, gender, image, summary = [obj.get(k, "") for k in PERSON_FIELDS] try: last, first, middle, title = split_name(name) except: print ">>> Error splitting name:", name return add_person(name=legal_name(last, first, middle), gender=gender, title=title, dob=dob, image=image, summary=summary)
def make_actor(self, data): raw_actors = data["credits"]["cast"] actors = [] for actor in raw_actors: firstname, lastname = utils.split_name(actor["name"]) actor = { "firstname": firstname, "lastname": lastname, } actors.append(actor) return actors
def make_crew(self, data): raw_crew = data["credits"]["crew"] crew = [] for person in raw_crew: firstname, lastname = utils.split_name(person["name"]) person = { "firstname": firstname, "lastname": lastname, "role": person["job"] } crew.append(person) return crew
def add_person_object(obj): name, dob, gender, image, summary = \ [obj.get(k, '') for k in PERSON_FIELDS] try: last, first, middle, title = split_name(name) except: print ">>> Error splitting name:", name return add_person(name=legal_name(last, first, middle), gender=gender, title=title, dob=dob, image=image, summary=summary)
def clean_students(self): """ Ignore blanks lines and remove trailing spaces in the student name. Validates that the students count match the limit and that there is not repeated name. """ names = [n.strip() for n in self.cleaned_data['students'].splitlines()] names = filter(None, names) # remove blank lines if len(names) > self.klass.max_students: msg = _('You cannot set more than {0} students.') raise forms.ValidationError(msg.format(self.klass.max_students)) seen = set() for name in names: if name in seen: msg = _('The name {0} appears more than once.') raise forms.ValidationError(msg.format(name)) seen.add(name) return [split_name(n) for n in names if n]
def create_teacher(self, name, email): """ Creates and returns users not found by email, otherwise, reuse them. Returns the user instance and the password used when created, or None if the password wasn't set because the user already exists. """ try: user = User.objects.get(email=email) password = None except User.DoesNotExist: first_name, last_name = split_name(name) user = User(username=id_as_username(prefix='teacher'), first_name=first_name, last_name=last_name, email=email) password = generate_random_passwords(1, flat=True) user.set_password(password) user.save() return user, password
def add_mp(obj): party, occupation, name, dob, votes, image, \ marital_status, religion, hometown, last_employment, constituency, \ region, education, gender, summary, term_start, term_end = [obj.get(k, '') for k in MP_FIELDS] try: last, first, middle, title = split_name(name) except: print ">>> Error splitting name:", name, "=> ", title, first, middle, last return person = add_person(name=legal_name(last, first, middle), gender=gender, title=title, dob=dob, image=image, summary=summary) print constituency, slugify( constituency), constituency_kind, region, slugify(region), region_kind region, _ = Place.objects.get_or_create(slug=slugify(region), defaults={ 'name': region, 'kind': region_kind }) constituency, _ = Place.objects.get_or_create(slug=slugify(constituency), defaults={ 'name': constituency, 'kind': constituency_kind }) party, _ = Organisation.objects.get_or_create(slug=slugify(party), defaults={ 'name': party, 'kind': party_kind }) # add to party party_position, _ = Position.objects.get_or_create(person=person, title=member_job_title, organisation=party) # add to parliament (as mp) if (term_start): term_start = datetime.strptime(term_start, '%B %d %Y').strftime("%Y-%m-%d") if (term_end): term_end = datetime.strptime(term_end, '%B %d %Y').strftime("%Y-%m-%d") mp_position, _ = Position.objects.get_or_create(person=person, title=mp_job_title, organisation=parliament, place=constituency, start_date=term_start, end_date=term_end, category=str('political')) mp, _ = MP.objects.get_or_create(person=person, party_position=party_position, parliament_position=mp_position) mp.first_name = first mp.middle_name = middle mp.last_name = last mp.occupation = occupation mp.marital_status = marital_status mp.hometown = hometown mp.education = education mp.religion = religion mp.last_employment = last_employment[:150] mp.votes_obtained = votes[:150] mp.save()
def clean_name(self): """ Returns the name as a tuple of first name and last name. """ name = self.cleaned_data['name'] return split_name(name)
def test_split_name(self): for name, split, legal in self.NAMES: self.assertEqual(split, split_name(name)) last_name, first_name, middle_name, title = split self.assertEqual(legal, legal_name(last_name, first_name, middle_name))
def retrieve(self, request, pk=None): """ Retrieves an item by id. --- parameters: - name: copy_to description: Copies the retrieved item into another workspace, identified by the given id. paramType: query - name: target_name description: An alternative pckg.name for the item to be copied under. paramType: query """ copy_to_id_param = request.query_params.get('copy_to') target_name_param = request.query_params.get('target_name') if not copy_to_id_param: return super(FunctionSigViewSet, self).retrieve(request, pk) else: try: obj = FunctionSig.objects.get(pk=pk) except django.core.exceptions.ObjectDoesNotExist: return Response( {'error when copying function': 'resource does not exist'}, status=status.HTTP_404_NOT_FOUND) try: wspc = Workspace.objects.get(pk=copy_to_id_param) except: return Response( {'error when copying function': 'target workspace does not exist or inaccessible'}, status=status.HTTP_400_BAD_REQUEST) obj.workspace = wspc self.check_object_permissions(request, obj) pckg = name = None if target_name_param: pckg, name = utils.split_name(target_name_param) cloner = WorkspaceCloner(wspc, None, request.user, target_workspace=wspc, context={'request': request}) clone = None try: clone = cloner.clone_function(obj, pckg, name) except IntegrityError as e: if clone and clone.pk: clone.delete() return Response( {'error when copying function': 'integrity error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) except: if clone and clone.pk: clone.delete() return Response( {'error when copying function': 'internal error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) serializer = FunctionSigSerializer(clone, context={'request': request}) return Response(serializer.data)