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 119 }
120 120  
121 121 function setChatFormSubmit() {
122   - var frm = $('#chat-form');
  122 + var frm = $('#chat-form'),
  123 + error_msg = frm.data('error');
123 124  
124 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 150 $.ajax({
128 151 type: frm.attr('method'),
... ... @@ -152,7 +175,12 @@ function setChatFormSubmit() {
152 175 $('#chat-modal-form').modal('hide');
153 176 },
154 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 184 setChatFormSubmit();
157 185 },
158 186 cache: false,
... ...
chat/models.py
... ... @@ -13,7 +13,7 @@ def validate_img_extension(value):
13 13  
14 14 if hasattr(value.file, 'content_type'):
15 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 18 def upload_filename(instance, filename):
19 19 path = "chat/"
... ... @@ -26,7 +26,7 @@ class Conversation(models.Model):
26 26 user_two = models.ForeignKey(User, verbose_name = _('User Two'), related_name = 'talk_user_end')
27 27  
28 28 class TalkMessages(models.Model):
29   - text = models.TextField(_('Comment'), blank = True)
  29 + text = models.TextField(_('Message'), blank = True)
30 30 image = models.ImageField(verbose_name = _('Image'), null = True, blank = True, upload_to = upload_filename, validators = [validate_img_extension])
31 31 talk = models.ForeignKey(Conversation, verbose_name = _('Conversation'), related_name = 'message_talk', null = True)
32 32 user = models.ForeignKey(User, verbose_name = _('User'), related_name = 'message_user', null = True)
... ...
chat/templates/chat/_form.html
... ... @@ -4,7 +4,7 @@
4 4 <div class="modal-dialog" role="document">
5 5 <div class="modal-content">
6 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 8 {% csrf_token %}
9 9  
10 10 <div class="form-group{% if form.has_error %} has-error {% endif %}">
... ... @@ -27,22 +27,22 @@
27 27 {% endif %}
28 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 31 {% render_field form.image %}
32 32  
33 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 37 </div>
38 38  
39 39 <span id="helpBlock" class="help-block">{{ form.image.help_text }}</span>
40 40  
41 41 {% if form.image.errors %}
42 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 46 <ul>
47 47 {% for error in form.image.errors %}
48 48 <li>{{ error }}</li>
... ...