Private
Public Access
1
0

Merge branch 'feature/droparea' into develop

This commit is contained in:
Sander Roosendaal
2017-11-07 08:34:46 +01:00
5 changed files with 251 additions and 69 deletions

View File

@@ -49,8 +49,9 @@ class StrokeDataForm(forms.Form):
# The form used for uploading files # The form used for uploading files
class DocumentsForm(forms.Form): class DocumentsForm(forms.Form):
title = forms.CharField(required=False) title = forms.CharField(required=False)
file = forms.FileField(required=True, file = forms.FileField(required=False,
validators=[validate_file_extension]) validators=[validate_file_extension])
workouttype = forms.ChoiceField(required=True, workouttype = forms.ChoiceField(required=True,
choices=Workout.workouttypes) choices=Workout.workouttypes)
# initial='rower') # initial='rower')

View File

@@ -3,7 +3,10 @@
import shutil import shutil
import time import time
from django.conf import settings from django.conf import settings
from rowers.tasks import handle_sendemail_unrecognized from rowers.tasks import (
handle_sendemail_unrecognized,
handle_sendemail_unrecognizedowner
)
from django_mailbox.models import Message, MessageAttachment from django_mailbox.models import Message, MessageAttachment
from rowers.models import User, Rower, RowerForm from rowers.models import User, Rower, RowerForm

View File

@@ -5,14 +5,15 @@
{% block title %}File loading{% endblock %} {% block title %}File loading{% endblock %}
{% block meta %} {% block meta %}
<script type='text/javascript' <script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'> src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'>
</script> </script>
<script type='text/javascript' <script type='text/javascript'
src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js'> src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js'>
</script> </script>
<script> <script>
$(document).ready(function() { $(document).ready(function() {
$('#id_file').on('change', function(evt) { $('#id_file').on('change', function(evt) {
var f = this.files[0]; var f = this.files[0];
var istcx = false; var istcx = false;
@@ -45,10 +46,11 @@
} }
}); });
}); });
</script> </script>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<div id="id_drop-files" class="grid_12 alpha drop-files">
<form id="file_form" enctype="multipart/form-data" action="{{ formloc }}" method="post"> <form id="file_form" enctype="multipart/form-data" action="{{ formloc }}" method="post">
<div id="left" class="grid_6 alpha"> <div id="left" class="grid_6 alpha">
<h1>Upload Workout File</h1> <h1>Upload Workout File</h1>
@@ -85,10 +87,148 @@
If you check "make private", this workout will not be visible to your followers and will not show up in your teams' workouts list. With the Landing Page option, you can select to which (workout related) page you will be If you check "make private", this workout will not be visible to your followers and will not show up in your teams' workouts list. With the Landing Page option, you can select to which (workout related) page you will be
taken after a successfull upload. taken after a successfull upload.
</p> </p>
<p>Select Files with the File button or drag them on the marked area</p>
</div> </div>
</form> </form>
</div>
{% endblock %} {% endblock %}
{% block scripts %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
console.log("CSRF token",csrftoken);
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
console.log("Loading dropper");
jQuery.event.props.push('dataTransfer');
var frm = $("#file_form");
var data = new FormData(frm[0]);
$('input').each(function( i ) {
$(this).change(function() {
if ($(this).attr('type') == 'checkbox') {
data.set($(this).attr('name'),$(this).prop('checked'));
console.log($(this).attr('id'),$(this).attr('name'),$(this).prop('checked'));
} else {
data.set($(this).attr('name'),$(this).val());
if ($(this).attr('id') == 'id_file') {
data.set("file",this.files[0]);
}
console.log($(this).attr('name'),$(this).val());
};
});});
$('select').each(function( i ) {
console.log($(this).attr('name'),$(this).val());
$(this).change(function() {
data.set($(this).attr('name'),$(this).val());
console.log($(this).attr('id'),$(this).attr('name'),$(this).val());
});
});
frm.submit(function() {
console.log("Form submission");
console.log(data);
$("#id_drop-files").replaceWith(
'<div id="id_waiting"><img src="/static/img/ajax_loader_blue_350.gif" width="120" height="100">'
);
$.ajax({
data: data,
type: $(this).attr('method'),
url: '/rowers/workout/upload/',
contentType: false,
processData: false,
error: function(result) {
$("#id_waiting").replaceWith(
'<div id="id_failed" class="grid_12 alpha message">Your upload failed</div>'
);
setTimeout(function() {
location.reload();
},1000);
},
success: function(result) {
console.log('got something back');
console.log(result);
if (result.result == 1) {
window.location.href = result.url;
}
}
});
return false;
});
$('#id_drop-files').bind({
drop: function(e) {
e.preventDefault();
console.log("you dropped something");
var files = e.dataTransfer.files;
console.log(files[0]);
var f = files[0];
var istcx = false;
var isgzip = false;
var size1 = 10485760;
var size2 = 1048576;
if ((/\.(tcx|TCX)/i).test(f.name)) {
istcx = true;
console.log('tcx');
if ((/\.(gz|GZ)/i).test(f.name)) {
isgzip = true;
console.log('gzip');
size1 /= 5;
size2 /= 5;
}
}
console.log(f);
console.log(size1)
console.log(size2)
if (f.size > size1) {
alert("File Size must be smaller than 10 MB");
$("#id_file").value = 0;
return false;
}
if (f.size > size2) {
$('#id_offline').val('True');
$('#id_offline').prop('checked','True');
console.log("Set offline to True");
$('#extra_message').text('Because of the large size, we recommend to use background processing. You will receive email when it is done. The extra actions will not be performed.');
$('#extra_message').addClass('message');
}
data.append("file",files[0]);
$("#id_file").replaceWith('<div id="id_file">'+files[0].name+'&nbsp; <a class="remove" href="javascript:void(0);"><b><font color="red">X</font></b></a></div>');
},
mouseenter:function(){$("#id_drop-files").css("background-color","#E9E9E4");},
mouseleave:function(){$("#id_drop-files").css("background-color","#FFFFFF");},
dragover:function(e){
e.preventDefault();
$("#id_drop-files").css("background-color","#E9E9E4");},
dragleave:function(e){ e.preventDefault();},
});
$(document).on("click", "a.remove", function() {
$(this).parent().replaceWith('<td><input id="id_file" name="file" type="file" /></td>');
});
})
</script>
{% endblock %}

