1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-02 23:48:47 +00:00

fixed bug and improved code for file/subdir share in repo/file_view

This commit is contained in:
llj
2013-01-07 15:34:25 +08:00
parent 415e71608c
commit ff62c38ce2
3 changed files with 57 additions and 100 deletions

View File

@@ -386,7 +386,7 @@ def get_shared_link(request):
path = request.GET.get('p', '')
if not (repo_id and path):
err = _('Invalid arguments')
data = json.dumps([{'error': err}])
data = json.dumps({'error': err})
return HttpResponse(data, status=400, content_type=content_type)
if share_type == 'f':
@@ -394,8 +394,8 @@ def get_shared_link(request):
path = path[:-1]
else:
if path == '/': # can not share root dir
err = _('Can not share root dir.')
data = json.dumps([{'error': err}])
err = _('You cannot share the repo in this way.')
data = json.dumps({'error': err})
return HttpResponse(data, status=400, content_type=content_type)
else:
if path[-1] != '/': # append '/' at end of path
@@ -420,12 +420,12 @@ def get_shared_link(request):
fs.save()
except IntegrityError, e:
err = _('Failed to get the link, please retry later.')
data = json.dumps([{'error': err}])
data = json.dumps({'error': err})
return HttpResponse(data, status=500, content_type=content_type)
shared_link = gen_shared_link(request, token, fs.s_type)
data = json.dumps([{'token': token, 'shared_link': shared_link}])
data = json.dumps({'token': token, 'shared_link': shared_link})
return HttpResponse(data, status=200, content_type=content_type)
@login_required
@@ -449,8 +449,8 @@ def remove_shared_link(request):
FileShare.objects.filter(token=token).delete()
msg = _('Removed successfully')
data = json.dumps([{'msg': msg}])
msg = _('Deleted successfully')
data = json.dumps({'msg': msg})
return HttpResponse(data, status=200, content_type=content_type)
@login_required
@@ -490,10 +490,10 @@ def send_shared_link(request):
t.render(Context(c)), None, [to_email],
fail_silently=False)
except:
data = json.dumps({'error':_(u'Send failed.')})
data = json.dumps({'error':_(u'Internal server error. Send failed.')})
return HttpResponse(data, status=500, content_type=content_type)
data = json.dumps("success")
data = json.dumps({"msg": _(u'Successfully sent.')})
return HttpResponse(data, status=200, content_type=content_type)
else:
return HttpResponseBadRequest(json.dumps(form.errors),

View File

@@ -661,25 +661,24 @@ $('.file-share').click(function() {
if ($(this).data('link')) {
showPopup($(this).data('link'));
} else {
var aj_url = '';
var aj_url = '{% url 'get_shared_link' %}?repo_id={{ repo.id }}&p={{ path|urlencode }}' + e(filename);
if ($(this).data('type') == 'd') {
aj_url = '{% url 'get_shared_link' %}?repo_id={{ repo.id }}&p={{ path|urlencode }}' + e(filename) + '&type=d';
} else {
aj_url = '{% url 'get_shared_link' %}?repo_id={{ repo.id }}&p={{ path|urlencode }}' + e(filename);
}
aj_url += '&type=d';
}
$.ajax({
url: aj_url,
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf-8',
success: function(data) {
if (data.length > 0) {
showPopup(data[0]['shared_link']);
}
showPopup(data['shared_link']);
},
error: function(xhr, ajaxOptions, thrownError) {
var jsonVal = jQuery.parseJSON(xhr.responseText);
feedback(jsonVal[0]['error'], 'error');
error: function(xhr, textStatus, errorThrown) {
if (xhr.responseText) {
feedback(jQuery.parseJSON(xhr.responseText).error, 'error');
} else {
feedback('{% trans "Failed. Please check the network." %}', 'error');
}
}
});
}
@@ -952,69 +951,10 @@ $('#rename-form').submit(function() {
});
return false;
});
// file share form
var share_list = [];
{% for contact in contacts %}
share_list.push({value:'{{ contact.contact_email }}', label:'{{ contact.contact_email }}'});
{% endfor %}
$('#send-link').click(function() {
$(this).addClass('hide');
var input = $('#link-send-input');
input.css({'min-width':input.css('width'), 'width': $('#file-share').width() - parseInt(input.css('padding-left')) - parseInt(input.css('padding-right')) - parseInt(input.css('border-left-width')) - parseInt(input.css('border-right-width'))});
$('#link-send-form').removeClass('hide');
addAutocomplete('#link-send-input', '#link-send-form', share_list);
});
$("#link-send-form").submit(function() {
var form = $(this),
file_shared_link = form.children('input[name="file_shared_link"]').val(),
email = $.trim(form.children('textarea[name="email"]').val()),
submit_btn = form.children('input[type="submit"]'),
err = form.children('.error');
if (!email) {
err.html('{% trans "Please input at least an email." %}').removeClass('hide');
return false;
}
disable(submit_btn);
err.addClass('hide');
$('#sending').removeClass('hide');
$.ajax({
type: "POST",
url: "{% url 'send_shared_link' %}",
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf-8',
beforeSend: prepareCSRFToken,
data: {file_shared_link: file_shared_link, email: email},
success: function(data) {
$.modal.close();
feedback('{% trans "Successfully shared." %}', "success");
},
error: function(data, textStatus, jqXHR) {
$('#sending').addClass('hide');
enable(submit_btn);
var errors = $.parseJSON(data.responseText);
$.each(errors, function(index, value) {
if (index == 'error') {
err.html(value);
} else {
err.html(value[0]);
}
err.removeClass('hide');
});
}
});
return false;
});
$('#shared-link-text').click(function() { $(this).select(); });
{% if path == '/' %}
{% include "snippets/list_commit_detail.html" %}
{% else %}
{% include "snippets/shared_link_js.html" %}
{% endif %}
{% include "snippets/shared_link_js.html" %}
{% include "snippets/bottom_bar.html" %}
</script>
{% include 'snippets/file_upload_progress_js.html' %}

View File

@@ -31,17 +31,17 @@ $('#get-shared-link').click(function() {
cache: false,
contentType: 'application/json; charset=utf-8',
success: function(data) {
if (data.length > 0) {
var t = data[0]['token'];
$('#rm-shared-link').attr('data', '{% url 'remove_shared_link' %}?t=' + t);
$('#shared-link, input[name="file_shared_link"]').val(data[0]['shared_link']);
setLinkWidth();
showLink();
}
$('#rm-shared-link').attr('data', '{% url 'remove_shared_link' %}?t=' + data['token']);
$('#shared-link, input[name="file_shared_link"]').val(data['shared_link']);
setLinkWidth();
showLink();
},
error: function(xhr, ajaxOptions, thrownError) {
var jsonVal = jQuery.parseJSON(xhr.responseText);
feedback(jsonVal[0]['error'], 'error');
error: function(xhr, textStatus, errorThrown) {
if (xhr.responseText) {
feedback(jQuery.parseJSON(xhr.responseText).error, 'error');
} else {
feedback('{% trans "Failed. Please check the network." %}', 'error');
}
}
});
});
@@ -55,7 +55,11 @@ $('#rm-shared-link').click(function() {
contentType: 'application/json; charset=utf-8',
success: function(data) {
hideLink();
feedback(data['msg'], 'success');
$('#shared-link').val('');
},
error: function() {
feedback('{% trans "Failed. Please check the network." %}', 'error');
}
});
});
@@ -64,11 +68,18 @@ var share_list = [];
{% for contact in contacts %}
share_list.push({value:'{{ contact.contact_email }}', label:'{{ contact.contact_email }}'});
{% endfor %}
$('#send-shared-link').click(function() {
$('#send-shared-link').click(function() { // for file_view.html and subdir page
$("#link-send-form").modal({appendTo: "#main", focus: false});
$('#simplemodal-container').css('height', 'auto');
addAutocomplete('#link-send-input', '#link-send-form', share_list);
});
$('#send-link').click(function() { // for 'file-share' in repo.html
$(this).addClass('hide');
var input = $('#link-send-input');
input.css({'min-width':input.css('width'), 'width': $('#file-share').width() - parseInt(input.css('padding-left')) - parseInt(input.css('padding-right')) - parseInt(input.css('border-left-width')) - parseInt(input.css('border-right-width'))});
$('#link-send-form').removeClass('hide');
addAutocomplete('#link-send-input', '#link-send-form', share_list);
});
$("#link-send-form").submit(function(event) {
var form = $(this),
@@ -95,23 +106,29 @@ $("#link-send-form").submit(function(event) {
data: {file_shared_link: file_shared_link, email: email},
success: function(data) {
$.modal.close();
feedback('{% trans "Successfully sent." %}', "success");
feedback(data['msg'], "success");
},
error: function(data, textStatus, jqXHR) {
error: function(xhr, textStatus, errorThrown) {
$('#sending').addClass('hide');
enable(submit_btn);
var errors = $.parseJSON(data.responseText);
$.each(errors, function(index, value) {
if (index == 'error') {
apply_form_error('link-send-form', value);
var err_str = '';
if (xhr.responseText) {
var err = jQuery.parseJSON(xhr.responseText);
if (err.error) {
err_str = err.error;
} else {
apply_form_error('link-send-form', value[0]);
for (var i in err) {
err_str += err[i];
}
}
});
} else {
err_str = '{% trans "Failed. Please check the network." %}';
}
apply_form_error('link-send-form', err_str);
}
});
return false;
});
$('#shared-link').click(function() {
$('#shared-link, #shared-link-text').click(function() {
$(this).select();
});