Commit 85b98e0af1e2dde046e1cc44f2299cb52f1e302c

Authored by Erik Zambom
Committed by GitHub
2 parents c1e441bd 9b2d9e49

Merge pull request #472 from amadeusproject/refactoring

Daily Update - 13_04_2017
amadeus/static/js/printThis.js 0 → 100644
... ... @@ -0,0 +1,304 @@
  1 +/*
  2 + * printThis v1.10.0
  3 + * @desc Printing plug-in for jQuery
  4 + * @author Jason Day
  5 + *
  6 + * Resources (based on) :
  7 + * jPrintArea: http://plugins.jquery.com/project/jPrintArea
  8 + * jqPrint: https://github.com/permanenttourist/jquery.jqprint
  9 + * Ben Nadal: http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
  10 + *
  11 + * Licensed under the MIT licence:
  12 + * http://www.opensource.org/licenses/mit-license.php
  13 + *
  14 + * (c) Jason Day 2015
  15 + *
  16 + * Usage:
  17 + *
  18 + * $("#mySelector").printThis({
  19 + * debug: false, // show the iframe for debugging
  20 + * importCSS: true, // import page CSS
  21 + * importStyle: false, // import style tags
  22 + * printContainer: true, // grab outer container as well as the contents of the selector
  23 + * loadCSS: "path/to/my.css", // path to additional css file - us an array [] for multiple
  24 + * pageTitle: "", // add title to print page
  25 + * removeInline: false, // remove all inline styles from print elements
  26 + * printDelay: 333, // variable print delay
  27 + * header: null, // prefix to html
  28 + * footer: null, // postfix to html
  29 + * base: false, // preserve the BASE tag, or accept a string for the URL
  30 + * formValues: true, // preserve input/form values
  31 + * canvas: false, // copy canvas elements (experimental)
  32 + * doctypeString: '...', // enter a different doctype for older markup
  33 + * removeScripts: false // remove script tags from print content
  34 + * });
  35 + *
  36 + * Notes:
  37 + * - the loadCSS will load additional css (with or without @media print) into the iframe, adjusting layout
  38 + */
  39 +;
  40 +(function($) {
  41 +
  42 + function appendContent($el, content) {
  43 + if (!content) return;
  44 +
  45 + // Simple test for a jQuery element
  46 + $el.append(content.jquery ? content.clone() : content);
  47 + }
  48 +
  49 + function appendBody($body, $element, opt) {
  50 + // Clone for safety and convenience
  51 + var $content = $element.clone();
  52 +
  53 + if (opt.removeScripts) {
  54 + $content.find('script').remove();
  55 + }
  56 +
  57 + if (opt.printContainer) {
  58 + // grab $.selector as container
  59 + $body.append($("<div/>").html($content).html());
  60 + } else {
  61 + // otherwise just print interior elements of container
  62 + $content.each(function() {
  63 + $body.append($(this).html());
  64 + });
  65 + }
  66 + }
  67 +
  68 + var opt;
  69 + $.fn.printThis = function(options) {
  70 + opt = $.extend({}, $.fn.printThis.defaults, options);
  71 + var $element = this instanceof jQuery ? this : $(this);
  72 +
  73 + var strFrameName = "printThis-" + (new Date()).getTime();
  74 +
  75 + if (window.location.hostname !== document.domain && navigator.userAgent.match(/msie/i)) {
  76 + // Ugly IE hacks due to IE not inheriting document.domain from parent
  77 + // checks if document.domain is set by comparing the host name against document.domain
  78 + var iframeSrc = "javascript:document.write(\"<head><script>document.domain=\\\"" + document.domain + "\\\";</s" + "cript></head><body></body>\")";
  79 + var printI = document.createElement('iframe');
  80 + printI.name = "printIframe";
  81 + printI.id = strFrameName;
  82 + printI.className = "MSIE";
  83 + document.body.appendChild(printI);
  84 + printI.src = iframeSrc;
  85 +
  86 + } else {
  87 + // other browsers inherit document.domain, and IE works if document.domain is not explicitly set
  88 + var $frame = $("<iframe id='" + strFrameName + "' name='printIframe' />");
  89 + $frame.appendTo("body");
  90 + }
  91 +
  92 + var $iframe = $("#" + strFrameName);
  93 +
  94 + // show frame if in debug mode
  95 + if (!opt.debug) $iframe.css({
  96 + position: "absolute",
  97 + width: "0px",
  98 + height: "0px",
  99 + left: "-600px",
  100 + top: "-600px"
  101 + });
  102 +
  103 + // $iframe.ready() and $iframe.load were inconsistent between browsers
  104 + setTimeout(function() {
  105 +
  106 + // Add doctype to fix the style difference between printing and render
  107 + function setDocType($iframe, doctype){
  108 + var win, doc;
  109 + win = $iframe.get(0);
  110 + win = win.contentWindow || win.contentDocument || win;
  111 + doc = win.document || win.contentDocument || win;
  112 + doc.open();
  113 + doc.write(doctype);
  114 + doc.close();
  115 + }
  116 +
  117 + if (opt.doctypeString){
  118 + setDocType($iframe, opt.doctypeString);
  119 + }
  120 +
  121 + var $doc = $iframe.contents(),
  122 + $head = $doc.find("head"),
  123 + $body = $doc.find("body"),
  124 + $base = $('base'),
  125 + baseURL;
  126 +
  127 + // add base tag to ensure elements use the parent domain
  128 + if (opt.base === true && $base.length > 0) {
  129 + // take the base tag from the original page
  130 + baseURL = $base.attr('href');
  131 + } else if (typeof opt.base === 'string') {
  132 + // An exact base string is provided
  133 + baseURL = opt.base;
  134 + } else {
  135 + // Use the page URL as the base
  136 + baseURL = document.location.protocol + '//' + document.location.host;
  137 + }
  138 +
  139 + $head.append('<base href="' + baseURL + '">');
  140 +
  141 + // import page stylesheets
  142 + if (opt.importCSS) $("link[rel=stylesheet]").each(function() {
  143 + var href = $(this).attr("href");
  144 + if (href) {
  145 + var media = $(this).attr("media") || "all";
  146 + $head.append("<link type='text/css' rel='stylesheet' href='" + href + "' media='" + media + "'>");
  147 + }
  148 + });
  149 +
  150 + // import style tags
  151 + if (opt.importStyle) $("style").each(function() {
  152 + $(this).clone().appendTo($head);
  153 + });
  154 +
  155 + // add title of the page
  156 + if (opt.pageTitle) $head.append("<title>" + opt.pageTitle + "</title>");
  157 +
  158 + // import additional stylesheet(s)
  159 + if (opt.loadCSS) {
  160 + if ($.isArray(opt.loadCSS)) {
  161 + jQuery.each(opt.loadCSS, function(index, value) {
  162 + $head.append("<link type='text/css' rel='stylesheet' href='" + this + "'>");
  163 + });
  164 + } else {
  165 + $head.append("<link type='text/css' rel='stylesheet' href='" + opt.loadCSS + "'>");
  166 + }
  167 + }
  168 +
  169 + // print header
  170 + appendContent($body, opt.header);
  171 +
  172 + if (opt.canvas) {
  173 + // add canvas data-ids for easy access after cloning.
  174 + var canvasId = 0;
  175 + $element.find('canvas').each(function(){
  176 + $(this).attr('data-printthis', canvasId++);
  177 + });
  178 + }
  179 +
  180 + appendBody($body, $element, opt);
  181 +
  182 + if (opt.canvas) {
  183 + // Re-draw new canvases by referencing the originals
  184 + $body.find('canvas').each(function(){
  185 + var cid = $(this).data('printthis'),
  186 + $src = $('[data-printthis="' + cid + '"]');
  187 +
  188 + this.getContext('2d').drawImage($src[0], 0, 0);
  189 +
  190 + // Remove the markup from the original
  191 + $src.removeData('printthis');
  192 + });
  193 + }
  194 +
  195 + // capture form/field values
  196 + if (opt.formValues) {
  197 + // loop through inputs
  198 + var $input = $element.find('input');
  199 + if ($input.length) {
  200 + $input.each(function() {
  201 + var $this = $(this),
  202 + $name = $(this).attr('name'),
  203 + $checker = $this.is(':checkbox') || $this.is(':radio'),
  204 + $iframeInput = $doc.find('input[name="' + $name + '"]'),
  205 + $value = $this.val();
  206 +
  207 + // order matters here
  208 + if (!$checker) {
  209 + $iframeInput.val($value);
  210 + } else if ($this.is(':checked')) {
  211 + if ($this.is(':checkbox')) {
  212 + $iframeInput.attr('checked', 'checked');
  213 + } else if ($this.is(':radio')) {
  214 + $doc.find('input[name="' + $name + '"][value="' + $value + '"]').attr('checked', 'checked');
  215 + }
  216 + }
  217 +
  218 + });
  219 + }
  220 +
  221 + // loop through selects
  222 + var $select = $element.find('select');
  223 + if ($select.length) {
  224 + $select.each(function() {
  225 + var $this = $(this),
  226 + $name = $(this).attr('name'),
  227 + $value = $this.val();
  228 + $doc.find('select[name="' + $name + '"]').val($value);
  229 + });
  230 + }
  231 +
  232 + // loop through textareas
  233 + var $textarea = $element.find('textarea');
  234 + if ($textarea.length) {
  235 + $textarea.each(function() {
  236 + var $this = $(this),
  237 + $name = $(this).attr('name'),
  238 + $value = $this.val();
  239 + $doc.find('textarea[name="' + $name + '"]').val($value);
  240 + });
  241 + }
  242 + } // end capture form/field values
  243 +
  244 + // remove inline styles
  245 + if (opt.removeInline) {
  246 + // $.removeAttr available jQuery 1.7+
  247 + if ($.isFunction($.removeAttr)) {
  248 + $doc.find("body *").removeAttr("style");
  249 + } else {
  250 + $doc.find("body *").attr("style", "");
  251 + }
  252 + }
  253 +
  254 + // print "footer"
  255 + appendContent($body, opt.footer);
  256 +
  257 + setTimeout(function() {
  258 + if ($iframe.hasClass("MSIE")) {
  259 + // check if the iframe was created with the ugly hack
  260 + // and perform another ugly hack out of neccessity
  261 + window.frames["printIframe"].focus();
  262 + $head.append("<script> window.print(); </s" + "cript>");
  263 + } else {
  264 + // proper method
  265 + if (document.queryCommandSupported("print")) {
  266 + $iframe[0].contentWindow.document.execCommand("print", false, null);
  267 + } else {
  268 + $iframe[0].contentWindow.focus();
  269 + $iframe[0].contentWindow.print();
  270 + }
  271 + }
  272 +
  273 + // remove iframe after print
  274 + if (!opt.debug) {
  275 + setTimeout(function() {
  276 + $iframe.remove();
  277 + }, 1000);
  278 + }
  279 +
  280 + }, opt.printDelay);
  281 +
  282 + }, 333);
  283 +
  284 + };
  285 +
  286 + // defaults
  287 + $.fn.printThis.defaults = {
  288 + debug: false, // show the iframe for debugging
  289 + importCSS: true, // import parent page css
  290 + importStyle: false, // import style tags
  291 + printContainer: true, // print outer container/$.selector
  292 + loadCSS: "", // load an additional css file - load multiple stylesheets with an array []
  293 + pageTitle: "", // add title to print page
  294 + removeInline: false, // remove all inline styles
  295 + printDelay: 333, // variable print delay
  296 + header: null, // prefix to html
  297 + footer: null, // postfix to html
  298 + formValues: true, // preserve input/form values
  299 + canvas: false, // copy canvas content (experimental)
  300 + base: false, // preserve the BASE tag, or accept a string for the URL
  301 + doctypeString: '<!DOCTYPE html>', // html doctype
  302 + removeScripts: false // remove script tags before appending
  303 + };
  304 +})(jQuery);
