Commit 5b5d6594c6e02b76a1e28dd1e04b0648717eb751

Authored by Zambom
1 parent 355c051a

Chat bug fixes - part 1

amadeus/static/js/chat.js
@@ -119,10 +119,33 @@ function getForm(field) { @@ -119,10 +119,33 @@ function getForm(field) {
119 } 119 }
120 120
121 function setChatFormSubmit() { 121 function setChatFormSubmit() {
122 - var frm = $('#chat-form'); 122 + var frm = $('#chat-form'),
  123 + error_msg = frm.data('error');
123 124
124 frm.submit(function () { 125 frm.submit(function () {
125 - var formData = new FormData($(this)[0]); 126 + var formData = new FormData($(this)[0]),
  127 + file = $("#id_image")[0].files[0],
  128 + max_filesize = 5*1024*1024;
  129 +
  130 + if (typeof(file) != "undefined") {
  131 + var image_container = $("#id_image").parent(),
  132 + overlay_msg = image_container.data('overlay'),
  133 + wrong_format_msg = image_container.data('invalid'),
  134 + file_type = file.type,
  135 + type_accept = /^image\/(jpg|png|jpeg|gif)$/;
  136 +
  137 + if (file.size > max_filesize) {
  138 + image_container.append('<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><ul><li>' + overlay_msg + '</li></ul></div>');
  139 +
  140 + return false;
  141 + }
  142 +
  143 + if (!type_accept.test(file_type)) {
  144 + image_container.append('<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><ul><li>' + wrong_format_msg + '</li></ul></div>');
  145 +
  146 + return false;
  147 + }
  148 + }
126 149
127 $.ajax({ 150 $.ajax({
128 type: frm.attr('method'), 151 type: frm.attr('method'),
@@ -152,7 +175,12 @@ function setChatFormSubmit() { @@ -152,7 +175,12 @@ function setChatFormSubmit() {
152 $('#chat-modal-form').modal('hide'); 175 $('#chat-modal-form').modal('hide');
153 }, 176 },
154 error: function(data) { 177 error: function(data) {
155 - $("#chat-modal-form").html(data.responseText); 178 + if (data.status == 400) {
  179 + $("#chat-modal-form").html(data.responseText);
  180 + } else {
  181 + alertify.error(error_msg);
  182 + }
  183 +
156 setChatFormSubmit(); 184 setChatFormSubmit();
157 }, 185 },
158 cache: false, 186 cache: false,
chat/models.py
@@ -13,7 +13,7 @@ def validate_img_extension(value): @@ -13,7 +13,7 @@ def validate_img_extension(value):
13 13
14 if hasattr(value.file, 'content_type'): 14 if hasattr(value.file, 'content_type'):
15 if not value.file.content_type in valid_formats: 15 if not value.file.content_type in valid_formats:
16 - raise ValidationError(_('File not supported.')) 16 + raise ValidationError(_('Select a valid file. The file must posses one of this extensions: .jpg, .png, .gif'))
17 17
18 def upload_filename(instance, filename): 18 def upload_filename(instance, filename):
19 path = "chat/" 19 path = "chat/"
@@ -26,7 +26,7 @@ class Conversation(models.Model): @@ -26,7 +26,7 @@ class Conversation(models.Model):
26 user_two = models.ForeignKey(User, verbose_name = _('User Two'), related_name = 'talk_user_end') 26 user_two = models.ForeignKey(User, verbose_name = _('User Two'), related_name = 'talk_user_end')
27 27
28 class TalkMessages(models.Model): 28 class TalkMessages(models.Model):
29 - text = models.TextField(_('Comment'), blank = True) 29 + text = models.TextField(_('Message'), blank = True)
30 image = models.ImageField(verbose_name = _('Image'), null = True, blank = True, upload_to = upload_filename, validators = [validate_img_extension]) 30 image = models.ImageField(verbose_name = _('Image'), null = True, blank = True, upload_to = upload_filename, validators = [validate_img_extension])
31 talk = models.ForeignKey(Conversation, verbose_name = _('Conversation'), related_name = 'message_talk', null = True) 31 talk = models.ForeignKey(Conversation, verbose_name = _('Conversation'), related_name = 'message_talk', null = True)
32 user = models.ForeignKey(User, verbose_name = _('User'), related_name = 'message_user', null = True) 32 user = models.ForeignKey(User, verbose_name = _('User'), related_name = 'message_user', null = True)
chat/templates/chat/_form.html
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 <div class="modal-dialog" role="document"> 4 <div class="modal-dialog" role="document">
5 <div class="modal-content"> 5 <div class="modal-content">
6 <div class="modal-body"> 6 <div class="modal-body">
7 - <form id="chat-form" method="post" action="{{ form_url }}" enctype="multipart/form-data"> 7 + <form id="chat-form" method="post" action="{{ form_url }}" enctype="multipart/form-data" data-error="{% trans 'Something went wrong. Please try again later.' %}">
8 {% csrf_token %} 8 {% csrf_token %}
9 9
10 <div class="form-group{% if form.has_error %} has-error {% endif %}"> 10 <div class="form-group{% if form.has_error %} has-error {% endif %}">
@@ -27,22 +27,22 @@ @@ -27,22 +27,22 @@
27 {% endif %} 27 {% endif %}
28 </div> 28 </div>
29 29
30 - <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> 30 + <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput" data-overlay="{% trans 'The image is too large. It should have less than 5MB.' %}" data-invalid="{% trans 'Select a valid file. The file must posses one of this extensions: .jpg, .png, .gif' %}">
31 {% render_field form.image %} 31 {% render_field form.image %}
32 32
33 <div class="filedrag"> 33 <div class="filedrag">
34 - {% trans 'Click or drop the file here' %}<br /> 34 + {% trans 'Click or drop the image here' %}<br />
35 35
36 - <small>{% trans 'The file could not exceed 5MB.' %}</small> 36 + <small>{% trans 'The image could not exceed 5MB.' %}</small>
37 </div> 37 </div>
38 38
39 <span id="helpBlock" class="help-block">{{ form.image.help_text }}</span> 39 <span id="helpBlock" class="help-block">{{ form.image.help_text }}</span>
40 40
41 {% if form.image.errors %} 41 {% if form.image.errors %}
42 <div class="alert alert-danger alert-dismissible" role="alert"> 42 <div class="alert alert-danger alert-dismissible" role="alert">
43 - <button type="button" class="close" data-dismiss="alert" aria-label="Close">  
44 - <span aria-hidden="true">&times;</span>  
45 - </button> 43 + <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  44 + <span aria-hidden="true">&times;</span>
  45 + </button>
46 <ul> 46 <ul>
47 {% for error in form.image.errors %} 47 {% for error in form.image.errors %}
48 <li>{{ error }}</li> 48 <li>{{ error }}</li>