diff --git a/forms.py b/forms.py index 0476e4809f..84cf21c8b6 100644 --- a/forms.py +++ b/forms.py @@ -45,8 +45,11 @@ class FileLinkShareForm(forms.Form): Form for sharing file shared link to emails. """ - email = forms.CharField(max_length=512) - file_shared_link = forms.CharField(max_length=40) + email = forms.CharField(max_length=512, error_messages={ + 'required': '输入不能为空', + 'max_length': '邮箱太长,不超过512个字符' + }) + file_shared_link = forms.CharField() class FileCommentForm(forms.Form): """ diff --git a/media/js/utils.js b/media/js/utils.js index 1a4a52e771..6c7e101dfe 100644 --- a/media/js/utils.js +++ b/media/js/utils.js @@ -267,7 +267,7 @@ function apply_form_error(formid, error_msg) { var form_err = $("#" + formid + " .error"), container = $("#simplemodal-container"); - form_err.html(error_msg).attr('class', 'error'); + form_err.html(error_msg).attr('class', 'error').removeClass('hide'); container.css('height', $('#'+formid).height()); } diff --git a/templates/repo_view_file.html b/templates/repo_view_file.html index 849eeab214..870c690521 100644 --- a/templates/repo_view_file.html +++ b/templates/repo_view_file.html @@ -77,10 +77,9 @@

-

输入不能为空。

-

发送中...

-

+

+

发送中...

@@ -233,42 +232,36 @@ $('#send-shared-link').click(function() { }); $("#link-send-form").submit(function(event) { - $('#error, #sending, #success').attr('class', 'hide'); + $('#link-send-form .error').addClass('hide'); + $('#sending').removeClass('hide'); + var form = $(this), file_shared_link = form.children('input[name="file_shared_link"]').val(), email = $.trim(form.children('textarea[name="email"]').val()); - if (!email) { - $('#error').attr('class', 'error'); - $('#simplemodal-container').css('height', $('#link-send-form').height()); - return false; - } - - $('#sending').removeClass('hide'); - $('#simplemodal-container').css('height', $('#link-send-form').height()); - - if (email.length <= 512) { $.ajax({ type: "POST", - url: "{{ SITE_ROOT }}sharedlink/send/", + url: "{% url 'seahub.views.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) { - $('#sending').attr('class', 'hide'); - $('#success').html(data[0]['msg']).removeClass('hide'); - $('#simplemodal-container').css('height', $('#link-send-form').height()); + location.reload(true); }, - error: function(xhr, ajaxOptions, thrownError) { - var jsonVal = jQuery.parseJSON(xhr.responseText); - $('#sending').attr('class', 'hide'); - $('#error').html(jsonVal[0]['error']).attr('class','error'); - $('#simplemodal-container').css('height', $('#link-send-form').height()); + error: function(data, textStatus, jqXHR) { + $('#sending').addClass('hide'); + var errors = $.parseJSON(data.responseText); + $.each(errors, function(index, value) { + if (index == 'error') { + apply_form_error('link-send-form', value); + } else { + apply_form_error('link-send-form', value[0]); + } + }); } }); - } return false; }); diff --git a/views.py b/views.py index 43686defcf..9f80cd50c6 100644 --- a/views.py +++ b/views.py @@ -2213,47 +2213,46 @@ def remove_shared_link(request): @login_required def send_shared_link(request): """ - Handle ajax post request to share file shared link. + Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 + result = {} content_type = 'application/json; charset=utf-8' - + form = FileLinkShareForm(request.POST) - if not form.is_valid(): - err = '发送失败' - data = json.dumps([{'error':err}]) - return HttpResponse(data, status=400, content_type=content_type) + if form.is_valid(): + email = form.cleaned_data['email'] + file_shared_link = form.cleaned_data['file_shared_link'] - email = form.cleaned_data['email'] - file_shared_link = form.cleaned_data['file_shared_link'] + t = loader.get_template('shared_link_email.html') + to_email_list = string2list(email) + for to_email in to_email_list: + # Add email to contacts. + mail_sended.send(sender=None, user=request.user.username, + email=to_email) - t = loader.get_template('shared_link_email.html') - to_email_list = string2list(email) - for to_email in to_email_list: - # Add email to contacts - mail_sended.send(sender=None, user=request.user.username, - email=to_email) + c = { + 'email': request.user.username, + 'to_email': to_email, + 'file_shared_link': file_shared_link, + } - c = { - 'email': request.user.username, - 'to_email': to_email, - 'file_shared_link': file_shared_link, - } - - try: - send_mail('您的好友通过SeaCloud分享了一个文件给您', - t.render(Context(c)), None, [to_email], - fail_silently=False) - except: - err = '发送失败' - data = json.dumps([{'error':err}]) - return HttpResponse(data, status=500, content_type=content_type) - - msg = '发送成功。' - data = json.dumps([{'msg': msg}]) - return HttpResponse(data, status=200, content_type=content_type) + try: + send_mail('您的好友通过SeaCloud分享了一个文件给您', + t.render(Context(c)), None, [to_email], + fail_silently=False) + except: + data = json.dumps({'error':u'发送失败'}) + return HttpResponse(data, status=500, content_type=content_type) + + data = json.dumps("success") + messages.add_message(request, messages.INFO, u'发送成功') + return HttpResponse(data, status=200, content_type=content_type) + else: + return HttpResponseBadRequest(json.dumps(form.errors), + content_type=content_type) def document_prepare(raw_path, obj_id, doctype): curl = DOCUMENT_CONVERTOR_ROOT + 'convert'