... ...
amadeus/templates/base.html
... ... @@ -34,6 +34,8 @@
34 34 <script src="{% static 'js/crop.js' %}"></script> <!-- Js for cropper-->
35 35 <link href="{% static 'css/cropper.min.css' %}" rel="stylesheet"> <!-- CSS for cropper-->
36 36  
  37 + <script type="text/javascript" src="{% static 'js/printThis.js' %}"></script> <!-- Print this plugin js-->
  38 +
37 39  
38 40 <script type="text/javascript" src="{% static 'bootstrap-3.3.7/js/bootstrap.min.js' %}"></script>
39 41 <!--<script type="text/javascript" src="{% static 'js/bootstrap-acessibility2.min.js' %}"></script>-->
... ...
news/templates/news/delete.html 0 → 100644
... ... @@ -0,0 +1,23 @@
  1 +{% load static i18n permission_tags %}
  2 +
  3 +<!-- Modal (remember to change the ids!!!) -->
  4 +<div class="modal fade" id="news" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  5 + <div class="modal-dialog" role="document">
  6 + <div class="modal-content">
  7 + <!-- Modal Body -->
  8 + <div class="modal-body">
  9 + <!-- Put ONLY your content here!!! -->
  10 + <form id="delete_form" action="{% url 'news:delete' new.slug %}" method="post">
  11 + {% csrf_token %}
  12 + <p>{% trans 'Are you sure you want to delete the news' %} "{{new.title}}"? {% trans "All its data will be lost and can't be recovered" %}</p>
  13 + </form>
  14 + </div>
  15 + <!-- Modal Footer -->
  16 + <div id="delete-category-footer"class="modal-footer">
  17 + <!-- Don't remove that!!! -->
  18 + <button type="button" class="btn btn-default btn-raised" data-dismiss="modal">{% trans "Close" %}</button>
  19 + <button type="submit" id="button" form="delete_form" class="btn btn-success btn-raised erase-button">{% trans "Delete" %}</button>
  20 + </div>
  21 + </div>
  22 + </div>
  23 +</div>
