Exemplo n.º 1
0
def start_session_intervalled(request, *args, **kwargs):

    stt = Settings()
    #print (request.session ['stt'])
    stt.from_json(request.session['stt'])
    #print ("start_session_intervalled:::: stt.randomize = {}".format (stt.session.randomize))

    #index = int (kwargs ['index']) - 1

    start = int(kwargs['start'])
    size = int(kwargs['size'])

    project_id = request.session['project_id']
    all_voc = models.all_to_dbvoc(request.user.id, project_id)
    #    idL = all_voc.get_dated_idL (index)

    idL = all_voc.get_intervalled_idL(start, size)

    ss = session()
    if ss.start(stt, all_voc, idL):

        #eng.start ()
        #request.session ['eng']     =  eng.to_json ()
        #request.session ['all_voc'] =  all_voc.to_json ()
        request.session['ss'] = ss.to_json()

        return HttpResponseRedirect(reverse('fcards:user_guessing'))
    else:
        return HttpResponseRedirect(reverse('home'))
Exemplo n.º 2
0
def start_session_dated(request, *args, **kwargs):

    stt = Settings()
    stt.from_json(request.session['stt'])

    #index = int (kwargs ['index']) - 1

    date = kwargs['date']

    project_id = request.session['project_id']
    all_voc = models.all_to_dbvoc(request.user.id, project_id)
    #    idL = all_voc.get_dated_idL (index)
    idL = all_voc.get_dated_idL(date)

    ss = session()
    if ss.start(stt, all_voc, idL):

        #eng.start ()
        #request.session ['eng']     =  eng.to_json ()
        #request.session ['all_voc'] =  all_voc.to_json ()
        request.session['ss'] = ss.to_json()

        return HttpResponseRedirect(reverse('fcards:user_guessing'))
    else:
        return HttpResponseRedirect(reverse('home'))
Exemplo n.º 3
0
def toggle_stt_session_mode(request):

    stt = Settings()
    stt.from_json(request.session['stt'])

    if stt.session.mode == 'generation':
        stt.session.mode = 'recognition'
    else:
        stt.session.mode = 'generation'

    #print ('mydebug>>>>>> toggle_stt_session_mode : stt.session.mode = {}'.format (stt.session.mode))

#    request.session ['stt'] = stt.to_json ()
    project_id = request.session['project_id']

    dbst = FCSettings.objects.get(user_id=request.user.id,
                                  project_id=project_id)
    dbst.from_stt(stt)

    #print ('mydebug>>>>>> toggle_stt_session_mode : dbst.mode = {}'.format (dbst.mode))

    #    dbst.user_id = request.user.id
    dbst.save()

    return HttpResponseRedirect(reverse('home'))
Exemplo n.º 4
0
def end_of_session(request):

    ss = session()
    ss.from_json(request.session['ss'])
    ss.end()

    stats = dict()

    stats['kills'] = "Total words processed: {}".format(ss.stats.kills)
    stats['clean_kills'] = "First try guesses: {}".format(ss.stats.clean_kills)
    if ss.stats.alive == 0 and ss.stats.kills == ss.stats.clean_kills:
        stats['congrats'] = "*** Congrats!!! ***"
    elif ss.stats.alive > 0:
        stats['yet_to_process'] = "Total words yet to process: {}".format(
            ss.stats.alive)
        stats['yet_untouched'] = "Total words yet untouched: {}".format(
            ss.stats.untouched)

    stt = Settings()
    stt.from_json(request.session['stt'])
    if stt.session.mode == 'generation':
        other_fc_mode = 'recognition'
    else:
        other_fc_mode = 'generation'

    stats['other_fc_mode'] = other_fc_mode

    request.session['ss'] = ss.to_json()

    return render(request, 'end_of_session.html', context=stats)
