Created
February 22, 2013 00:46
-
-
Save shofetim/5009883 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from celery.task import task | |
| import pyes | |
| import json | |
| from django.conf import settings | |
| from warehouse_pieces.models import PieceMeta | |
| from products.models import SimpleProduct | |
| from lib.aquameta.pricelevel import RETAIL, WHOLESALE | |
| @task | |
| def update_search(mapping=False): | |
| conn = pyes.ES(settings.SEARCH_ENDPOINT) | |
| try: | |
| conn.indices.create_index(settings.DEFAULT_SEARCH_INDEX) | |
| except: | |
| conn.indices.delete_index(settings.DEFAULT_SEARCH_INDEX) | |
| conn.indices.create_index(settings.DEFAULT_SEARCH_INDEX) | |
| if not mapping: | |
| #Read the ES docs | |
| mapping = { | |
| "_all": { | |
| "enabled": True | |
| }, | |
| "id": { | |
| "boost": 1.0, | |
| "index": "not_analyzed", | |
| "store": "yes", | |
| "type": "integer" | |
| }, | |
| "image": { | |
| "boost": 1.0, | |
| "index": "not_analyzed", | |
| "store": "yes", | |
| "type": "string" | |
| }, | |
| "size_retail": { | |
| "boost": 1.0, | |
| "index": "not_analyzed", | |
| "store": "yes", | |
| "type": "object" | |
| }, | |
| "size_wholesale": { | |
| "boost": 1.0, | |
| "index": "not_analyzed", | |
| "store": "yes", | |
| "type": "object" | |
| }, | |
| "name": { | |
| "boost": 7.0, | |
| "index": "analyzed", | |
| "store": "yes", | |
| "type": "string", | |
| "term_vector": "with_positions_offsets" | |
| }, | |
| "brand": { | |
| "boost": 10.0, | |
| "index": "analyzed", | |
| "store": "yes", | |
| "type": "string", | |
| "term_vector": "with_positions_offsets" | |
| }, | |
| "description": { | |
| "boost": 1.0, | |
| "index": "analyzed", | |
| "store": "yes", | |
| "type": "string", | |
| "term_vector": "with_positions_offsets" | |
| }, | |
| "code": { | |
| "boost": 1.0, | |
| "index": "not_analyzed", | |
| "store": "yes", | |
| "type": "string" | |
| } | |
| } | |
| conn.indices.put_mapping("product-type", {'properties':mapping}, [settings.DEFAULT_SEARCH_INDEX]) | |
| piece_meta = PieceMeta.objects.all() | |
| count = 0 | |
| for pm in piece_meta: | |
| count += 1 | |
| #debugging | |
| if count > 100: | |
| exit() | |
| pm = _get_products(pm) | |
| _id = pm.products[0].id | |
| try: | |
| image = unicode(pm.products[0].cached_primary_image) | |
| except: | |
| image = "generic_images/no_image.gif" | |
| brand = unicode(pm.products[0].brand) | |
| name = pm.piecemeta.display_name | |
| code = pm.products[0].code | |
| description = pm.description | |
| sizes_retail = [] | |
| for p in pm.products: | |
| sizes_retail.append(p.size + " - " + _price(product, RETAIL)) | |
| sizes_wholesale = [] | |
| for p in pm.products: | |
| sizes_wholesale.append(p.size + " - " + _price(product, WHOLESALE)) | |
| #for debugging | |
| print {"id": _id, "image": image, "sizes_retail": sizes_retail, "sizes_wholesale": sizes_wholesale, "name": name, "brand": brand, "description": description, "code": code} | |
| try: | |
| conn.index({ | |
| "id": _id, | |
| "image": image, | |
| "sizes_retail": sizes_retail, | |
| "sizes_wholesale": sizes_wholesale, | |
| "name": name, | |
| "brand": brand, | |
| "description": description, | |
| "code": code}, | |
| settings.DEFAULT_SEARCH_INDEX, | |
| "product-type", | |
| count) | |
| except: | |
| print p | |
| conn.indices.refresh(settings.DEFAULT_SEARCH_INDEX) | |
| #copied and modified from beehive/lib/aquameta/template_tags_and_filters/templatetags/beehive_tags.py ln:30 | |
| def _get_products(piecemetas): | |
| import ipdb | |
| ipdb.set_trace() | |
| if piecemetas: | |
| metas = {} | |
| simpleprods = SimpleProduct.objects.should_show_up_in_search().filter(piece__piece_meta__in=piecemetas).select_related("piece", "piece__piece_meta", "piece__piece_meta__brand") | |
| for prod in simpleprods: | |
| if not metas.has_key(prod.piece.piece_meta): | |
| metas[prod.piece.piece_meta] = {'piecemeta': prod.piece.piece_meta, 'products': []} | |
| metas[prod.piece.piece_meta]['products'].append(prod) | |
| return [metas[pm] for pm in piecemetas if metas.has_key(pm)] | |
| #copied and modified from beehive/lib/aquameta/template_tags_and_filters/templatetags/beehive_tags.py ln:43 | |
| def _price(product, level): | |
| if product.is_priced_by_weight: | |
| price_obj = product.sell_price_per_pound(level) | |
| else: | |
| price_obj = product.sell_price(level) | |
| amount, off = price_obj | |
| if off == False: | |
| off = "" | |
| try: | |
| amount = float(amount) | |
| if amount: | |
| if product.is_priced_by_weight: | |
| price = "$%s per lb." % intcomma(floatformat(amount,2)) | |
| else: | |
| price = "$%s" % intcomma(floatformat(amount,2)) | |
| if amount > 0 and off != "": | |
| return mark_safe("<nobr>%s %s%% off</nobr>" % (price, off)) | |
| elif amount > 0: | |
| return price | |
| else: | |
| return mark_safe("<span class='negative_amount'>%s</span>" % price) | |
| except: | |
| pass | |
| return "$0.00" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment