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
class DocumentsForm(forms.Form):
title = forms.CharField(required=False)
file = forms.FileField(required=True,
file = forms.FileField(required=False,
validators=[validate_file_extension])
workouttype = forms.ChoiceField(required=True,
choices=Workout.workouttypes)
# initial='rower')

View File

@@ -3,7 +3,10 @@
import shutil
import time
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 rowers.models import User, Rower, RowerForm

View File

@@ -5,50 +5,52 @@
{% block title %}File loading{% endblock %}
{% block meta %}
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'>
</script>
<script type='text/javascript'
src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js'>
</script>
<script>
$(document).ready(function() {
$('#id_file').on('change', function(evt) {
var f = this.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");
this.value = null;
}
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');
}
});
});
</script>
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'>
</script>
<script type='text/javascript'
src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js'>
</script>
<script>
$(document).ready(function() {
$('#id_file').on('change', function(evt) {
var f = this.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");
this.value = null;
}
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');
}
});
});
</script>
{% 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">
<div id="left" class="grid_6 alpha">
<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
taken after a successfull upload.
</p>
<p>Select Files with the File button or drag them on the marked area</p>
</div>
</form>
</form>
</div>
{% 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
@login_required()
def workout_tp_upload_view(request,id=0):
message = ""
r = getrower(request.user)
res = -1
@@ -8414,6 +8415,10 @@ def workout_upload_view(request,
'workouttype':'rower',
}):
is_ajax = False
if request.is_ajax():
is_ajax = True
r = getrower(request.user)
if 'uploadoptions' in request.session:
@@ -8489,8 +8494,10 @@ def workout_upload_view(request,
if request.method == 'POST':
form = DocumentsForm(request.POST,request.FILES)
optionsform = UploadOptionsForm(request.POST)
if form.is_valid():
f = request.FILES['file']
res = handle_uploaded_file(f)
t = form.cleaned_data['title']
workouttype = form.cleaned_data['workouttype']
@@ -8552,20 +8559,29 @@ def workout_upload_view(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")
url = reverse(workout_upload_view)
response = HttpResponseRedirect(url)
return response
if is_ajax:
return JSONResponse({'result':1,'url':url})
else:
response = HttpResponseRedirect(url)
return response
if not id:
messages.error(request,message)
url = reverse(workout_upload_view)
response = HttpResponseRedirect(url)
return response
if is_ajax:
return JSONResponse({'result':0,'url':url})
else:
response = HttpResponseRedirect(url)
return response
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.'
messages.info(request,message)
url = reverse(workout_upload_view)
response = HttpResponseRedirect(url)
return response
if is_ajax:
return JSONResponse({'result':1,'url':url})
else:
response = HttpResponseRedirect(url)
return response
else:
if message:
messages.error(request,message)
@@ -8574,8 +8590,12 @@ def workout_upload_view(request,
kwargs = {
'id':int(id),
})
response = HttpResponseRedirect(url)
if is_ajax:
response = {'result': 1,'url':url}
else:
response = HttpResponseRedirect(url)
w = Workout.objects.get(id=id)
r = getrower(request.user)
@@ -8671,26 +8691,38 @@ def workout_upload_view(request,
})
else:
url = reverse(landingpage)
return HttpResponseRedirect(url)
if is_ajax:
response = {'result':1,'url':url}
else:
response = HttpResponseRedirect(url)
else:
response = render(request,
'document_form.html',
{'form':form,
'teams':get_my_teams(request.user),
'optionsform': optionsform,
})
if not is_ajax:
response = render(request,
'document_form.html',
{'form':form,
'teams':get_my_teams(request.user),
'optionsform': optionsform,
})
return response
if is_ajax:
return JSONResponse(response)
else:
return response
else:
form = DocumentsForm(initial=docformoptions)
optionsform = UploadOptionsForm(initial=uploadoptions)
return render(request, 'document_form.html',
{'form':form,
'teams':get_my_teams(request.user),
'optionsform': optionsform,
})
if not is_ajax:
form = DocumentsForm(initial=docformoptions)
optionsform = UploadOptionsForm(initial=uploadoptions)
return render(request, 'document_form.html',
{'form':form,
'teams':get_my_teams(request.user),
'optionsform': optionsform,
})
else:
return {'result':0}
# This is the main view for processing uploaded files
@user_passes_test(iscoachmember,login_url="/",redirect_field_name=None)
def team_workout_upload_view(request,message="",