示例#1
0
    def handle(self, *args, **options):
        old_brand_names_count = Brand.objects.count()
        p("database_count", old_brand_names_count)
        all_brand_names = Command.soup_to_brand_names(Command.get_full_soup())
        new_brand_names = Command.select_new_brands(all_brand_names)

        if len(new_brand_names):
            p("new_count", len(all_brand_names), len(new_brand_names))
            interactive_mode = True
            for i, new_brand_name in enumerate(new_brand_names):
                if interactive_mode:
                    question = msg["write_to_instance"] % new_brand_name
                    raw_ans = raw_input(question)
                    ans = raw_ans[0].lower() if raw_ans else 'n'
                    if ans == 'a':
                        p('allow_all', len(new_brand_names) - i - 1)
                        interactive_mode = False
                    elif ans == 'q':
                        abort()
                    elif ans == 'n':
                        p('skip_create', new_brand_name)
                        continue
                    elif ans == 'y':
                        p('ok_create')
                    else:
                        p('invalid_choice', raw_ans)

                new_brand = Brand.objects.create(
                    name=new_brand_name,
                    slug=strings.sluggify(new_brand_name))
                p('has_been_created', new_brand.name)

            abort()
        else:
            p("no_new_brands")
            abort()
示例#2
0
文件: models.py 项目: anon303/fishnet
 def save(self, *args, **kwargs):
     self.slug = strings.sluggify(self.name)
     super(Shop, self).save(*args, **kwargs)
    def handle(self, *args, **options):

        start_time = time.time()
        shop = Command.get_shop_instance(options['shop_slug'])
        Command.initial_checks(shop)

        self.skipped_names = []
        self.added_names = []
        initial_brand_count = shop.brands.count()
        soup = web.get_full_soup(shop.brands_url())
        mined_brand_names = self.soup_to_brand_names(
            soup, shop.search_brands_soup_call)
        alt_brand_names = [abn.alternative_name.lower() for abn in
                           ABN.objects.all()]

        for mined_name in mined_brand_names:
            p("-")
            slug = strings.sluggify(mined_name)

            # If the lowercase mined name occurs in the list of alternative
            # brand names, we can add the recognized brand to this shop and
            # continue to the next mined name:

            if mined_name.lower() in alt_brand_names:
                abn = ABN.objects.filter(
                            alternative_name__icontains=mined_name.lower())[0]
                if shop.brands.filter(name=abn.brand.name).exists():
                    p("found_already_added_brand", abn.brand.name)
                else:
                    self.add(shop, mined_name, abn.brand)
                continue

            # We assume the mined name corresponds with a readily existing
            # database entry for a Brand: we try/except to get it, and add it
            # to the shop's list (if this weren't done before):

            try:
                known_brand = Brand.objects.get(slug=slug)
                if known_brand in shop.brands.all():
                    p('found_already_added_brand', known_brand.name)
                else:
                    self.add(shop, mined_name, known_brand)

            # When the mined name doesn't have a corresponding Brand in the
            # database, we look for all partial matches, list them, and the
            # user may or may not choose one of these partial matches:

            except Brand.DoesNotExist:
                p('found_unknown_brand', mined_name)
                matching_brands = Command.get_matching_brands(slug)
                if matching_brands:
                    Command.list_partial_matches(mined_name, matching_brands)
                    while True:
                        ans = q("add_partial_match?", default='1')
                        if ans == 'n':
                            self.skip_found_name(mined_name, shop)
                        else:
                            try:
                                ans_index = int(ans) - 1
                                existing_brand = matching_brands[ans_index]
                            except (ValueError, IndexError):
                                p("retarded_answer", ans)
                                continue
                            ans = q("sure_match?", existing_brand.name,
                                    mined_name, default='y')
                            if ans == 'y':
                                shop.brands.add(existing_brand)
                                p("linked_brands_ok", existing_brand.name)

                                if not ABN.objects.filter(
                                        alternative_name=mined_name).exists():
                                    abn = ABN.objects.create(
                                        alternative_name=mined_name,
                                        brand=existing_brand,
                                        shop=shop)
                                    p("new_alt_brand_name", abn)
                            else:
                                self.skip_found_name(mined_name, shop)
                        break
                else:
                    self.skip_found_name(mined_name, shop)

        final_brand_count = shop.brands.count()
        diff_brand_count = final_brand_count - initial_brand_count
        duration = time.time() - start_time

        nl()
        p('=')
        print('\nRESULT for indexing brands sold at shop "%s":\n' % shop.name)
        print("[*] command duration.............: %.2f sec" % duration)
        print("[*] shop's initial brand count...: %i" % initial_brand_count)
        print("[*] shop's final brand count.....: %i" % final_brand_count)

        print("[*] brands added to shop.........: %i" % diff_brand_count)
        if self.added_names:
            nl()
            for i, added_name in enumerate(self.added_names):
                print("\t%2.i - %s" % (i + 1, added_name))
            nl()

        self.skipped_names = filter(bool, self.skipped_names)
        print("[*] names found but skipped: %i" % len(self.skipped_names))
        if self.skipped_names:
            nl()
            for i, skipped_name in enumerate(self.skipped_names):
                print("\t%2.i - %s" % (i + 1, skipped_name))
            nl()