Friday 27 May 2016

How to Create Excel file with Images and Trigger to Download in Django.



This Article is contains how we can attach the images in excel sheet and trigger to download the same in django project.

Packages we need ..

Install   :  pip install xlsxwriter

Dont Forget To import the following packages in your views

from django.shortcuts import render
from django.http import HttpResponse
from excel_response import ExcelResponse
import datetime as date_time
from django.utils.html import strip_tags
import io
from xlsxwriter.workbook import Workbook
import urllib2
import base64

Here "Product" Is model

@login_required
def download_excel(request,p_id):

    if p_id:
        output = io.BytesIO()
        workbook = Workbook(output, {'in_memory': True})
        bold = workbook.add_format({'bold': True})
        worksheet = workbook.add_worksheet()
        merge_format = workbook.add_format({
           'bold': 1,
           'border': 1,
           'align': 'center',
           'valign': 'vcenter',
           'fg_color': 'yellow'})
        product = Product.objects.get(id=p_id)
        worksheet.merge_range('A2:M2',"Product Name :"+product.name, merge_format)
        heading = ['ProductName','Product Upc','category','Manufacturer','Brand',\
                   'Product Style Number','Length','Width','Height','Weight','Color',\
                   'Size','Description']
        for counter,head in enumerate(heading):
            worksheet.write(4,counter,head,merge_format)
        category = product.category.all()
        for cat in category:
            cat_name = cat.name
            colour = []
        for clr in product.color.all():
            cr = clr.color
            colour.append(cr)
        size = []
        for si in product.size.all():
            s = si.size
            size.append(s)
        html = product.description
        discription = strip_tags(html)
        content = [product.name,product.upc,\
                   cat_name,product.brand.manufacturer.name,\
                   product.brand.name,product.style_number,\
                   product.length +':'+product.length_mi,\
                   product.width +':'+product.length_mi,\
                   product.height+':'+product.length_mi,\
                   product.weight+':'+product.weight_mi,\
                   colour,size,discription]
        for counter,data in enumerate(content):
            worksheet.write(6,counter,''.join(data))
            worksheet.write(4,13,'NetWork Price',merge_format)
            worksheet.write(6,13,product.network_price)
            worksheet.write('A10:M10', 'Product Images:',merge_format)
        gallery = []
        row = 7
        col = 0
        for image in product.gallery.all():
            url = settings.SITE_URL + image.image_thumbnail.url
            image_data = io.BytesIO(urllib2.urlopen(url).read())
            gallery.append(image_data)
        for counter,image in enumerate(gallery):
            row += 10+counter
        worksheet.insert_image(row,col, url, {'image_data': image,'x_offset': 15, 'y_offset': 10})
        workbook.close()
        output.seek(0)
        response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        response['Content-Disposition'] = "attachment; filename=%s.xlsx" %product.upc
    return response

The Best Django Haystack and Elasticsearch Tutorial



Search is one of the most basic and major requirements of any modern web-app. Search helps your users find the relevant content, articles, products etc. I have been developing web application for a decade now and frankly I have not seen a single web app that does not implement search. Without the search functionality an app’s user experience goes for a toss as most of the users would not be able to reach the content on your app that they are looking for and in such a case it is very unlikely that they’ll ever return back to your app. With that being said, today’s article will show how you can setup search functionality in your django web app using haystack and elastic search. Let’s Look into what role each of these play.

The Django Framework

Django framework is one of the most popular web application development frameworks written in python to develop amazing we application within the deadlines. If you have reached this article, I assume you already have a fair knowledge about django and how it works. So we are not gonna go in details and straight-up move to the next to applications that we’d required to implement search functionality in our django app.

Elastic Search

Elasticsearch is a lucene based search server that offers full text search and supports schema-less JSON Documents. In Laymen terms, this is the search engine that indexes (stores) the searchable content for your web application. Elasticsearch is written in Java and can be used as a stand-alone service. It offers an API that can be used to communicate to the search server over HTTP.

Haystack

Haystack is a django app that acts as a wrapper over multiple search backends (servers) including elasticsearch, solr etc. Different search server may work differently and communicate differently and also serve the search results differently. Haystack implements generic methods and classes that can be used with multiple search servers. So for instance say you have been using elasticsearch for your web application but due to some requirement you need to switch to another search server (for example Solr). Just imagine all the work you’d require to put in to change the entire search flow just because you chose to try another search engine. With haystack it would only take a one line setting change and your search would work as it currently does.

Now we have a basic idea about what django elasticsearch and haystack are and what roles do they play. So let’s put them all together and I promise you it wouldn’t take more than 10 mins to have full text search up and running for your django web application.

Django Haystack and Elasticsearch

I am assuming that you already have a django app to work on so we are gonna dive directly into setting up haystack and elasticsearch. Let’s first install haystack using pip.

pip install django-haystack

The above will install the latest stable release of the django-haystack package. In case you are looking to install the development version of haystack you can use the following command rather.


pip install -e git+https://github.com/toastdriven/django-haystack.git@master#egg=django-haystack


once you have installed django-haystack, add it to your INSTALLED_APPS in your django project’s settings file.


INSTALLED_APPS = [
    ...
    'haystack',

]


Now let’s download, unzip and run elasticsearch before we configure haystack to use elasticsearch. You can download the latest version of elasticsearch from the official elasticsearch website. Once you have downloaded the latest package, unzip it at a desired location on your computer. For this example I am going to extract it in ~/es folder.


mkdir ~/es
mv ~/Downloads/elasticsearch-5.0.0-alpha2.tar.gz ~/es
cd ~/es
tar -xzvf elasticsearch-5.0.0-alpha2.tar.gz
cd elasticsearch-5.0.0

Once the files from the package have been extracted you can simply start your elasticsearch server using the following command.


./bin/elasticsearch -d



Once elasticsearch has been started you can confirm it by going to http://127.0.0.1:9200/ in your web browser. If elasticsearch has been successfully started, you’ll see some information like the following.

elasticsearch

Great! Now let’s tell haystack that we want to use the elasticsearch server to index our searchable content. In order to do so we’ll need to define a backends setting for haystack in our projects settings.py file.


HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'products',
    },
}

Add the above to your settings.py file. the ENGINE parameter in the above configuration tells hasytack which backend engine to use. In our case we set it to elasticsearch. INDEX_NAME defines the default index name that you would like to store your data in and query your data from. The URL configuration option tells haystack the address where elasticsearch will be available.



Well, now we have the basic setup up and running. Let’s add some data to our search index. You can do this according to your project. For this example I am going to index the data from a model name Product from my app called catalog.

This is how my product model looks like.


from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=244)
    description = models.TextField()

    def __unicode__(self):
        return self.name

In order to store my products data into elasticsearch, I am required to create a search_indexes.py file in the application where my models reside. So I will create the file first.


touch apps/catalog/search_indexes.py

Now I will create a ProductIndex in this file to tell haystack, what data from my Product model I want to store in the search engine.


from haystack import indexes

from apps.catalog.models import Product


class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    name = indexes.CharField(model_attr='name')
    description = indexes.CharField(model_attr='description')

    def get_model(self):
        return Product

    def index_queryset(self, using=None):
        return self.get_model().objects.all()


As we notified haystack through our product index that we’d like to use a template for our text field. We’ll now create it. In your main templates directory create a file called search/indexes/catalog/product_text.txt. Make sure you change the path to use your own app name and index name. So if your app is called blog and your model & Index are called Post and PostIndex respectively then you should create a file called search/indexes/blog/post_text.txt

now we’ll add the searchable information in the template file we just created.


{{ object.name }}
{{ object.description }}


As I had only two fields and I wanted to use both in the search template I added them, you can use any text fields from your model that you’d like to search on.

Next thing we’d do is to add the search urls to our urlconfig (urls.py) file.


(r'^search/', include('haystack.urls')),

Now add a template that implements a search form and shows the search results at templates/search/search.html

{% extends 'base.html' %}

{% block content %}
    <h2>Search</h2>

    <form method="get" action=".">
        <table>
            {{ form.as_table }}
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Search">
                </td>
            </tr>
        </table>

        {% if query %}
            <h3>Results</h3>

            {% for result in page.object_list %}
                <p>
                    <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
                </p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}

            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}
    </form>
{% endblock %}

That’s it. Now we’ll build the index by running the following command.

python manage.py rebuild_index


Now you have full text search enabled on your django project. Now in case elasticsearch does not suit your requirements or you want to try some other search server. All you’d need to do is Install and run the new search server, change the engine settings in your settings.py file and finally run python manage.py rebuild_index. For example if we switch to SOLR.

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'products',
    },
}

Thursday 26 May 2016

order_by in django template


put this code into a file in a folder "templatetags" inside some application and use it like this:

in django template

{% load your_file_name %}

{% for item in your_list|order_by:"field1,-field2,other_class__field_name" %}


temp.py inside the template tag folder

from django.template import Library
register = Library()

@register.filter_function
def order_by(queryset, args):
    args = [x.strip() for x in args.split(',')]
    return queryset.order_by(*args)

Sunday 22 May 2016

Simplifying Django

Despite Django’s popularity and maturity, some developers believe that it is an outdated web framework made primarily for “content-heavy” applications. Since the majority of modern web applications and services tend not to be rich in their content, this reputation leaves Django seeming like a less than optimal choice as a web framework.
Let’s take a moment to look at Django from the ground up and get a better idea of where the framework stands in today’s web development practices.

