From 5b5d6594c6e02b76a1e28dd1e04b0648717eb751 Mon Sep 17 00:00:00 2001 From: Zambom Date: Mon, 3 Apr 2017 22:31:58 -0300 Subject: [PATCH] Chat bug fixes - part 1 --- amadeus/static/js/chat.js | 34 +++++++++++++++++++++++++++++++--- chat/models.py | 4 ++-- chat/templates/chat/_form.html | 14 +++++++------- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/amadeus/static/js/chat.js b/amadeus/static/js/chat.js index 17410cb..1c51cf0 100644 --- a/amadeus/static/js/chat.js +++ b/amadeus/static/js/chat.js @@ -119,10 +119,33 @@ function getForm(field) { } function setChatFormSubmit() { - var frm = $('#chat-form'); + var frm = $('#chat-form'), + error_msg = frm.data('error'); frm.submit(function () { - var formData = new FormData($(this)[0]); + var formData = new FormData($(this)[0]), + file = $("#id_image")[0].files[0], + max_filesize = 5*1024*1024; + + if (typeof(file) != "undefined") { + var image_container = $("#id_image").parent(), + overlay_msg = image_container.data('overlay'), + wrong_format_msg = image_container.data('invalid'), + file_type = file.type, + type_accept = /^image\/(jpg|png|jpeg|gif)$/; + + if (file.size > max_filesize) { + image_container.append(''); + + return false; + } + + if (!type_accept.test(file_type)) { + image_container.append(''); + + return false; + } + } $.ajax({ type: frm.attr('method'), @@ -152,7 +175,12 @@ function setChatFormSubmit() { $('#chat-modal-form').modal('hide'); }, error: function(data) { - $("#chat-modal-form").html(data.responseText); + if (data.status == 400) { + $("#chat-modal-form").html(data.responseText); + } else { + alertify.error(error_msg); + } + setChatFormSubmit(); }, cache: false, diff --git a/chat/models.py b/chat/models.py index 5e92986..3ac8da0 100644 --- a/chat/models.py +++ b/chat/models.py @@ -13,7 +13,7 @@ def validate_img_extension(value): if hasattr(value.file, 'content_type'): if not value.file.content_type in valid_formats: - raise ValidationError(_('File not supported.')) + raise ValidationError(_('Select a valid file. The file must posses one of this extensions: .jpg, .png, .gif')) def upload_filename(instance, filename): path = "chat/" @@ -26,7 +26,7 @@ class Conversation(models.Model): user_two = models.ForeignKey(User, verbose_name = _('User Two'), related_name = 'talk_user_end') class TalkMessages(models.Model): - text = models.TextField(_('Comment'), blank = True) + text = models.TextField(_('Message'), blank = True) image = models.ImageField(verbose_name = _('Image'), null = True, blank = True, upload_to = upload_filename, validators = [validate_img_extension]) talk = models.ForeignKey(Conversation, verbose_name = _('Conversation'), related_name = 'message_talk', null = True) user = models.ForeignKey(User, verbose_name = _('User'), related_name = 'message_user', null = True) diff --git a/chat/templates/chat/_form.html b/chat/templates/chat/_form.html index b96e09b..0cd1d14 100644 --- a/chat/templates/chat/_form.html +++ b/chat/templates/chat/_form.html @@ -4,7 +4,7 @@