Exemplo n.º 5
0
def new_project(request):

    form = forms.NewProjectForm()

    if request.method == 'POST':
        form = forms.NewProjectForm(request.POST)
        if form.is_valid():
            project = Project()
            project.name = form.cleaned_data['name']
            language_obj = Language.objects.get(
                name=form.cleaned_data['language'])
            project.language_id = language_obj.id
            project.user_id = request.user.id
            project.secret = not form.cleaned_data['allow_sharing']
            project.save()

            fc_settings = FCSettings()
            fc_settings.project_id = project.id
            fc_settings.user_id = request.user.id

            stt = Settings()
            fc_settings.from_stt(stt)
            request.session['stt'] = stt.to_json()
            fc_settings.save()

            request.session['project_id'] = project.id

            return HttpResponseRedirect(reverse("home"))

    return render(request, 'new_project.html', {'form': form})
Exemplo n.º 6
0
    def get_context_data(self, **kwargs):

        stt = Settings()
        stt.from_json(self.request.session['stt'])

        context = super().get_context_data(**kwargs)

        #all_voc = dbVoc ()
        #all_voc.from_json (self.request.session ['all_voc'])
        ss = session()
        ss.from_json(self.request.session['ss'])

        #vdbe = all_voc.get_voc_entry (str (ss.get_cur_entry_ID()))
        vdbe = get_vdbe(ss.get_cur_entry_ID())

        if stt.session.mode == 'generation':
            context[
                'answer'] = vdbe.rgt_lemma_display + ' = ' + vdbe.lft_lemma_display
            #            context ['context'] = vdbe.get_lft_ctx_str ()
            citL = vdbe.get_citL()
            ctxL = vdbe.get_ctxL()
            str_out = ctxL[0]

            for ii in range(len(citL)):
                cit_spl = [w.strip() for w in citL[ii].split('=')]
                str_out += '<span class=active_cit>' + cit_spl[0] + '</span>'
                str_out += ctxL[ii + 1]
            context['context'] = str_out
        else:
            context[
                'answer'] = vdbe.lft_lemma_display + ' = ' + vdbe.rgt_lemma_display
            #            context ['context'] =  vdbe.get_lft_ctx_str ()
            citL = vdbe.get_citL()
            ctxL = vdbe.get_ctxL()
            str_out = ctxL[0]

            for ii in range(len(citL)):
                cit_spl = [w.strip() for w in citL[ii].split('=')]
                str_out += '<span class=active_cit>' + cit_spl[0] + '</span>'
                str_out += ctxL[ii + 1]
            context['context'] = str_out

        context[
            'answer'] = '<span class=lemma>' + context['answer'] + '</span>'

        #            answer_str = vdbe.lft_lemma + ' = ' + vdbe.rgt_lemma + 2*'\n' + vdbe.get_lft_ctx_str ()

        context['session_size'] = len(ss)
        context['rhn'] = ss.get_cur_entry_rhn()

        return context
Exemplo n.º 7
0
def fw_move_to_start(request):

    stt = Settings()
    stt.from_json(request.session['stt'])
    project_id = request.session['project_id']
    all_voc = fcards.models.all_to_dbvoc(request.user.id, project_id)

    #print ('mydebug>>>>>> start_new_floating_window : len (all_voc)'.format (len (all_voc)))

    dbfwindex = fcards.models.FloatingWindowIndex.objects.get(
        project_id=project_id, user_id=request.user.id)

    dbfwindex.index = 0
    dbfwindex.save()

    return HttpResponseRedirect(reverse('home'))
Exemplo n.º 8
0
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)

        if form.is_valid():

            uploaded_file = request.FILES['file']
            utf8_str = uploaded_file.read().decode('utf-8')

            stt = Settings()
            stt.from_json(request.session['stt'])

            new_db_voc = dbVoc()
            print('time: {}'.format(time.asctime(time.localtime(time.time()))))
            new_db_voc.from_text(utf8_str, stt.extract_sentences)
            print('time: {}'.format(time.asctime(time.localtime(time.time()))))

            #print ('mydebug>>>> upload_file : file: {}'.format (form.cleaned_data ['file']))

            if 'save_file' in form.cleaned_data:
                save = form.cleaned_data['save_file']
            else:
                save = False

            project_id = request.session['project_id']
            if save:
                file_name = str(form.cleaned_data['file'])
                print('mydebug>>>> upload_file: saving ' + file_name)
                save_file(utf8_str, file_name, request.user.id, project_id)
                make_src_current(file_name, request.user.id, project_id)
            #action = request.POST ['action']
            action = 'overwrite'

            db_voc2voc_entry_db(action, new_db_voc, request.user.id,
                                project_id)

            print('time: {}'.format(time.asctime(time.localtime(time.time()))))

        else:
            print("Form invalid")

        return HttpResponseRedirect(reverse('home'))
    else:
        form = UploadFileForm()
        return render(request, 'upload_file.html', {'form': form})
