예제 #1
0
    def __finalize_recipe(self, recipe, is_new, indexer):
        """ Final steps in saving a new or updated recipe. Returns recipe
        or redirects. """
        if is_new:
            manager = user_manager.UserManager(self.db)
            recipe.author = manager.current_user()

        if not recipe.title:
            hint_text = _('Title must not be empty.')
            self.hints.append(hint.Hint(hint_text))
        else:
            # First check if the recipe already exists and is new.
            new_exists = is_new and \
                recipe_entity.Recipe.title_exists(self.db, recipe.title)

            # Save the recipe.
            recipe.save(self.db)

            # Update search index.
            scheme = indexer.scheme()
            writer = indexer.open_index(scheme)
            indexer.fill_index(writer, [recipe])
            indexer.close_index()

            if new_exists:
                type = self.HINT_NEW_EXISTS
            elif is_new:
                type = self.HINT_NEW
            else:
                type = self.HINT_EDIT
            self.set_cookie(self.HINT_COOKIE, type)
            self.set_cookie(self.HINT_NAME, recipe.title)
            redirect(url.Url.from_path(['manage', 'recipe', str(recipe.id)]))

        return recipe
예제 #2
0
def login(db):
    """ Login page. """
    language, is_admin = validate_user_and_language(db, False)

    manager = user_manager.UserManager(db)
    hints = manager.login(language, PW_HASH_ITERATIONS, ADMIN_USER)
    return dict(hints=hints, language=language, languages=LANGUAGES,
                is_admin=is_admin)
예제 #3
0
def validate_user_and_language(db, enable_users):
    """ Validate the user and get the current language. """
    is_admin = False
    if enable_users:
        manager = user_manager.UserManager(db)
        manager.validate_login(PW_HASH_ITERATIONS, ADMIN_USER)
        is_admin = (manager.current_user() and
                    manager.current_user().name == ADMIN_USER)
    language = translator.Translator.current_language(LANGUAGE)
    return language, is_admin
예제 #4
0
def manage_users(db):
    """ User managing page or personal profile. """
    language, is_admin = validate_user_and_language(db, ENABLE_USERS)
    categories = category.Category.find_all(db)

    manager = user_manager.UserManager(db)
    users = manager.action(language, PW_HASH_ITERATIONS, ADMIN_USER)
    hints = manager.hints
    return dict(categories=categories, hints=hints, users=users,
                is_admin=is_admin, language=language, languages=LANGUAGES,
                enable_users=ENABLE_USERS)
예제 #5
0
def logout(db):
    """ Login page. """
    manager = user_manager.UserManager(db)
    manager.logout()