def returnDateExtensionReminder(): current_timestamp = Utils.getCurrentTimestamp() return_date = Utils.getDefaultReturnTimestamp(current_timestamp, 3) cursor = mysql.connect().cursor() cursor.execute("""SELECT order_id, user_id FROM orders WHERE DATE(order_return) = DATE('%s') AND order_status >=4 AND order_id NOT IN (SELECT DISTINCT parent_id FROM orders) AND bought = 0 """ % (return_date)) num_items = cursor.rowcount for num in range(num_items): query_data = Utils.fetchOneAssoc(cursor) user = User(query_data['user_id']) print 'order_id:', query_data['order_id'] notification_data = { "notification_id": 3, "entity_id": query_data['order_id'], "order_type": "borrow", "title": "3 Days Left to Return the Book", "message": "Not finised yet? Extend the reading period.", "expanded_text": "You can extend the reading period by going to the order's page in \"My Orders\".", "order_type": "borrow" } Notifications(user.gcm_id).sendNotification(notification_data) #if int(query_data['user_id']) in Utils.getAdmins(): upsellEmail(query_data['order_id'])
def searchFail(): #NOTE deprecated. Done directly from backend return jsonify(status='true') user_id = Utils.getParam(request.form, 'user_id', 'int') q = Utils.getParam(request.form, 'q') q_type = Utils.getParam(request.form, 'type') flow = Utils.getParam(request.form, 'flow', default='borrow') Search(q, {'user_id': user_id}, flow).reportFail(True, True, q_type) return jsonify(status='true')
def wrapper(**kwargs): from app.models import Utils #TODO move it to check app version or some other reliable source user_data = session.get('_user', None) if (Utils.getParam(request.form, 'ref') == 'web' and (user_data is None or (user_data and user_data['user_id'] != Utils.getParam( request.form, 'user_id', 'int')))): return Utils.errorResponse({'status': 'false'}, 'HTTP_STATUS_CODE_CLIENT_ERROR') return func(**kwargs)
def pickupSchedule(): current_ts = Utils.getCurrentTimestamp() date = current_ts.split(' ')[0] # Order Pickup order_list = [] cursor = mysql.connect().cursor() user_id_format_char = ",".join(["%s"] * len(Utils.getAdmins())) cursor.execute( """SELECT COUNT(*) FROM orders WHERE DATE(order_return) = %s AND order_id NOT IN (SELECT DISTINCT parent_id FROM orders) AND user_id NOT IN (""" + user_id_format_char + """) AND order_status >= 4""", tuple([date]) + tuple(Utils.getAdmins())) order_ids = cursor.fetchone() if order_ids[0] > 0: Utils.notifyAdmin(-1, "PICKUPS DUE TODAY!!") cursor.execute( """SELECT COUNT(*) FROM b2b_users WHERE DATE(timestamp) = %s""", (Utils.getDefaultReturnTimestamp(current_ts, -21).split(' ')[0], )) order_ids = cursor.fetchone() if order_ids[0] > 0: Utils.notifyAdmin(-1, "B2B PICKUPS DUE!!") return None
def indexCollections(self, query_condition): #TODO fetch collection object from Collection class cursor = mysql.connect().cursor() condition = 'WHERE c.partial_order = 0' if query_condition: condition += ' AND ' + query_condition cursor.execute("""SELECT c.*, (select group_concat(ci.item_id SEPARATOR ',') FROM collections_items ci WHERE ci.collection_id = c.collection_id) AS item_ids FROM collections c """ + condition) num_items = cursor.rowcount for i in range(num_items): collection = Utils.fetchOneAssoc(cursor) collection['item_ids'] = [ int(_) for _ in collection['item_ids'].split(',') ] collection['metadata'] = self.fetchCollectionsMetadata( collection['collection_id']) try: self.es.index(index=self.es_index, doc_type='collections', id=collection['collection_id'], body=collection, refresh=True) self.updateItems(collection['item_ids'], collection['name']) except Exception, e: print str(e), collection['collection_id']
def add(self, uri, sourceNamespace, setNamespace, batchTag=None): existingRes = self.getExisting(uri, sourceNamespace, setNamespace) Rstatus = "created" myResource = None try: uriContents, ext = Utils.getContent(uri) except ValueError as e: raise ValueError(str(e)) contentHash = self.getHash(uriContents, self.hashAlgorithm) if existingRes: if contentHash == existingRes.hashVal: return existingRes else: myResource = existingRes Rstatus = "updated" else: myResource = model.Resources() resProps = { 'sourceNamespace': sourceNamespace, 'setNamespace': setNamespace, 'batchTag': batchTag, 'status': Rstatus, 'URI': uri, 'lastmod': datetime.today(), 'hashVal': contentHash } for key, value in resProps.items(): setattr(myResource, key, value) resID = model.commitItem(self.session, myResource) myResource.ID = resID self.cacheResult(myResource, uriContents, ext) return marshal(myResource, resource_fields)
def indexItems(self, query_condition='', limit='', custom_keys={}): search_query = """SELECT i.item_id, i.item_name, i.author, i.price,i.ratings, i.num_ratings, i.num_reviews, i.img_small, i.asin, i.goodreads_id, i.summary, i.slug_url, (select group_concat(c.category_name SEPARATOR '|') FROM categories c INNER JOIN items_categories ic ON ic.category_id = c.category_id WHERE ic.item_id = i.item_id) AS categories FROM items i WHERE i.active=1""" if query_condition: search_query += query_condition if limit: search_query += ' LIMIT ' + limit cursor = mysql.connect().cursor() cursor.execute(search_query) num_items = cursor.rowcount for num in range(num_items): record = Utils.fetchOneAssoc(cursor) print record['item_id'] if record['categories'] is not None: record['categories'] = record['categories'].split("|") else: record['categories'] = [] record = self.fetchItemProperties(record, custom_keys) record = self.extendItemProperties(record) record = self.handleUnicode(record) self.indexItemObject(record)
def welcomeMailer(user): name = Utils.getUserName(user) with webapp.app_context(): email = Message('Welcome to Ostrich!', recipients=[user.email]) email.html = transform( render_template('mailers/welcome.html', name=name)) mail.send(email) return True
def thankyou(user): name = Utils.getUserName(user) email = Message('Thank you for offering your book.', recipients=[user.email]) email.html = render_template('mailers/inlined/thank_you.html', name=name) thr = Thread(target=Mailer.send_async_mail, args=[webapp, email]) thr.start() return True
def user_followup(): client = MongoClient(webapp.config['MONGO_DB']) db = client.ostrich cursor = mysql.connect().cursor() ts = str(date.today() - timedelta(1)) cursor.execute( """ select user_id, name, phone, date_created from users where DATE(date_created) = %s and user_id not in (select distinct user_id from orders) """, (ts, )) users = [] for i in range(cursor.rowcount): users.append(OrderedDict(Utils.fetchOneAssoc(cursor))) for user in users: distinct_q = [] cursor.execute( """select query, timestamp from search_fails where user_id = %s""", (user['user_id'], )) user['fails'] = [] for i in range(cursor.rowcount): q = Utils.fetchOneAssoc(cursor) if q['query'] not in distinct_q: user['fails'].append(q) distinct_q.append(q['query']) user['searches'] = [] for query in db.search_log.find({"user_id": str(user['user_id'])}): if query['q'] not in distinct_q: user['searches'].append({ "query": query['q'], "timestamp": str(query['timestamp']) }) distinct_q.append(query['q']) if users: users = json.dumps({'users': users}, indent=4) Mailer.genericMailer( { 'subject': 'Dropped Users for ' + str(ts), 'body': users }, recipients=['*****@*****.**', '*****@*****.**'])
def editReview(self, review_data): conn = mysql.connect() cursor = conn.cursor() cursor.execute( """INSERT INTO reviews_edit_log (review_id, title, description, rating) VALUES (%s, %s, %s, %s)""", (self.review_id, self.title, self.description, self.rating)) conn.commit() title = Utils.getParam(review_data, 'title') description = Utils.getParam(review_data, 'description') rating = Utils.getParam(review_data, 'rating') #TODO delete review if data missing cursor.execute( """UPDATE reviews SET title = %s, description = %s, rating = %s, edited = 1, date_edited = CURRENT_TIMESTAMP WHERE review_id = %s""", (title, description, rating, self.review_id)) conn.commit() return True
def searchString(): response = {'status': 'False'} results = {} query = Utils.getParam(request.args, 'q') page = Utils.getParam(request.args, 'page', var_type='int', default=1) search_type = Utils.getParam(request.args, 'type', default='free') user_id = Utils.getParam(request.args, 'userId', 'int') flow = Utils.getParam(request.args, 'flow', default='borrow') gcm_id = Utils.getParam(request.args, 'gcm_id', default=None) uuid = Utils.getParam(request.args, 'distinct_id', default=None) ref = Utils.getParam(request.args, 'ref', default='mobile') if not query: return Utils.errorResponse(response, 'HTTP_STATUS_CODE_DATA_MISSING') if ref == 'web': return json.dumps(WebUtils.fetchSearchResults(query, search_type, page)) user_info = {'user_id': user_id, 'gcm_id': gcm_id, 'uuid': uuid} search = Search(query, user_info, flow) if search_type == 'free': results = search.basicSearch(page=page - 1) elif search_type == 'category': results = search.categorySearch(page=page - 1) elif search_type == 'collections': results = search.collectionsSearch(page=page - 1) elif search_type == 'isbn': results = search.isbnSearch(page=page - 1) elif search_type == 'auto': results = search.autoComplete() elif search_type == 'custom': results = search.customQuery() return results #log if user_id not in Utils.getAdmins(): Search.logSearch({_: request.args.get(_) for _ in request.args}, search_type) return jsonify(results) if flow != 'admin' else jsonp(results)
def getData(self, review_id, user_id, item_id): cursor = mysql.connect().cursor() if review_id: cursor.execute("""SELECT * FROM reviews WHERE review_id = %s""", (review_id, )) else: cursor.execute( """SELECT * FROM reviews WHERE item_id = %s AND user_id = %s""", (item_id, user_id)) self.data = Utils.fetchOneAssoc(cursor) cursor.close()
def submitReview(review_data): user_id = Utils.getParam(review_data, 'user_id') item_id = Utils.getParam(review_data, 'item_id') order_id = Utils.getParam(review_data, 'order_id') if not (user_id and item_id and order_id): return False title = Utils.getParam(review_data, 'title') description = Utils.getParam(review_data, 'description') rating = Utils.getParam(review_data, 'rating') conn = mysql.connect() cursor = conn.cursor() cursor.execute( """SELECT review_id FROM reviews WHERE user_id = %s AND item_id = %s""", (user_id, item_id)) present_review_id = cursor.fetchone() if present_review_id: review_data['review_id'] = present_review_id[0] review = Review(review_id=review_data['review_id']) review.editReview(review_data) return review_data['review_id'] cursor.execute( """INSERT INTO reviews (user_id, item_id, order_id, title, description, rating) VALUES (%s,%s,%s,%s,%s,%s)""", (user_id, item_id, order_id, title, description, rating)) conn.commit() review_id = cursor.lastrowid #TODO Index item again with review return review_id
def pickupTimeslot(): current_timestamp = Utils.getCurrentTimestamp() return_date = Utils.getDefaultReturnTimestamp(current_timestamp, 1) cursor = mysql.connect().cursor() cursor.execute("""SELECT order_id, user_id, pickup_slot FROM orders WHERE DATE(order_return) = DATE('%s') AND order_status >=4 AND order_id NOT IN (SELECT DISTINCT parent_id FROM orders) """ % (return_date)) num_items = cursor.rowcount for num in range(num_items): query_data = Utils.fetchOneAssoc(cursor) user = User(query_data['user_id']) ts = Order.getTimeSlot(query_data['pickup_slot']) ts = Utils.cleanTimeSlot(ts) notification_data = { "notification_id": 3, "entity_id": query_data['order_id'], "order_type": "borrow", "title": "Return Date Tomorrow", "message": "Would you like to extend your reading period?", "expanded_text": "You can extend the reading period by going to the order's page in \"My Orders\". We'd contact you shortly to confirm your current pickup time: %s" % ts, "order_type": "borrow" } Notifications(user.gcm_id).sendNotification(notification_data)
def wrapper(**kwargs): from app.models import Utils if Utils.getParam(request.args, 'session', default=None): user_data = session.get('_user', None) if user_data and user_data['is_admin']: session.clear() user_data = session.get('_user', None) kwargs['props'] = { 'user': user_data, 'cdn': webapp.config['S3_HOST'] + 'website/', 'host': webapp.config['HOST'] + '/' } return func(**kwargs)
def getRelatedItemsApi(): client = MongoClient(webapp.config['MONGO_DB']) db = client.ostrich item_id = Utils.getParam(request.args, 'item_id', 'int') related_items_cursor = db.related_item_ids.find({'_id': item_id}) related_item_ids = [_ for _ in related_items_cursor] if len(related_item_ids) == 0: #check redis queue getRelatedItemsAsyncWrapper(item_id) return jsonify({'status': 'wait', 'message': 'Crawling in progress'}) related_item_ids = related_item_ids[0]['item_ids'] items = Search().getById(related_item_ids) return json.dumps(items)
def sendMassNotification(self, notification_data, admin=0): if 'notification_id' in notification_data: notification_data['collapse_key'] = notification_data[ 'notification_id'] if admin: admins = ",".join([str(_) for _ in Utils.getAdmins()]) query_condition = " WHERE user_id in (" + admins + ")" else: query_condition = " UNION SELECT gcm_id FROM users_unregistered" cursor = mysql.connect().cursor() cursor.execute("""SELECT gcm_id FROM users""" + query_condition) all_gcm = cursor.fetchall() all_gcm_ids = [] for gcm in all_gcm: all_gcm_ids.append(gcm[0]) self.gcm.json_request(registration_ids=all_gcm_ids, data=notification_data)
def sendUpsellEmail(data): name = Utils.getUserName(data['user']) with webapp.app_context(): consumer_mail = render_template( 'mailers/extend_order.html', name=name, book_name=data['book_name'], order_id=data['order_id'], items=data['items'], curated_items=data['curated_items'], quote=data['quote'], quote_author=data['quote_author']) pre = Premailer(consumer_mail, remove_classes=False, strip_important=False) consumer_mail = pre.transform() email = Message('Enjoying the book?', recipients=[data['user'].email]) email.html = consumer_mail mail.send(email) return True
def render_notification_created_at(record): return Utils.timesince(record.notification_created_at)