Exemplo n.º 9
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        stt = Settings()
        stt.from_json(self.request.session['stt'])

        #all_voc = dbVoc ()
        #all_voc.from_json (self.request.session ['all_voc'])
        ss = session()
        ss.from_json(self.request.session['ss'])

        #print ("mydebug >>> views.UserGuessing eng.running : {}".format (eng.running))

        #vdbe = all_voc.get_voc_entry (str (ss.get_cur_entry_ID()))
        vdbe = get_vdbe(ss.get_cur_entry_ID())
        #print ("mydebug >>> views.UserGuessing cur_entry_ind = {}".format (ss.chunk.cur_entry_ind))
        #print ("mydebug >>> views.UserGuessing cur_entry_ID = {}".format (ss.get_cur_entry_ID()))

        if stt.session.mode == 'generation':
            context['question'] = vdbe.rgt_lemma_display
            #            context ['context'] = vdbe.get_rgt_ctx_str ()
            citL = vdbe.get_citL()
            ctxL = vdbe.get_ctxL()
            str_out = ctxL[0]

            for ii in range(len(citL)):
                cit_spl = [w.strip() for w in citL[ii].split('=')]

                str_out += '<span class=active_cit>' + cit_spl[1] + '</span>'
                str_out += ctxL[ii + 1]

    #        context ['question'] += '<br>' + str_out
            context['context'] = str_out

        else:
            context['question'] = vdbe.lft_lemma_display

        context['question'] = '<span class=lemma>' + context[
            'question'] + '</span>'

        context['session_size'] = len(ss)
        context['rhn'] = ss.get_cur_entry_rhn()

        return context
Exemplo n.º 10
0
    def to_stt(self):
        stt = Settings()
        stt.db_id = self.id
        #    stt.chunk.size = self.chunk_size
        stt.session.mode = self.mode
        stt.session.rhn_punitive = self.punitive_rhn
        stt.session.rhn_initial = self.initial_rhn
        stt.session.randomize = self.randomize
        #print ("fcards:models:FCSettings.to_stt :: stt.session.randomize = {}".format (stt.session.randomize))

        stt.voc.frequency = self.voc_freq

        stt.lessons.lesson = self.fw_lesson_size
        stt.lessons.window = self.fw_review_lesson_cnt
        stt.extract_sentences = self.extract_sentences

        stt.lessons.rand_old = self.lessons_rand_old

        return stt
Exemplo n.º 11
0
def fw_move_forward(request):

    stt = Settings()
    stt.from_json(request.session['stt'])
    project_id = request.session['project_id']
    all_voc = fcards.models.all_to_dbvoc(request.user.id, project_id)

    #print ('mydebug>>>>>> start_new_floating_window : len (all_voc)'.format (len (all_voc)))

    dbfwindex = fcards.models.FloatingWindowIndex.objects.get(
        project_id=project_id, user_id=request.user.id)
    fw_index = dbfwindex.index

    floating_window = FloatingWindow(stt, len(all_voc), fw_index)
    if not floating_window.is_at_end():
        fw_index = floating_window.cur + 1

    dbfwindex.index = fw_index
    dbfwindex.save()

    return HttpResponseRedirect(reverse('home'))
Exemplo n.º 12
0
def edit_settings(request):

    stt = Settings()
    stt.from_json(request.session['stt'])
    project_id = request.session['project_id']

    form = forms.SettingsForm(stt)

    if request.method == 'POST':
        form = forms.SettingsForm(stt, request.POST)
        if form.is_valid():
            form.to_stt(stt)
            dbst = FCSettings.objects.get(user_id=request.user.id,
                                          project_id=project_id)
            dbst.from_stt(stt)
            #    dbst.user_id = request.user.id
            dbst.save()
            request.session['stt'] = stt.to_json()

            return HttpResponseRedirect(reverse("home"))

    return render(request, 'edit_settings.html', {'form': form})
