Commit f45c7e96 authored by c24b's avatar c24b

Check file format error if wrong extension file

parent 7d4586fa
......@@ -23,9 +23,15 @@ def download(url, name=''):
basedir = DOWNLOAD_DIRECTORY,
)
def check_format(corpus_type, name):
#~ if True:
acc_formats = RESOURCETYPES[corpus_type]["accepted_formats"]
if name.split(".")[-1].lower() not in acc_formats:
raise TypeError('Uncorrect format of file. File must be a %s file' %" or ".join(acc_formats))
def upload(uploaded):
print(repr(uploaded))
if uploaded.size > UPLOAD_LIMIT:
raise IOError('Uploaded file is bigger than allowed: %d > %d' % (
uploaded.size,
......
......@@ -2,6 +2,7 @@ from gargantext.util.http import *
from gargantext.util.db import *
from gargantext.util.db_cache import cache
from gargantext.util.files import upload
from gargantext.util.files import check_format
from gargantext.models import *
from gargantext.constants import *
from gargantext.util.scheduling import scheduled
......@@ -58,6 +59,7 @@ def overview(request):
class NewCorpusForm(forms.Form):
'''c24b: je dirai que je ne sais pas quand il sert ce truc'''
type = forms.ChoiceField(
choices = enumerate(resource_type['name'] for resource_type in RESOURCETYPES),
widget = forms.Select(attrs={ 'onchange' :'CustomForSelect( $("option:selected", this).text() );'})
......@@ -89,28 +91,62 @@ def project(request, project_id):
if not user.owns(project):
raise HttpResponseForbidden()
# new corpus
# add a new corpus
if request.method == 'POST':
corpus = project.add_child(
name = request.POST['name'],
typename = 'CORPUS',
)
#check type and name
print(request.POST)
type = int(request.POST['type'])
try:
corpus.add_resource(
type = int(request.POST['type']),
path = upload(request.FILES['file']),
format = check_format(type, str(request.FILES['file']))
except TypeError as e:
return render(
template_name = 'pages/projects/overview.html',
request = request,
context = {
'debug': True,
#'date': datetime.now(),
# projects owned by the user
#'number': user_projects.count(),
#'projects': user_projects,
# projects owned by the user's contacts
#'common_users': (contact for contact, projects in contacts_projects),
#'common_projects': sum((projects for contact, projects in contacts_projects), []),
'error_msg': str(e),
},
)
try:
path = upload(request.FILES['file'])
except OSError:
#file upload limit in files.py
return render(
template_name = 'pages/projects/wait.html',
request = request,
context = {
'user' : request.user,
'project': project,
},
template_name = 'pages/projects/overview.html',
request = request,
context = {
'debug': True,
'date': datetime.now(),
# projects owned by the user
'number': user_projects.count(),
'projects': user_projects,
# projects owned by the user's contacts
'common_users': (contact for contact, projects in contacts_projects),
'common_projects': sum((projects for contact, projects in contacts_projects), []),
'error_msg':"File uploaded is two heavy > 1G ",
},
)
corpus.add_resource(
type,
path
)
#except Exception as error:
session.add(corpus)
session.commit()
......@@ -121,12 +157,12 @@ def project(request, project_id):
template_name = 'pages/projects/wait.html',
request = request,
context = {
'user' : request.user,
'project': project,
'user' : request.user,
'project': project,
},
)
# corpora within this project
# list all the corpora within this project
corpora = project.children('CORPUS', order=True).all()
print(corpora)
sourcename2corpora = defaultdict(list)
......@@ -138,9 +174,8 @@ def project(request, project_id):
resource_type_name = RESOURCETYPES[resource['type']]['name']
resource_type_accepted_formats = RESOURCETYPES[resource['type']]['accepted_formats']
else:
print("(WARNING) PROJECT view: no listed resource")
print("(DEBUG) PROJECT view: one of the corpus has an invalid type")
raise Http404("One of the corpus has an invalid type")
print("(WARNING) PROJECT view: no listed resource or one of the corpus has an invalid type")
# add some data for the viewer
corpus.count = corpus.children('DOCUMENT').count()
status = corpus.status()
......
......@@ -71,22 +71,28 @@
<div class="container">
<div class="container">
<div class="container">
{%if error_msg %}
<div class="alert alert-warning">
Error: {{ error_msg}}
</div>
{%endif %}
{% if projects %}
{% for project in projects %}
<!--<div class="col-md-offset-7 col-md-4 content" style="background-color:grey">!-->
<div id="project_{{project.id}}" class="row">
<h3>
<div class="col-md-1 content"></div>
<div class="col-md-5 content">
<a
<a
href="/projects/{{ project.id }}">
<span class="glyphicon glyphicon-book" aria-hidden="true"></span>
{{ project.name }}
</a>
</div>
<div class="col-md-3 content">
<a href="/projects/{{project.id}}" >
<button type="button" class="btn btn-default" aria-label="Left Align">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment