Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import psycopg2
  3.  
  4. from apilib.utils.seo import get_product_seo_url_tail
  5.  
  6. if __name__ == '__main__':
  7.     conn_string = "host='ddubinin.jail.local' port='1433' dbname='site' user='qa' password=''"
  8.     conn = psycopg2.connect(conn_string)
  9.     cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  10.     while True:
  11.         cursor.execute("""
  12.            SELECT p.sku, p.type, p.name, b.url, b.name as brand_name
  13.            FROM product p LEFT JOIN
  14.                brand b ON (b.id = p.brand_id)
  15.            WHERE p.seo_tail IS NULL
  16.            LIMIT 100;
  17.        """)
  18.         if not cursor:
  19.             break;
  20.         update_cursor = conn.cursor()
  21.         for row in cursor:
  22.             seo_tail = get_product_seo_url_tail(row.get('type').decode('utf-8'), row.get('name').decode('utf-8'), row.get('url').decode('utf-8'))
  23.             update_cursor.execute("""
  24.                UPDATE product
  25.                SET seo_tail = %(seo_tail)s,
  26.                    brand_name = %(brand_name)s
  27.                WHERE sku = %(sku)s
  28.            """, {
  29.                 'seo_tail': seo_tail,
  30.                 'brand_name': row.get('brand_name').decode('utf-8'),
  31.                 'sku': row.get('sku')
  32.             })
  33.         conn.commit()
  34.         update_cursor.close()
  35.     cursor.close()
  36.     conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement