Commit 9a0747ad authored by c24b's avatar c24b

Check file format error if wrong extension file

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