... ...
news/templates/news/list.html
... ... @@ -28,10 +28,10 @@
28 28  
29 29 <div class="row">
30 30 <div class="col-md-9">
31   - <form action="{% url 'users:search' %}" method="GET" class="form-horizontal">
  31 + <form action="{% url 'news:search' %}" method="GET" class="form-horizontal">
32 32 <div class="form-group">
33 33 <div class="col-md-11 col-sm-11 col-xs-11">
34   - <input type="text" class="form-control" name="search" placeholder="{% trans 'Search...' %}" />
  34 + <input type="text" class="form-control" name="search" placeholder="{% trans 'Search by title, author or date' %}" />
35 35 </div>
36 36 <div class="col-md-1 col-sm-1 col-xs-1">
37 37 <button type="submit" class="btn btn-fab btn-fab-mini">
... ... @@ -57,7 +57,7 @@
57 57 <div class="col-md-6">
58 58 <div align="right">
59 59 <a href="{% url 'news:update' new.slug %}" class="btn btn-success btn-raised btn-sm"><i class="fa fa-edit"></i> {% trans 'Edit' %}</a>
60   - <a href="" class="btn btn-default btn-raised btn-sm"><i class="fa fa-trash"></i> {% trans 'Delete' %}</a>
  60 + <a href="javascript:delete_news.get('{% url 'news:delete' new.slug %}','#news','#modal_remove')" class="btn btn-default btn-raised btn-sm" class="btn btn-default btn-raised btn-sm"><i class="fa fa-trash"></i> {% trans 'Delete' %}</a>
61 61 </div>
62 62 </div>
63 63 </div>
... ... @@ -75,5 +75,21 @@
75 75 <div id="modal_remove">
76 76  
77 77 </div>
  78 +<script type="text/javascript">
  79 +var delete_news = {
  80 + get: function (url, id_modal, id_div_modal){
  81 + $.get(url, function(data){
  82 + if($(id_modal).length){
  83 + $(id_div_modal).empty();
  84 + $(id_div_modal).append(data);
  85 + } else {
  86 + $(id_div_modal).append(data);
  87 + }
  88 + $(id_modal).modal('show');
  89 + });
  90 + }
  91 +};
  92 +
  93 +</script>
78 94  
79 95 {% endblock %}
... ...
news/templates/news/search.html 0 → 100644
... ... @@ -0,0 +1,79 @@
  1 +{% extends 'news/list.html' %}
  2 +
  3 +{% load static i18n django_bootstrap_breadcrumbs permission_tags pagination %}
  4 +
  5 +{% block breadcrumbs %}
  6 + {{ block.super }}
  7 + {% trans 'Search News' as search_news %}
  8 + {% breadcrumb search_news 'news:search' %}
  9 +{% endblock %}
  10 +
  11 +{% block render_breadcrumbs %}
  12 + {% render_breadcrumbs %}
  13 +{% endblock %}
  14 +
  15 +{% block content %}
  16 +
  17 +{% if messages %}
  18 + {% for message in messages %}
  19 + <script type="text/javascript">
  20 + {% if message.tags == "danger" %}
  21 + alertify.error('{{message}}');
  22 + {% else %}
  23 + alertify.success('{{message}}');
  24 + {% endif %}
  25 + </script>
  26 + {% endfor %}
  27 +{% endif %}
  28 +
  29 +<div class="row">
  30 + <div class="col-md-9">
  31 + <form action="{% url 'news:search' %}" method="GET" class="form-horizontal">
  32 + <div class="form-group">
  33 + <div class="col-md-11 col-sm-11 col-xs-11">
  34 + <input type="text" class="form-control" name="search" placeholder="{% trans 'Search by title, author or date' %}" />
  35 + </div>
  36 + <div class="col-md-1 col-sm-1 col-xs-1">
  37 + <button type="submit" class="btn btn-fab btn-fab-mini">
  38 + <i class="fa fa-search"></i>
  39 + </button>
  40 + </div>
  41 + </div>
  42 + </form>
  43 + </div>
  44 + <div class="col-md-3">
  45 + <a href="{% url 'news:create' %}" class="pull-right btn btn-success btn-raised btn-md"><i class="fa fa-plus"></i> {% trans 'Create News' %}</a>
  46 + </div>
  47 +</div>
  48 +
  49 +{% if news %}
  50 + {% for new in news %}
  51 + <div class="row-fluid panel panel-default">
  52 + <div class="panel-body">
  53 + <div class="col-md-6">
  54 + <a href="{% url 'news:view' new.slug %}"><h3> <b>{{ new }}</b> </h3></a>
  55 + <p>{{ new.creator}}, em {{ new.create_date }}</p>
  56 + </div>
  57 + <div class="col-md-6">
  58 + <div align="right">
  59 + <a href="{% url 'news:update' new.slug %}" class="btn btn-success btn-raised btn-sm"><i class="fa fa-edit"></i> {% trans 'Edit' %}</a>
  60 + <a href="" class="btn btn-default btn-raised btn-sm"><i class="fa fa-trash"></i> {% trans 'Delete' %}</a>
  61 + </div>
  62 + </div>
  63 + </div>
  64 +
  65 + </div>
  66 + {% endfor %}
  67 + {% pagination request paginator page_obj %}
  68 +{% else %}
  69 + <div class="row">
  70 + <div class="col-md-12 col-sm-12 col-xs-12">
  71 + <p>{% trans 'No news found' %}</p>
  72 + </div>
  73 + </div>
  74 +{% endif %}
  75 +<div id="modal_remove">
  76 +
  77 +</div>
  78 +
  79 +{% endblock %}
