Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload and download moved to the general controller #4

Merged
merged 1 commit into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion machinehub/server/app/controllers/form_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ def filter_str(var):
if type(var) is str:
if (var.startswith('\'') and var.endswith('\'')) or \
(var.startswith('\"') and var.endswith('\"')):
result = str(var[1:-1].decode('utf-8'))
result = str(var[1:-1].decode('utf-8'))
return result
24 changes: 2 additions & 22 deletions machinehub/server/app/controllers/machine_controller.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from flask.globals import request
from flask.helpers import flash, send_from_directory, url_for
import os
from werkzeug.utils import redirect
from flask_classy import route, FlaskView
from flask.templating import render_template
from machinehub.server.app.controllers import resources
from machinehub.errors import NotMachineHub
from machinehub.server.app.controllers.auth_controller import requires_auth
from machinehub.config import MACHINEHUB_FOLDER, UPLOAD_FOLDER
from machinehub.config import UPLOAD_FOLDER
from machinehub.server.app.models.machine_model import MachineModel
from machinehub.server.app.controllers.form_generator import metaform
from flask.helpers import flash


types = {'int': int,
Expand Down Expand Up @@ -57,20 +54,3 @@ def machine(self, machine_name):
show_stl=show_stl,
file_name=file_name,
machine_name=machine_name)

@route('/download/<filename>')
def download(self, filename):
return send_from_directory(UPLOAD_FOLDER, filename)

@route('/upload', methods=['GET', 'POST'])
def upload(self):
if request.method == 'POST':
_file = request.files['file']
file_path = resources.save(_file, MACHINEHUB_FOLDER, ALLOWED_EXTENSIONS)
if file_path:
try:
name = self.machines_model.update(file_path)
return redirect(url_for('MachineController:machine', machine_name=name))
except NotMachineHub as e:
flash('WARNING! %s' % e.message, 'warning')
return render_template('machine/upload.html')
25 changes: 24 additions & 1 deletion machinehub/server/app/controllers/machinehub_controller.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from flask.helpers import url_for
from flask.helpers import url_for, send_from_directory, flash
from flask_classy import route, FlaskView
from flask.templating import render_template
from machinehub.server.app.controllers.auth_controller import requires_auth
from machinehub.server.app.models.machine_model import MachineModel
from werkzeug.exceptions import abort
from machinehub.server.app.models.explorer_model import Pagination
from machinehub.config import UPLOAD_FOLDER, MACHINEHUB_FOLDER
from machinehub.server.app.controllers import resources
from machinehub.errors import NotMachineHub
from werkzeug.utils import redirect
from flask.globals import request
from machinehub.server.app.controllers.machine_controller import ALLOWED_EXTENSIONS


PER_PAGE = 20
Expand Down Expand Up @@ -57,3 +63,20 @@ def chunks(l, n):

return render_template('home.html',
splited_machines_info=splited_machines_info)

@route('/download/<filename>')
def download(self, filename):
return send_from_directory(UPLOAD_FOLDER, filename)

@route('/upload', methods=['GET', 'POST'])
def upload(self):
if request.method == 'POST':
_file = request.files['file']
file_path = resources.save(_file, MACHINEHUB_FOLDER, ALLOWED_EXTENSIONS)
if file_path:
try:
name = self.machines_model.update(file_path)
return redirect(url_for('MachineController:machine', machine_name=name))
except NotMachineHub as e:
flash('WARNING! %s' % e.message, 'warning')
return render_template('machine/upload.html')
2 changes: 1 addition & 1 deletion machinehub/server/app/templates/base/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="/">Home</a></li>
<li><a href="{{ url_for('MachinehubController:machines', page=1) }}">Machines</a></li>
<li><a href="{{ url_for('MachineController:upload') }}">Upload</a></li>
<li><a href="{{ url_for('MachinehubController:upload') }}">Upload</a></li>
</ul>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions machinehub/server/app/templates/machine/machine.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
thingiview.setObjectColor('#C0D8F0');
thingiview.initScene();
thingiview.setRotation(false);
thingiview.loadSTL("{{ url_for('MachineController:download', filename=file_name) }}");
thingiview.loadSTL("{{ url_for('MachinehubController:download', filename=file_name) }}");
}
</script>

Expand Down Expand Up @@ -89,7 +89,7 @@ <h1>{{ title }}</h1>
</div>
<div class="container text-center">
{% if show_stl %}
<a name="viewer" style="text-align:center;" href="{{ url_for('MachineController:download', filename=file_name) }}" class="btn btn-lg btn-primary"><span class="glyphicon glyphicon-download-alt"></span> Download the generated STL</a><br><br>
<a name="viewer" style="text-align:center;" href="{{ url_for('MachinehubController:download', filename=file_name) }}" class="btn btn-lg btn-primary"><span class="glyphicon glyphicon-download-alt"></span> Download the generated STL</a><br><br>
<p>
<input class="btn btn-primary" onclick="thingiview.setCameraView('top');" type="button" value="Top" />
<input class="btn btn-primary" onclick="thingiview.setCameraView('side');" type="button" value="Side" />
Expand Down
2 changes: 1 addition & 1 deletion machinehub/server/app/templates/machine/upload.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base/layout.html" %}
{% block content %}
<div class="container text-center">
<form action="{{ url_for('MachineController:upload') }}" method=post enctype=multipart/form-data role="form">
<form action="{{ url_for('MachinehubController:upload') }}" method=post enctype=multipart/form-data role="form">
<div class="form-group">
<label for="ejemplo_archivo_1">Add a new machine file!</label>
<br>
Expand Down