Exemplo n.º 13
0
def restart_session_in_other_mode(request):

    stt = Settings()
    stt.from_json(request.session['stt'])
    if stt.session.mode == 'generation':
        stt.session.mode = 'recognition'
    else:
        stt.session.mode = 'generation'
    request.session['stt'] = stt.to_json()

    project_id = request.session['project_id']

    dbst = FCSettings.objects.filter(user_id=request.user.id).get(
        project_id=project_id)
    dbst.from_stt(stt)
    dbst.save()

    ss = session()
    ss.from_json(request.session['ss'])
    ss.restart()
    request.session['ss'] = ss.to_json()

    return HttpResponseRedirect(reverse('fcards:user_guessing'))
Exemplo n.º 14
0
def start_session_all(request):

    stt = Settings()
    stt.from_json(request.session['stt'])

    project_id = request.session['project_id']
    all_voc = models.all_to_dbvoc(request.user.id, project_id)

    idL = all_voc.get_idL()

    ss = session()
    if ss.start(stt, all_voc, idL):

        #eng = engine ()
        #eng.start ()

        #request.session ['eng']     =  eng.to_json ()

        #request.session ['all_voc'] =  all_voc.to_json ()
        request.session['ss'] = ss.to_json()

        return HttpResponseRedirect(reverse('fcards:user_guessing'))
    else:
        return HttpResponseRedirect(reverse('home'))
Exemplo n.º 15
0
def start_session_randold(request):

    stt = Settings()
    stt.from_json(request.session['stt'])

    project_id = request.session['project_id']
    all_voc = models.all_to_dbvoc(request.user.id, project_id)

    idL = all_voc.get_dated_idL_given_date_ind((0, -2))

    ss = session()
    """
    print ('mydebug>>>> start_session_randold : calling ss.start')
    print ()
    print (idL)
    print ()
    """
    if ss.start(stt, all_voc, idL, stt.lessons.rand_old):

        request.session['ss'] = ss.to_json()

        return HttpResponseRedirect(reverse('fcards:user_guessing'))
    else:
        return HttpResponseRedirect(reverse('home'))
Exemplo n.º 16
0
def edit_btn_on_click(request):

    stt = Settings()
    stt.from_json(request.session['stt'])
    ss = session()
    ss.from_json(request.session['ss'])
    project_id = request.session['project_id']
    project_obj = Project.objects.filter(user_id=request.user.id).get(
        id=project_id)
    language_id = project_obj.language_id

    #all_voc = dbVoc ()
    #all_voc.from_json (request.session ['all_voc'])

    #vdbe = all_voc.get_voc_entry (str (ss.get_cur_entry_ID ()))
    vdbe = get_vdbe(ss.get_cur_entry_ID())

    form = forms.EditVocEntryForm(vdbe)

    if request.method == 'POST':
        form = forms.EditVocEntryForm(vdbe, request.POST)
        if form.is_valid():
            form.to_vdbe(vdbe)
            ve = models.VocEntry()
            ve.from_vdbe(vdbe)
            ve.id = vdbe.ID
            ve.user_id = request.user.id
            ve.project_id = project_id
            ve.language_id = language_id

            #        request.session ['all_voc'] =  all_voc.to_json ()

            ve.save()
            return HttpResponseRedirect(reverse("fcards:awaiting_approval"))

    return render(request, 'edit_entry.html', {'form': form})