... ...
news/templates/news/view.html
... ... @@ -25,11 +25,11 @@
25 25 </script>
26 26 {% endfor %}
27 27 {% endif %}
28   -<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">
29   - <div class="col-lg-12 row-fluid panel panel-default">
30   - <div class="panel-body">
  28 +
  29 + <div class="row-fluid panel panel-default" >
  30 + <div class="panel-body" id="printArea">
31 31 <div class="col-md-12" style="display:inline-block">
32   - <h2><b>{{new.title}}</b></h2>
  32 + <h2><b>{{new.title}}</b></h2> <button style="text-transform:none; text-decoration: underline;" type="button" class="pull-right btn btn-default btn-sm" id="printButton" name="button">{% trans "Print News" %} <i class="fa fa-print" aria-hidden="true"></i></button><br>
33 33 <p>{{new.creator}}, em {{new.create_date}}</p>
34 34 </div>
35 35 <div class="col-md-12">
... ... @@ -43,6 +43,12 @@
43 43  
44 44 </div>
45 45  
  46 + <script type="text/javascript" src="{% static 'js/printThis.js' %}"></script>
  47 + <script type="text/javascript">
  48 + $('#printButton').click(function(){
  49 + $("#printArea").printThis({removeInline:true,importStyle: true, });
  50 + });
  51 +
  52 + </script>
