Ejemplo n.º 1
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
        # return redirect(url_for('addWord'))

    form = LoginForm()
    if form.validate_on_submit():
        name = form.name.data
        password = form.password.data
        remember = form.remember.data

        try:
            smartDict.loginUser(name, password, remember)
            # flash('Authorization successful.')

            next_page = request.args.get('next')
            if not next_page or url_parse(next_page).netloc != '':
                next_page = url_for('index')
                # return redirect(url_for('addWord'))
            return redirect(next_page)

        except InvalidUsernameOrPasswordError:
            flash('Invalid name or password.', consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return render_template('login.html', form=form, active=consts.active.LOGIN)
Ejemplo n.º 2
0
def importWords():
    form = ImportForm()
    form.dictionary.choices = functions.choicesForSelect(smartDict)
    addedWords = list()

    if form.validate_on_submit():
        dictionary = form.dictionary.data
        words = form.words.data
        updateTime = time()

        try:
            addedWords = smartDict.importWords(dictionary, words, updateTime)
            if addedWords:
                flash('Next words were added:', consts.SUCCESS)
                for word in addedWords:
                    flash('{0} - {1}'.format(word[0], word[1]), consts.SUCCESS)
            else:
                flash('Words were not added.', consts.ERROR)
            return redirect(url_for('importWords'))
        except DictionaryNotExistError:
            flash(consts.DICT_NOT_EXIST.format(dictionary), consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return render_template('import.html',
                           form=form,
                           addedWords=addedWords,
                           active=consts.active.IMPORT)
Ejemplo n.º 3
0
def deleteWord():
    form = DeleteWordForm()
    next_page = request.args.get('next')

    if not next_page or url_parse(next_page).netloc != '':
        next_page = url_for('dictionaries')

    if form.validate_on_submit():
        dictionary = form.dictionary.data
        original = form.original.data

        try:
            smartDict.deleteWord(dictionary, original)
            flash(consts.WORD_DEL.format(original), consts.SUCCESS)
            return redirect(url_for('editDictionary', name=dictionary))

        except DictionaryNotExistError:
            flash(consts.DICT_NOT_EXIST.format(dictionary), consts.ERROR)
        except WordNotExistError:
            flash(consts.WORD_NOT_EXIST.format(original), consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return redirect(next_page)
Ejemplo n.º 4
0
def changeWord():
    form = ChangeWordForm()
    next_page = request.args.get('next')

    if not next_page or url_parse(next_page).netloc != '':
        next_page = url_for('dictionaries')

    if form.validate_on_submit():
        old = form.old.data
        original = form.original.data
        translate = form.translate.data
        transcription = form.transcription.data
        dictionary = form.dictionary.data
        updateTime = time()

        try:
            smartDict.changeWord(dictionary, old, original, translate,
                                 transcription, updateTime)
            flash(consts.WORD_CHANGED.format(old), consts.SUCCESS)
            # return redirect(url_for('editDictionary', name=dictionary))
            return redirect(next_page)

        except DictionaryNotExistError:
            flash(consts.DICT_NOT_EXIST.format(dictionary), consts.ERROR)
        except WordNotExistError:
            flash(consts.WORD_NOT_EXIST.format(old), consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return redirect(next_page)
Ejemplo n.º 5
0
def search():
    form = SearchForm()
    dicts = dict()

    if form.is_submitted():
        word = form.word.data
        dicts = smartDict.search(word)

    else:  # form not valid
        functions.flashErrors(form)

    return render_template('search.html', form=form, dicts=dicts)
Ejemplo n.º 6
0
def startTest():
    do = request.args.get('do')
    forms = dict()
    addedWords = dict()
    forms['startTest'] = TestStartForm()
    forms['startTest'].dictionary.choices = list()

    if smartDict.isTestInit():
        forms['startTest'].dictionary.choices.append(
            (consts.ADDED_WORDS, consts.ADDED_WORDS_S))
        addedWords = smartDict.addedWords()

    forms['startTest'].dictionary.choices += functions.choicesForSelect(
        smartDict, addAll=True)

    forms['startTest'].period.choices = [
        (consts.period.ALL_I, consts.period.ALL_S),
        (consts.period.LAST_DAY_I, consts.period.LAST_DAY_S),
        (consts.period.LAST_WEEK_I, consts.period.LAST_WEEK_S),
        (consts.period.LAST_MONTH_I, consts.period.LAST_MONTH_S)
    ]

    mistakes = smartDict.mistakes()
    if mistakes:
        forms['correctMistakes'] = CorrectMistakesForm()

    if do == 'start':
        if forms['startTest'].validate_on_submit():
            try:
                dictionary = forms['startTest'].dictionary.data
                period = forms['startTest'].period.data
                smartDict.testInit(dictionary, period)
                return redirect(url_for('test'))

            except DictionaryNotExistError:
                flash(consts.DICT_NOT_EXIST.format(dictionary), consts.ERROR)
        else:  # form not valid
            functions.flashErrors(forms['startTest'])

    if do == 'mistakes':
        smartDict.testInit(consts.MISTAKE_DICT)
        return redirect(url_for('test'))

    return render_template('start-test.html',
                           forms=forms,
                           addedWords=addedWords,
                           mistakes=mistakes,
                           active=consts.active.TEST)
Ejemplo n.º 7
0
def deleteDictionary():
    form = DeleteDictionaryForm()

    if form.validate_on_submit():
        name = form.name.data

        try:
            smartDict.deleteDictionary(name)
            flash(consts.DICT_DEL.format(name), consts.SUCCESS)
            return redirect(url_for('dictionaries'))

        except DictionaryNotExistError:
            flash(consts.DICT_NOT_EXIST.format(name), consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return redirect(request.referrer)
Ejemplo n.º 8
0
def addDictionary():
    form = AddDictionaryForm()

    if form.validate_on_submit():
        name = form.name.data
        description = form.description.data

        try:
            smartDict.addDictionary(name, description)
            flash(consts.DICT_ADDED.format(name), consts.SUCCESS)
            return redirect(url_for('viewDictionary', name=name))

        except DictionaryAlreadyExistError:
            flash(consts.DICT_EXIST.format(name), consts.ERROR, consts.ERROR)
    else:  # form not valid
        functions.flashErrors(form)

    return render_template('add-dictionary.html',
                           form=form,
                           active=consts.active.DICT)
Ejemplo n.º 9
0
def changeDictionary():
    form = ChangeDictionaryForm()

    if form.validate_on_submit():
        name = form.name.data
        description = form.description.data
        old = form.old.data

        try:
            smartDict.changeDictionary(old, name, description)
            flash(consts.DICT_CHANGED.format(name), consts.SUCCESS)
            return redirect(url_for('editDictionary', name=name))

        except DictionaryAlreadyExistError:
            flash(consts.DICT_EXIST.format(name), consts.ERROR)
        except DictionaryNotExistError:
            flash(consts.DICT_NOT_EXIST.format(old), consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return redirect(request.referrer)
Ejemplo n.º 10
0
def addWord(wrapped=False):
    """
    wrapped arg is used for save flashed messages
    """
    form = AddWordSelectForm()
    form.dictionary.choices = functions.choicesForSelect(smartDict)
    total = smartDict.totalWords()

    if form.validate_on_submit():
        dictionary = form.dictionary.data
        original = form.original.data
        translate = form.translate.data
        transcription = form.transcription.data
        replace = form.replace.data
        createTime = time()

        try:
            smartDict.addWord(dictionary, original, translate, transcription,
                              createTime, replace)
            flash(
                consts.WORD_ADDED.format(
                    original,
                    functions.generateLink(
                        url_for('viewDictionary', name=dictionary),
                        dictionary)), consts.SUCCESS)

        except DictionaryNotExistError:
            flash(consts.DICT_NOT_EXIST.format(dictionary), consts.ERROR)

        return wrapped if wrapped else redirect(url_for('addWord'))

    else:  # form not valid
        functions.flashErrors(form)

    return wrapped if wrapped\
        else render_template('add-word.html', form=form,
                             total=total, active=consts.active.WORD)
Ejemplo n.º 11
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))

    form = RegisterForm()
    if form.validate_on_submit():
        name = form.name.data
        password = form.password.data

        try:
            smartDict.registerUser(name, password)
            # flash('Registration successful.')
            # return redirect(url_for('login'))
            return redirect(url_for('index'))

        except UserAlreadyExistError:
            flash('User already exists.', consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    return render_template('register.html',
                           form=form,
                           active=consts.active.REGISTER)
Ejemplo n.º 12
0
def test():
    forms = dict()
    forms['testNext'] = TestNextForm()
    # if there are errors, last question will remains
    question = dict()
    question['question'] = forms['testNext'].question.data

    if smartDict.isTestInit():
        if forms['testNext'].validate_on_submit():
            answer = forms['testNext'].answer.data
            smartDict.addAnswer(question['question'], answer)
        else:
            functions.flashErrors(forms['testNext'])

        if not forms['testNext'].errors:
            question = smartDict.nextQuestion()

            if not question:
                result = smartDict.testResult()

                forms['correctMistakes'] = CorrectMistakesForm(
                ) if smartDict.mistakes() else None

                return render_template('finish-test.html',
                                       result=result,
                                       form=forms['correctMistakes'],
                                       active=consts.active.TEST)
    else:
        flash(
            'Please, <a href="{0}">choice the dictionary</a> \
            to pass the test.'.format(url_for('startTest')), consts.ERROR)

    return render_template('test.html',
                           form=forms['testNext'],
                           question=question,
                           active=consts.active.TEST)
Ejemplo n.º 13
0
def addQuestion():
    form = AddQuestionForm()
    next_page = request.args.get('next')

    if not next_page or url_parse(next_page).netloc != '':
        next_page = url_for('dictionaries')

    if form.validate_on_submit():
        question = form.question.data
        answer = form.answer.data
        try:
            smartDict.addQuestion(question, answer)
            flash('Word {0} - {1} was added to Test.'.format(question, answer),
                  consts.SUCCESS)
        except QuestionAlreadyAddedError:
            flash(
                'Word {0} - {1} have already added to Test.'.format(
                    question, answer), consts.ERROR)

    else:  # form not valid
        functions.flashErrors(form)

    # return redirect(request.referrer)
    return redirect(next_page)