예제 #1
0
파일: utils.py 프로젝트: gluwer/przepisymm
def update_friends(start_key=None):
  while (True):
    try:
      rec_friends = RecipeFriends.all()
      if start_key:
        rec_friends.filter('__key__ >', start_key)
      rec_friends.order('__key__')
      rec_friends = rec_friends.fetch(20)
      
      if not rec_friends:
        break
      else:
        start_key = None

      for rec_friend in rec_friends:
        rec_friend.friends = get_cached_friends(rec_friend.parent_key().parent().name()).keys()
        rec_friend.put()
        start_key = rec_friend.key()

    except (DeadlineExceededError, Timeout), e:
      deferred_lib.defer(update_friends, start_key)
    except (CapabilityDisabledError), e:
      break # End if there is no more time...
예제 #2
0
파일: views.py 프로젝트: gluwer/przepisymm
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(),
  })
  
예제 #3
0
def recipe_update(recipe_key, type, tag_diff=None, category_diff=None, rec_vis_diff=None):
  recipe = models.Recipe.get(recipe_key)
  
  if not recipe:
    raise deferred.PermanentTaskFailure('Recipe key %s no longer exist!' % recipe_key)
  
  if type in ('add', 'enable') and not recipe.disabled:
    def ctx():
      category = models.Category.get_by_key_name(models.CATEGORIES_SV[recipe.category])
      if category:
        category.counter += 1
        category.put()
    
    # dodaj etykiety
    # + AC etykiet
    models.Tag.update_elements(recipe.tags)

    # uaktualnij kategorie
    db.run_in_transaction(ctx)

    # dodaj indeks
    if recipe.rec_vis == VISIBILITY[2]:
      recipe.update_index()
    
    if recipe.rec_vis in (VISIBILITY[1], VISIBILITY[2]):
      recipe.update_friends_idx(get_cached_friends(recipe.parent_key().name()).keys())
  
  if type == 'update' and not recipe.disabled:
    if tag_diff:
      models.Tag.update_elements(*tag_diff)
    
    if rec_vis_diff:
      if recipe.rec_vis == VISIBILITY[2]:
        recipe.update_index()
      elif recipe.rec_vis != VISIBILITY[2] and rec_vis_diff[0] == VISIBILITY[2]:
        recipe.remove_index()
  
      if recipe.rec_vis in (VISIBILITY[1], VISIBILITY[2]):
        recipe.update_friends_idx(get_cached_friends(recipe.parent_key().name()).keys())
  
    if category_diff:
      def ctx1():
        category = models.Category.get_by_key_name(models.CATEGORIES_SV[category_diff[0]])
        if category:
          category.counter -= 1
          if category.counter < 0:
            category.counter = 0          
          category.put()
      db.run_in_transaction(ctx1)
      
      def ctx2():
        category = models.Category.get_by_key_name(models.CATEGORIES_SV[category_diff[1]])
        if category:
          category.counter += 1
          category.put()
      db.run_in_transaction(ctx2)
  
  if type == 'delete' or (type in ('disable', 'update') and recipe.disabled):
    def ctx():
      category = models.Category.get_by_key_name(models.CATEGORIES_SV[recipe.category])
      if category:
        category.counter -= 1
        if category.counter < 0:
          category.counter = 0
        category.put()    
    
    # usuń etykiety
    models.Tag.update_elements(removed=recipe.tags)

    # uaktualnij kategorie
    db.run_in_transaction(ctx)

    # usuń indeks
    if recipe.rec_vis == VISIBILITY[2]:
      recipe.remove_index()

    if recipe.rec_vis in (VISIBILITY[1], VISIBILITY[2]):
      recipe.remove_friends_idx()
    
    # usuń przepis, jeśli usuwanie
    if type == 'delete':
      recipe.delete()