Exemplo n.º 17
0
def work_with_text(request, *args, **kwargs):

    file_name = kwargs['file_name']
    if len(file_name) > 0:
        project_id = request.session['project_id']
        raw_file_name = file_name + '_' + str(request.user.id)
        new_raw_file_name = file_name + '_' + str(
            request.user.id) + '_' + str(project_id)

        if os.path.isfile(new_raw_file_name):
            fin = codecs.open(new_raw_file_name, 'r', 'utf-8')
            txt = fin.read()
        else:
            try:

                #print ("mydebug>>> work_with_text: last_file : {}".format (last_file))
                fin = codecs.open(raw_file_name, 'r', 'utf-8')
                txt = fin.read()

            except:
                txt = ''
                file_name = ''
    else:
        txt = ''
    form = forms.WorkWithTextForm(txt, file_name)

    if request.method == 'POST':
        form = forms.WorkWithTextForm('', '', request.POST)
        if 'submit_cits' in form.data:
            if form.is_valid():

                txt = form.cleaned_data['text']
                file_name = form.cleaned_data['file_name']
                save_the_file = form.cleaned_data['save_file']

                stt = Settings()
                stt.from_json(request.session['stt'])

                new_db_voc = dbVoc()
                new_db_voc.from_text(txt, stt.extract_sentences)

                project_id = request.session['project_id']
                if save_the_file:
                    save_file(txt, file_name, request.user.id, project_id)
                    make_src_current(file_name, request.user.id, project_id)
                else:
                    make_current_non_current(request.user.id, project_id)

                #action = request.POST ['action']
                action = 'overwrite'
                db_voc2voc_entry_db(action, new_db_voc, request.user.id,
                                    project_id)
            else:
                print("Form invalid")

            return HttpResponseRedirect(reverse("home"))
        else:

            txt = request.POST['text']
            #print ('txt: ' + txt)
            new_file_name = request.POST['file_name'].strip()
            #print ('new_file_name: ' + new_file_name)

            if len(new_file_name) > 0:  # need more thorough check here
                project_id = request.session['project_id']

                save_file(txt, new_file_name, request.user.id, project_id)
                make_src_non_current(new_file_name, request.user.id,
                                     project_id)

                #need to check here is new_file_name is valid

                return HttpResponseRedirect(
                    reverse("text:work_with_text",
                            kwargs={'file_name': new_file_name}))

    return render(request, 'work_with_text.html', {
        'form': form,
        'file_name': file_name
    })
