def confirm(request): form = CorpusForm(request.POST, request.FILES) # so it passes validation form.fields["upload_path"].initial = True if form.is_valid(): c = Corpus(user = request.user, date_added = timezone.now()) c.name = form.cleaned_data["name"] c.description = form.cleaned_data["description"] c.process_steps = form.cleaned_data["process_steps"] c.metadata_url = form.cleaned_data["metadata_url"] lang_id = form.cleaned_data["lang_select"] if(lang_id == "new"): new_lang = form.cleaned_data["new_lang"] c.lang = Lang.objects.create(name=new_lang) else: c.lang = Lang.objects.get(id=lang_id) #To take value of the label field from form to the database label field c.label = c.name + " " + str(c.lang) + " " + str(c.user) + " " + form.cleaned_data["label"] path_type = form.cleaned_data["path_type"] if (path_type == "file_upload"): (c.location, c.file_name) = files.save_tmp_file(c.name) else: c.location = form.cleaned_data["location"].strip("/") c.file_name = form.cleaned_data["file_name"].strip("/") # To find out the size of the corpora #name2 = c.name + ".txt" #c.size = c.current_size() #To find out the word count of the corpora '''count = 0 with open(files.corpus_path(c),'r') as f: for line in f: for word in line.split(): count+=1 c.count = count''' #c.count = c.wc() c.save() return HttpResponseRedirect(reverse("corpora:corpus", kwargs={"corpus_id" : c.id})) # we should ideally never get here, so just redirect to the main index # in case something unexpected went wrong else: return HttpResponseRedirect(reverse("corpora:index"))
def index(request): corpora = Corpus.objects.all() langs = Lang.objects.all() context = {'my_corpora' : Corpus.objects.filter(user=request.user), 'other_corpora' : Corpus.objects.exclude(user=request.user), 'langs' : langs} if request.method == 'GET': if 'q' in request.GET : query = request.GET['q'] context['my_corpora'] = context['my_corpora'].filter(label__contains=query) context['other_corpora'] = context['other_corpora'].filter(label__contains=query) if request.method == "POST": form = CorpusForm(request.POST, request.FILES) if form.is_valid(): path_type = form.cleaned_data["path_type"] if (path_type == "file_upload"): name = form.cleaned_data["name"] file = form.cleaned_data["upload_path"] path = files.write_tmp_file(file, name) else: location = form.cleaned_data["location"].strip("/") file_name = form.cleaned_data["file_name"].strip("/") path = os.path.join(location, file_name) sample = files.corpus_sample(path, 15) size = files.file_size(path) wc = files.wc(path) context = {'form' : form, 'sample' : sample, 'size' : size, 'wc' : wc} return render(request, 'corpora/confirm_upload.html', context) else: context["form"] = form else: context["form"] = CorpusForm() return render(request, 'corpora/index.html', context)