46 53  
47   -</div>
48 54 {% endblock %}
... ...
news/urls.py
... ... @@ -6,5 +6,7 @@ urlpatterns = [
6 6 url(r'^view/(?P<slug>[\w_-]+)/$', views.VisualizeNews.as_view(), name = 'view'),
7 7 url(r'^create/$', views.CreateNewsView.as_view(), name='create'),
8 8 url(r'^update/(?P<slug>[\w_-]+)/$', views.UpdateNewsView.as_view(), name = 'update'),
  9 + url(r'^search/$', views.SearchNewsView.as_view(), name = 'search'),
  10 + url(r'^delete/(?P<slug>[\w_-]+)/$', views.DeleteNewsView.as_view(), name='delete'),
9 11  
10 12 ]
... ...
news/views.py
1   -from django.shortcuts import render
  1 +from django.shortcuts import get_object_or_404, redirect, render
2 2 from django.views import generic
3 3 from django.contrib.auth.mixins import LoginRequiredMixin
4 4 from log.models import Log
... ... @@ -6,7 +6,7 @@ from log.mixins import LogMixin
6 6 from django.core.urlresolvers import reverse, reverse_lazy
7 7 from django.contrib import messages
8 8 from django.utils.translation import ugettext_lazy as _
9   -
  9 +from django.db.models import Q, Count
10 10  
11 11 from .models import News
12 12 from .forms import NewsForm
... ... @@ -101,3 +101,70 @@ class UpdateNewsView(LoginRequiredMixin,LogMixin,generic.UpdateView):
101 101 self.object.save()
102 102  
103 103 return super(UpdateNewsView, self).form_valid(form)
  104 +
  105 +class SearchNewsView(LoginRequiredMixin, LogMixin, generic.ListView):
  106 + login_url = reverse_lazy("users:login")
  107 + redirect_field_name = 'next'
  108 +
  109 + template_name = 'news/search.html'
  110 + context_object_name = 'news'
  111 + paginate_by = 10
  112 +
  113 + def dispatch(self, request, *args, **kwargs):
  114 + search = self.request.GET.get('search', '')
  115 +
  116 + if search == '':
  117 + return redirect(reverse_lazy('news:manage_news'))
  118 +
  119 + return super(SearchNewsView, self).dispatch(request, *args, **kwargs)
  120 +
  121 + def get_queryset(self):
  122 + inteiro = False
  123 +
  124 + search = self.request.GET.get('search', '')
  125 + print(type(search))
  126 + try:
  127 + search = int(search)
  128 + inteiro = True
  129 + except Exception as e:
  130 + inteiro = False
  131 +
  132 + if inteiro:
  133 + news = News.objects.filter(Q(title__icontains = search) | Q(creator__username__icontains = search) | Q(create_date__icontains = search) | Q(create_date__year = search) | Q(create_date__month = search) | Q(create_date__day = search) ).distinct().order_by('create_date')
  134 + else:
  135 + news = News.objects.filter(Q(title__icontains = search) | Q(creator__username__icontains = search) | Q(create_date__icontains = search) ).distinct().order_by('create_date')
  136 +
  137 +
  138 + return news
  139 +
  140 + def get_context_data (self, **kwargs):
  141 + context = super(SearchNewsView, self).get_context_data(**kwargs)
  142 + context['title'] = _('Search News')
  143 + context['search'] = self.request.GET.get('search')
  144 +
  145 + return context
  146 +
  147 +class DeleteNewsView(LoginRequiredMixin,LogMixin,generic.DeleteView):
  148 + login_url = reverse_lazy("users:login")
  149 + redirect_field_name = 'next'
  150 +
  151 + model = News
  152 + template_name = 'news/delete.html'
  153 +
  154 + def delete(self, request, *args, **kwargs):
  155 + news = get_object_or_404(News, slug = self.kwargs.get('slug'))
  156 + return super(DeleteNewsView, self).delete(self, request, *args, **kwargs)
  157 +
  158 + def get_success_url(self):
  159 + messages.success(self.request, _('News "%s" removed successfully!')%(self.object.title))
  160 + success_url = reverse_lazy('news:manage_news')
  161 +
  162 + return success_url
  163 +
  164 + def get_context_data(self, **kwargs):
  165 + context = super(DeleteNewsView, self).get_context_data(**kwargs)
  166 + context['title'] = _('Delete News')
  167 + news = get_object_or_404(News, slug = self.kwargs.get('slug'))
  168 + context['new'] = news
  169 +
  170 + return context