View File

@@ -1671,6 +1671,7 @@ def workout_csvtoadmin_view(request,id=0):
# Send workout to TP # Send workout to TP
@login_required() @login_required()
def workout_tp_upload_view(request,id=0): def workout_tp_upload_view(request,id=0):
message = "" message = ""
r = getrower(request.user) r = getrower(request.user)
res = -1 res = -1
@@ -8414,6 +8415,10 @@ def workout_upload_view(request,
'workouttype':'rower', 'workouttype':'rower',
}): }):
is_ajax = False
if request.is_ajax():
is_ajax = True
r = getrower(request.user) r = getrower(request.user)
if 'uploadoptions' in request.session: if 'uploadoptions' in request.session:
@@ -8489,8 +8494,10 @@ def workout_upload_view(request,
if request.method == 'POST': if request.method == 'POST':
form = DocumentsForm(request.POST,request.FILES) form = DocumentsForm(request.POST,request.FILES)
optionsform = UploadOptionsForm(request.POST) optionsform = UploadOptionsForm(request.POST)
if form.is_valid(): if form.is_valid():
f = request.FILES['file'] f = request.FILES['file']
res = handle_uploaded_file(f) res = handle_uploaded_file(f)
t = form.cleaned_data['title'] t = form.cleaned_data['title']
workouttype = form.cleaned_data['workouttype'] workouttype = form.cleaned_data['workouttype']
@@ -8552,18 +8559,27 @@ def workout_upload_view(request,
request, request,
"The file was too large to process in real time. It will be processed in a background process. You will receive an email when it is ready") "The file was too large to process in real time. It will be processed in a background process. You will receive an email when it is ready")
url = reverse(workout_upload_view) url = reverse(workout_upload_view)
if is_ajax:
return JSONResponse({'result':1,'url':url})
else:
response = HttpResponseRedirect(url) response = HttpResponseRedirect(url)
return response return response
if not id: if not id:
messages.error(request,message) messages.error(request,message)
url = reverse(workout_upload_view) url = reverse(workout_upload_view)
if is_ajax:
return JSONResponse({'result':0,'url':url})
else:
response = HttpResponseRedirect(url) response = HttpResponseRedirect(url)
return response return response
elif id == -1: elif id == -1:
message = 'The zip archive will be processed in the background. The files in the archive will only be uploaded without the extra actions. You will receive email when the workouts are ready.' message = 'The zip archive will be processed in the background. The files in the archive will only be uploaded without the extra actions. You will receive email when the workouts are ready.'
messages.info(request,message) messages.info(request,message)
url = reverse(workout_upload_view) url = reverse(workout_upload_view)
if is_ajax:
return JSONResponse({'result':1,'url':url})
else:
response = HttpResponseRedirect(url) response = HttpResponseRedirect(url)
return response return response
else: else:
@@ -8575,7 +8591,11 @@ def workout_upload_view(request,
'id':int(id), 'id':int(id),
}) })
if is_ajax:
response = {'result': 1,'url':url}
else:
response = HttpResponseRedirect(url) response = HttpResponseRedirect(url)
w = Workout.objects.get(id=id) w = Workout.objects.get(id=id)
r = getrower(request.user) r = getrower(request.user)
@@ -8672,8 +8692,12 @@ def workout_upload_view(request,
else: else:
url = reverse(landingpage) url = reverse(landingpage)
return HttpResponseRedirect(url) if is_ajax:
response = {'result':1,'url':url}
else: else:
response = HttpResponseRedirect(url)
else:
if not is_ajax:
response = render(request, response = render(request,
'document_form.html', 'document_form.html',
{'form':form, {'form':form,
@@ -8681,8 +8705,13 @@ def workout_upload_view(request,
'optionsform': optionsform, 'optionsform': optionsform,
}) })
if is_ajax:
return JSONResponse(response)
else:
return response return response
else: else:
if not is_ajax:
form = DocumentsForm(initial=docformoptions) form = DocumentsForm(initial=docformoptions)
optionsform = UploadOptionsForm(initial=uploadoptions) optionsform = UploadOptionsForm(initial=uploadoptions)
return render(request, 'document_form.html', return render(request, 'document_form.html',
@@ -8690,6 +8719,9 @@ def workout_upload_view(request,
'teams':get_my_teams(request.user), 'teams':get_my_teams(request.user),
'optionsform': optionsform, 'optionsform': optionsform,
}) })
else:
return {'result':0}
# This is the main view for processing uploaded files # This is the main view for processing uploaded files
@user_passes_test(iscoachmember,login_url="/",redirect_field_name=None) @user_passes_test(iscoachmember,login_url="/",redirect_field_name=None)

View File

@@ -790,3 +790,9 @@ a.wh:hover {
-webkit-margin-before: 0em; -webkit-margin-before: 0em;
-webkit-margin-after: 0em; -webkit-margin-after: 0em;
} }
.drop-files {
border: 1px solid #000;
color: #000;
}