Plain and Simple Django

A web framework’s primary purpose is to help to generate the core architecture for an application and reuse it on other projects. Django was built on this foundation to rapidly create web applications. At its core, Django is primarily a Web Server Gateway Interface (WSGI) application framework that provides HTTP request utilities for extracting and returning meaningful HTTP responses. It handles various services with these utilities by generating things like URL routing, cookie handling, parsing form data and file uploads.
Also, when it comes to building those responses Django provides a dynamic template engine. Right out of the box, you are provided with a long list of filters and tags to create dynamic and extensible templates for a rich web application building experience.
By only using these specific pieces, you easily see how you can build a plain and simple micro-framework application inside a Django project.
We do know that there are some readers who may enjoy creating or adding their own utilities and libraries. We are not trying to take away from this experience, but show that using something like Django allows for fewer distractions. For example, instead of having to decide between Jinja2, Mako, Genshi, Cheetah, etc, you can simply use the existing template language while you focus on building out other parts. Fewer decisions up front make for a more enjoyable application building process.

Onboarding New Django Users

A bigger issue the Django community, and other web frameworks, continue to battle is the onboarding process of new users. For example, these new users typically begin learning Django through it’s official tutorial where you build a polling application. Many of us seasoned Django developers consider it a “rite of passage” into the Django community. But is it the best way to begin learning Django? Probably not.
Currently, there are a total of six parts to complete the polls app tutorial. While each part has it’s significance, it isn’t until part three that you finally write your first public facing view and start building out your HTTP responses. This is a far cry from the more simple “Hello World” tutorial you’ll see on the front pages of popular Python micro-frameworks (e.g. Flask or Bottle). The answer we see happening is to create less of a barrier to entry for learning the parts and pieces of Django and focus on the basic request to response interaction. New users can then build from there to see how the other pieces of the framework help you with common web tasks, like session management, user authentication or the built-in admin interface.
To show what we mean, let’s build out a sample of what a more simplified Django tutorial might look like:
Simple, right? This small bit of code is all you need to run a Django project. Let’s break down each component to explain the necessity for each part.
First, we’ll need to make sure to include Django’s HTTP response request utility and return the values we want to have in that response:
Normally, this code would be implemented in the standard views.py file of a typical Django project. For the simplicity of this project, we’ll be placing most of the code to run our application in this single file.
The next portion of the application ties in nicely in with this portion is the url structure. The code above expects the index url, so we’ll need to create that:
From only seven lines of code we have already created the basics to run an application in Django! Now, we’ll implement the basic settings to make the application runnable:
You’ll notice in the example that we have stripped down the settings and have specifically excluded the database configuration. We see these options as a particular barrier to entry for new users with the confusion it causes when trying to determine what databases they should use. We want to make sure that we focus on specific parts to implementing our project to drop the barrier levels.
NOTE: Be sure to make a private and random SECRET_KEY value in yoursettings.configure for the default session and cross-site request forgery (CSRF) protection.
Since we are not generating this structure by using the startproject command, we are missing the typical manage.py file that is generated. We’ll need to add the relevant portions that are part of manage.py and used to run our project:
You should now be able to go to your command line to start your application:
Now when you browse out to your localhost at 127.0.0.1:8000, you’ll get the output of “Powered by Django” in the window!
simplifying_django_fig_1
Now you may be asking yourself, “where is the views.py or models.py?!” First off, take a deep breath and relax. Remember what Django really is: it’s a Python web framework with a whole host of utilities you can easily import to do the things you need to do to run your application. This is where we see the next portion of the tutorial in teaching new users how to import those utilities. For example, building out a simple template seems like the most natural progression in this process. So, let’s do that!
The basic understanding of adding templates is through the way we define our settings and urls. We’ll need to first tell Django where our template files are located by adding the necessary settings. Let’s start by adding a variable to point to your where your project lives:
You’ll notice at the top, we’ve added an import of the os.path Python module. By doing this, we have created an easy way for new users to point to their project folder. Now we can easily add in our TEMPLATE_DIRS setting to point to this template directory and start taking advantage of Django’s built-in tags and filters!
As you can see, by decomposing the basics of creating a Django application into smaller parts, we can create an easier way to onboard new users. We need to re-learn how to teach Django by building Django applications without the ORM and without the Django admin. These parts of Django need to be seen for what they really are: built-in features. They aren’t necessary in order to use the framework and you don’t lose much if you don’t feel the need to implement them. We should start with the pieces of Django, just like you would learn the Python standard library, for the good parts and not feel the weight. Let’s start moving past that and see it for the open source, feature rich utility it really is.
So, with all of this in mind, what are some applications you are considering building that could be developed in this light-weight manner?