def utx(): user = PMMUser.get(recipe.parent_key()) user.rec_pub -= 1 if user.rec_pub < 0: user.rec_pub = 0 user.put() cache_delete('gfcu', recipe.parent_key().name())
def list(request, ordering='popularne'): if ordering not in ORDERINGS.keys(): return NotFound() prev, recipes, next = PagerQuery(models.Recipe) \ .filter('rec_vis =', VISIBILITY[2]) \ .filter('disabled =', False) \ .order(ORDERINGS[ordering]) \ .fetch(settings.PAGE_SIZE, request.args.get('b', None)) # Get authors, TODO: OPTIMIZE authors = PMMUser.get([recipe.parent_key() for recipe in recipes]) for i, recipe in enumerate(recipes): if recipe and authors[i]: recipe.author = authors[i] if request.is_xhr: template = 'przepisy/includes/list.html' else: template = 'przepisy/list.html' return render_to_response(template, { 'recipes': recipes, 'prev': prev, 'ordering': ordering, 'next': next, })
def utx(): user = PMMUser.get(self.request.user.key()) user.rec_pub -= 1 if user.rec_pub < 0: user.rec_pub = 0 user.put() cache_delete('gfcu', self.request.user.key().name())
def search(cls, q, limit=201, order=None, exact=True): if not q: return [] # Replace last known username with his id or if unknown, pass as user 0 author_match = AUTHOR_REGEXP.search(q) if author_match: author = PMMUser.all().filter('display_name =', author_match.group(1)).fetch(1) if author: q = q.replace(author_match.group(0), u'autor:"%s"' % author[0].key().name()) filters = [f.group(0) for f in FILTER_REGEXP.finditer(q)] for f in filters: q = q.replace(f, '') multi = [multi.group(0)[1:-1].lower() for multi in MULTIWORD_REGEXP.finditer(q)] for m in multi: q = q.replace(m, '') keywords = set(DELIMETER_REGEXP.sub(' ', q).lower().split()) keywords |= set(multi) keywords |= set(filters) keywords = filter(lambda x: len(x) >= 2, keywords) keywords = list(keywords)[:6] # Only do first 6 to limit filtering/queries if not keywords: return [] if not exact: if len(keywords) > 1: query = RecipeIdx.all().filter('idx IN', keywords) else: query = RecipeIdx.all().filter('idx =', keywords[0]) if order: query = query.order(order) index_keys = [entry.parent_key() for entry in query.fetch(limit)] else: if not order: query = RecipeIdx.all(keys_only=True) for keyword in keywords: query = query.filter('idx =', keyword) index_keys = [key.parent() for key in query.fetch(limit)] else: query = RecipeIdx.all() for keyword in keywords: query = query.filter('idx =', keyword) results = query.fetch(limit) # Sort in memory... _key = order[1:] if order[0] == '-' else order _reverse = True if order[0] == '-' else False results = sorted(results, key=lambda x: getattr(x, _key), reverse=_reverse) index_keys = [entry.parent_key() for entry in results] return index_keys
def category(request, slug, ordering='popularne'): if slug in models.CATEGORIES_SV.keys(): category = get_by_key_name_or_404(models.Category, models.CATEGORIES_SV[slug]) else: return NotFound() if ordering not in ORDERINGS.keys(): return NotFound() if category.counter == 0: prev, recipes, next = None, [], None else: prev, recipes, next = PagerQuery(models.Recipe) \ .filter('rec_vis =', VISIBILITY[2]) \ .filter('disabled =', False) \ .filter('category =', slug) \ .order(ORDERINGS[ordering]) \ .fetch(settings.PAGE_SIZE, request.args.get('b', None)) # Get authors, TODO: OPTIMIZE authors = PMMUser.get([recipe.parent_key() for recipe in recipes]) for i, recipe in enumerate(recipes): if recipe and authors[i]: recipe.author = authors[i] if request.is_xhr: return render_to_response('przepisy/includes/category_list.html', { 'recipes': recipes, 'prev': prev, 'next': next, 'ordering': ordering, 'category_slug': slug }) else: categories = [c for c in models.Category.get_for_lists() if c['name'] != category.key().name()] return render_to_response('przepisy/category.html', { 'recipes': recipes, 'prev': prev, 'next': next, 'ordering': ordering, 'category_slug': slug, 'category': category.key().name(), 'categories': categories, })
def atom_feed(request): feed = AtomFeed(u"Najnowsze przepisy diety Montignac", feed_url=request.url, url=request.host_url, subtitle=u"Lista 20 najnowszych przepisów publicznych dodanych przez użytkowników witryny.") recipes = models.Recipe.all().filter('rec_vis =', VISIBILITY[2]).filter('disabled =', False).order('-created').fetch(20) # Get authors, TODO: OPTIMIZE authors = PMMUser.get([recipe.parent_key() for recipe in recipes]) for i, recipe in enumerate(recipes): if recipe and authors[i]: recipe.author = authors[i] else: recipe.author = 'Nieznany' for post in recipes: body = post.recipe_html + u'<p><i>Proporcje składników znajdziesz na witrynie...</i></p>' feed.add(post.title, body, content_type='html', author=post.author, url=post.get_url(True), category=post.category, updated=post.updated, published=post.created) return feed.get_response()
def friend_list(request, ordering='popularne'): if ordering not in ORDERINGS.keys(): return NotFound() friends = get_cached_friends(request.user.key().name()) if friends: prev, recipes, next = PagerQuery(models.RecipeFriends) \ .filter('friends =', request.user.key().name()) \ .order(ORDERINGS[ordering]) \ .fetch(settings.PAGE_SIZE, request.args.get('b', None)) recipes = models.Recipe.get([recipe.parent_key() for recipe in recipes]) # Get authors, TODO: OPTIMIZE authors = PMMUser.get([recipe.parent_key() for recipe in recipes]) for i, recipe in enumerate(recipes): if recipe and authors[i]: recipe.author = authors[i] else: prev, recipes, next = None, [], None if request.is_xhr: template = 'przepisy/includes/friend_list.html' else: template = 'przepisy/friend_list.html' return render_to_response(template, { 'recipes': recipes, 'prev': prev, 'ordering': ordering, 'next': next, 'friends': friends.values(), })
def disabled_list(request): prev, recipes, next = PagerQuery(models.Recipe) \ .filter('disabled =', True) \ .order('-updated') \ .fetch(settings.PAGE_SIZE, request.args.get('b', None)) # Get authors, TODO: OPTIMIZE authors = PMMUser.get([recipe.parent_key() for recipe in recipes]) for i, recipe in enumerate(recipes): if recipe and authors[i]: recipe.author = authors[i] if request.is_xhr: return render_to_response('przepisy/includes/disabled_list.html', { 'recipes': recipes, 'prev': prev, 'next': next, }) else: return render_to_response('przepisy/disabled_list.html', { 'recipes': recipes, 'prev': prev, 'next': next, })
def search(request, ordering='popularne'): query = request.args.get('q', '').strip() page = request.args.get('p', 1, type=int) if page < 1: page = 1 order = request.args.get('o', '') if order != '' and order not in ORDERINGS.keys(): order = '' exact = request.args.get('e', '1') if exact not in ('0', '1'): exact = '1' advanced = request.args.get('a', '0') if advanced not in ('0', '1'): advanced = '0' recipes = models.Recipe.search(query, 201, order=ORDERINGS[order] if order else None, exact=True if exact == '1' else False) is_more = len(recipes) > 200 if is_more: recipes = recipes[:200] # Przygotuj urle if page != 1: prev = url_for('przepisy/search', q=query, p=page-1, o=order, e=exact, a=advanced) else: prev = None pages = int(math.ceil(len(recipes)/float(settings.PAGE_SIZE))) if pages != page and pages > 1: next = url_for('przepisy/search', q=query, p=page+1, o=order, e=exact, a=advanced) else: next = None # Końcowa lista przepisów, po przycięciu if pages > 1: recipes = recipes[settings.PAGE_SIZE*(page-1):settings.PAGE_SIZE*page] recipes = models.Recipe.get(recipes) recipes = [recipe for recipe in recipes if recipe is not None] # Get authors, TODO: OPTIMIZE authors = PMMUser.get([recipe.parent_key() for recipe in recipes]) for i, recipe in enumerate(recipes): if recipe and authors[i]: recipe.author = authors[i] if request.is_xhr: return render_to_response('przepisy/includes/search_list.html', { 'recipes': recipes, 'prev': prev, 'next': next, }) else: pop_recipes = models.Recipe.all() \ .filter('rec_vis =', VISIBILITY[2]) \ .filter('disabled =', False) \ .order('-views') \ .fetch(20) return render_to_response('przepisy/search.html', { 'recipes': recipes, 'prev': prev, 'order': order, 'page': page, 'exact': exact, 'advanced': advanced, 'query': query, 'squery': query, 'next': next, 'is_more': is_more, 'pop_recipes': pop_recipes, })