Exemplo n.º 18
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        #Project
        project_selected = False

        if self.request.user.id != None:

            if 'project_id' in self.request.session:
                project_id = self.request.session['project_id']
                try:
                    project_obj = Project.objects.filter(
                        user_id=self.request.user.id).get(id=project_id)
                except Project.DoesNotExist:
                    del (self.request.session['project_id'])
                    print(
                        "mydebug >>> [E] : project of specified id doesn't exit. Cleaning up"
                    )

            if 'project_id' in self.request.session:

                project_name = project_obj.name
                language_obj = Language.objects.get(id=project_obj.language_id)
                language_name = language_obj.name
                language_id = language_obj.id

                context['project'] = project_name
                context['language'] = language_name
                project_selected = True

            context['projects'] = Project.objects.filter(
                user_id=self.request.user.id)
            print("views:index: projects found: ")
            for pr in context['projects']:
                print('\t{}'.format(pr.name))
        #Settings
        if project_selected:
            print("views.index : project selected. Name: {}".format(
                project_name))

            stt = Settings()

            print("mydebug >>> HomePage.get_context_data user_id : {}".format(
                self.request.user.id))

            if self.request.user.id != None:
                # Work with source files menu

                files = MyTextFilesModel.objects.filter(
                    user_id=self.request.user.id, project_id=project_id)
                context['txt_files'] = files
                try:
                    current_file = MyTextFilesModel.objects.get(
                        user_id=self.request.user.id,
                        project_id=project_id,
                        current=True)
                    #print ("django_fc.views. Found current file")
                    context['current_file'] = current_file.file_name
                except MyTextFilesModel.DoesNotExist:
                    pass

                #settings

                try:
                    dbst = FCSettings.objects.get(user_id=self.request.user.id,
                                                  project_id=project_id)
                    #print ("mydebug >>> HomePage.get_context_data dbst.mode = {}".format (dbst.mode))
                    stt = dbst.to_stt()
                    #print ("mydebug >>> HomePage.get_context_data created stt. stt.mode = {}".format (stt.session.mode))

                except FCSettings.DoesNotExist:
                    dbst = FCSettings()
                    dbst.from_stt(stt)
                    dbst.project_id = project_id
                    print(
                        "mydebug >>> HomePage.get_context_data created FCSettings entry."
                    )
                    dbst.user_id = self.request.user.id
                    dbst.save()

                except FCSettings.MultipleObjectsReturned:
                    print(
                        "mydebug >>> HomePage.get_context_data deleting settings from db"
                    )
                    FCSettings.objects.filter(user_id=self.request.user.id,
                                              project_id=project_id).delete()

                    dbst = FCSettings()
                    dbst.from_stt(stt)
                    print(
                        "mydebug >>> HomePage.get_context_data created FCSettings entry."
                    )
                    dbst.user_id = self.request.user.id
                    dbst.project_id = project_id
                    dbst.save()

                self.request.session['stt'] = stt.to_json()

                #print ("mydebug >>> HomePage.get_context_data stt.session.randomize = {}".format (stt.session.randomize))

                #sessions

                all_voc = fcards.models.all_to_dbvoc(self.request.user.id,
                                                     project_id)
                date_count = all_voc.get_date_cnt()

                context['date_count'] = date_count

                str_dates = all_voc.get_dateL()

                ecounts = [
                    len(all_voc.get_dated_idL(date)) for date in str_dates
                ]

                debug_dates = True
                if debug_dates:
                    print(
                        "mydebug>>> views.HomePage.get_context_data. dates found. total {}"
                        .format(date_count))
                    for ii in range(len(str_dates)):
                        print("entry_count [{}] = {}".format(
                            str_dates[ii], ecounts[ii]))
                    print()

                context['stt_session_mode'] = stt.session.mode

                entry_count = all_voc.get_size()

                context['entry_count'] = entry_count
                context['random_entry_count'] = stt.lessons.rand_old

                str_dates.reverse()
                ecounts.reverse()

                if len(str_dates) > 0:
                    if not (len(str_dates) == 1 and str_dates[0] == 'NO_DATE'):
                        context['latest_date'] = str_dates[0]
                        context['latest_date_ecount'] = ecounts[0]

                if len(str_dates) > 1:
                    context['prev_date'] = str_dates[1]
                    context['prev_date_ecount'] = ecounts[1]

                class date_thing():
                    def __init__(self, date, ecount):
                        self.str_date = date
                        self.ecount = ecount

                if len(str_dates) > 2:

                    context['older_entry_count'] = entry_count - ecounts[
                        0] - ecounts[1]

                    #context ['date_list'] = str_dates [2:]
                    #context ['ecount_list'] = ecounts [2:]
                    date_things = []
                    for ii in range(2, len(str_dates)):
                        date_things.append(
                            date_thing(str_dates[ii], ecounts[ii]))

                    context['date_thing_list'] = date_things

            #floating window
                all_voc = fcards.models.all_to_dbvoc(self.request.user.id,
                                                     project_id)
                try:
                    dbfwindex = fcards.models.FloatingWindowIndex.objects.get(
                        project_id=project_id, user_id=self.request.user.id)
                    fw_index = dbfwindex.index

                except fcards.models.FloatingWindowIndex.DoesNotExist:
                    fw_index = 0
                    dbfwindex = fcards.models.FloatingWindowIndex()
                    dbfwindex.index = fw_index
                    dbfwindex.project_id = project_id
                    dbfwindex.user_id = self.request.user.id
                    dbfwindex.save()

                if entry_count > 0:

                    fw = FloatingWindow(stt, entry_count, fw_index)

                    context['session_state'] = 'Step {} of {}'.format(
                        fw_index + 1, fw.get_total_step_cnt())

                    context['new_lesson'] = 'Lesson {} of {}'.format(
                        fw_index + 1, fw.get_lesson_cnt())
                    context['is_there_new'] = fw.is_there_new()
                    context['is_there_prev'] = fw.is_there_prev()
                    context['is_there_window'] = fw.is_there_window()

                    context['window_start'] = fw.get_cur_window().start
                    context['window_size'] = fw.get_cur_window().size
                    context['is_there_window'] = fw.is_there_window()

                    context['fw_older_entry_count'] = fw.get_older().size

                    context['new_start'] = fw.get_cur_new().start
                    context['new_size'] = fw.get_cur_new().size

                    context['prev_start'] = fw.get_cur_prev().start
                    context['prev_size'] = fw.get_cur_prev().size

                    context['not_at_start'] = not fw.is_at_start()
                    context['not_at_end'] = not fw.is_at_end()

        return context