Commit 15202613 authored by Raphael Jacquot's avatar Raphael Jacquot
Browse files

we have an upload / display loop !

parent a245b277
FROM python:3
ADD . /app
WORKDIR /app
COPY . /app
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev
RUN pip install --upgrade pip setuptools
RUN apt-get update -y && \
apt-get install -y python-pip python-dev && \
pip install --upgrade pip setuptools wtforms && \
python setup.py develop
# ca ne semble pas fonctionner comme il faut, ca n'installe pas les dependances
RUN python setup.py install
CMD ["pserve", "development.ini"]
\ No newline at end of file
......@@ -25,16 +25,17 @@ jmdc_app/models/__init__.py
jmdc_app/models/meta.py
jmdc_app/models/mymodel.py
jmdc_app/models/star_info.py
jmdc_app/models/submission.py
jmdc_app/scripts/__init__.py
jmdc_app/scripts/initialize_db.py
jmdc_app/static/pyramid-16x16.png
jmdc_app/static/pyramid.png
jmdc_app/static/theme.css
jmdc_app/templates/404.jinja2
jmdc_app/templates/jmdc_home.jinja2
jmdc_app/templates/layout.jinja2
jmdc_app/templates/mytemplate.jinja2
jmdc_app/templates/upload_csv.jinja2
jmdc_app/views/__init__.py
jmdc_app/views/default.py
jmdc_app/views/jmdc_home.py
jmdc_app/views/notfound.py
\ No newline at end of file
jmdc_app/views/notfound.py
jmdc_app/views/upload_csv.py
\ No newline at end of file
......@@ -9,8 +9,8 @@ pyramid_tm
SQLAlchemy
transaction
zope.sqlalchemy
wtforms
wtforms-alchemy
WTForms
WTForms-Alchemy
pyramid-wtforms
[testing]
......
......@@ -74,3 +74,47 @@ class StarInfo(Base):
def __init__(self, data):
self.id1 = data[0]
self.id2 = data[1]
print('ud_diam', end=' ')
self.ud_diam = self.parse_float(data[2])
print('ld_diam', end=' ')
self.ld_diam = self.parse_float(data[3])
print('e_ld_diam', end=' ')
self.e_ld_diam = self.parse_float(data[4])
print('band', end=' ')
self.band = data[5]
print('mu_lambda', end=' ')
self.mu_lambda = self.parse_float(data[6])
print('method', end=' ')
self.method = self.parse_int(data[7])
print('band_code', end=' ')
self.band_code = self.parse_int(data[8])
print('notes', end=' ')
self.notes = data[9]
# note: there is no bibcode in the sample file
#print('bibcode', end=' ')
#self.bibcode = data[10]
# print('com', end=' ')
# try:
# self.com = data[10]
# except:
# self.com = None
print('done')
def parse_int(self, value):
if not value:
return None
return int(value)
def parse_float(self, value):
if not value:
return None
if isinstance(value, str):
try:
value = float(value)
return value
except ValueError:
value = value.replace(',', '.')
# if this doesn't go through, something is definately wrong
value = float(value)
return value
\ No newline at end of file
def includeme(config):
config.add_static_view('static', 'static', cache_max_age=3600)
#config.add_route('home', '/')
config.add_route('jmdc_home', '/')
\ No newline at end of file
config.add_route('upload_csv', '/upload_csv/')
config.add_route('jmdc_catalog', '/')
\ No newline at end of file
<html>
<body>
<div id="header"><a href="{{request.route_url('upload_csv')}}">envoyer un fichier CSV</a></div>
<div>
<table>
<tr>
<th>id1</th>
<th>id2</th>
<th>ud_diam</th>
<th>ld_diam</th>
<th>e_ld_diam</th>
</tr>
{% for star in stars %}
<tr>
<td>{{star.id1}}</td>
<td>{{star.id2}}</td>
<td>{{star.ud_diam}}</td>
<td>{{star.ld_diam}}</td>
<td>{{star.e_ld_diam}}</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
\ No newline at end of file
<html>
<body>
<div>testing stuff</div>
<form method="post" action="{{request.route_url('jmdc_home')}}" enctype="multipart/form-data" >
<form method="post" action="{{request.route_url('upload_csv')}}" enctype="multipart/form-data" >
{{form.import_file.label}} {{form.import_file}}
<button type="submit">envoyer</button>
</form>
......
from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy.exc import DBAPIError
from jmdc_app.models.star_info import StarInfo
@view_config(route_name='jmdc_catalog', renderer='../templates/jmdc_catalog.jinja2')
def jmdc_home(request):
session = request.dbsession
stars = session.query(StarInfo).all()
return { 'stars': stars }
\ No newline at end of file
import codecs
import csv
from pyramid.httpexceptions import HTTPFound
from pyramid.response import Response
from pyramid.view import view_config
from sqlalchemy.exc import DBAPIError
from jmdc_app.forms.csv_import import FormImportCsv
from jmdc_app.models.submission import StarInfoEntry, Submission
from jmdc_app.models.star_info import StarInfo
from jmdc_app.models.submission import StarInfoEntry, Submission
from .. import models
......@@ -18,11 +19,9 @@ def import_entry(entry):
return None
si = StarInfo(entry)
# si.id1 = entry[0]
# si.id2 = entry[1]
return si
@view_config(route_name='jmdc_home', renderer='../templates/jmdc_home.jinja2')
@view_config(route_name='upload_csv', renderer='../templates/upload_csv.jinja2')
def jmdc_home(request):
session = request.dbsession
......@@ -38,16 +37,32 @@ def jmdc_home(request):
reader = csv.reader(codecs.iterdecode(data_src, 'utf-8'))
sub = Submission()
for row in reader:
print(row)
line = 0
try:
for row in reader:
print(row)
si = import_entry(row)
if si:
sub.star_info_entries.append(StarInfoEntry(si))
line += 1
except Exception as e:
print('\n')
print(e)
print('something bad happened on line %d'%(line))
return {'form': format}
else:
session.add(sub)
si = import_entry(row)
if si:
sub.star_info_entries.append(StarInfoEntry(si))
session.add(sub)
print(si.submission)
print(si.submission)
# everything ok
# * commit the transaction
session.commit()
# * send back to the main catalog page
return HTTPFound(location=request.route_url('jmdc_catalog'))
finally:
print("hit finally")
session.rollback()
......
......@@ -20,8 +20,8 @@ requires = [
'SQLAlchemy',
'transaction',
'zope.sqlalchemy',
'wtforms',
'wtforms-alchemy',
'WTForms',
'WTForms-Alchemy',
'pyramid-wtforms',
]
......
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