... ...
users/forms.py
... ... @@ -81,18 +81,19 @@ class RegisterUserForm(Validation):
81 81  
82 82 if self.instance.image :
83 83 image = Image.open(self.instance.image)
84   - cropped_image = image.crop((x, y, w+x, h+y))
85   - resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
  84 + if not x is None:
  85 + cropped_image = image.crop((x, y, w+x, h+y))
  86 + resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
86 87  
87   - folder_path = join(settings.MEDIA_ROOT, 'users')
88   - #check if the folder already exists
89   - if not os.path.isdir(folder_path):
90   - os.makedirs(folder_path)
  88 + folder_path = join(settings.MEDIA_ROOT, 'users')
  89 + #check if the folder already exists
  90 + if not os.path.isdir(folder_path):
  91 + os.makedirs(folder_path)
91 92  
92   - if ("users" not in self.instance.image.path):
93   - self.deletepath = self.instance.image.path
  93 + if ("users" not in self.instance.image.path):
  94 + self.deletepath = self.instance.image.path
94 95  
95   - resized_image.save(self.instance.image.path)
  96 + resized_image.save(self.instance.image.path)
96 97  
97 98 self.instance.set_password(self.cleaned_data['new_password'])
98 99  
... ... @@ -128,21 +129,22 @@ class ProfileForm(Validation):
128 129 y = self.cleaned_data.get('y')
129 130 w = self.cleaned_data.get('width')
130 131 h = self.cleaned_data.get('height')
131   -
  132 +
132 133 if self.instance.image:
133 134 image = Image.open(self.instance.image)
134   - cropped_image = image.crop((x, y, w+x, h+y))
135   - resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
  135 + if not x is None:
  136 + cropped_image = image.crop((x, y, w+x, h+y))
  137 + resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
136 138  
137   - folder_path = join(settings.MEDIA_ROOT, 'users')
138   - #check if the folder already exists
139   - if not os.path.isdir(folder_path):
140   - os.makedirs(folder_path)
  139 + folder_path = join(settings.MEDIA_ROOT, 'users')
  140 + #check if the folder already exists
  141 + if not os.path.isdir(folder_path):
  142 + os.makedirs(folder_path)
141 143  
142   - if ("users" not in self.instance.image.path):
143   - self.deletepath = self.instance.image.path
  144 + if ("users" not in self.instance.image.path):
  145 + self.deletepath = self.instance.image.path
144 146  
145   - resized_image.save(self.instance.image.path)
  147 + resized_image.save(self.instance.image.path)
146 148  
147 149 self.instance.save()
148 150 if (self.deletepath):
... ... @@ -181,7 +183,7 @@ class UserForm(Validation):
181 183  
182 184 def save(self, commit=True):
183 185 super(UserForm, self).save(commit=False)
184   - self.deletepath = ""
  186 + self.deletepath = ""
185 187  
186 188 x = self.cleaned_data.get('x')
187 189 y = self.cleaned_data.get('y')
... ... @@ -190,18 +192,19 @@ class UserForm(Validation):
190 192  
191 193 if self.instance.image :
192 194 image = Image.open(self.instance.image)
193   - cropped_image = image.crop((x, y, w+x, h+y))
194   - resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
  195 + if not x is None:
  196 + cropped_image = image.crop((x, y, w+x, h+y))
  197 + resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
195 198  
196   - folder_path = join(settings.MEDIA_ROOT, 'users')
197   - #check if the folder already exists
198   - if not os.path.isdir(folder_path):
199   - os.makedirs(folder_path)
  199 + folder_path = join(settings.MEDIA_ROOT, 'users')
  200 + #check if the folder already exists
  201 + if not os.path.isdir(folder_path):
  202 + os.makedirs(folder_path)
200 203  
201   - if ("users" not in self.instance.image.path):
202   - self.deletepath = self.instance.image.path
  204 + if ("users" not in self.instance.image.path):
  205 + self.deletepath = self.instance.image.path
203 206  
204   - resized_image.save(self.instance.image.path)
  207 + resized_image.save(self.instance.image.path)
205 208  
206 209  
207 210 if not self.is_edit or self.cleaned_data['new_password'] != '':
... ...