Hacks by Ruddra

Docker: Tips on using Volume, Entrypoint and .dockerignore File

Have you worked with Docker before? Do you think you have trouble with it, like when you are developing an application, you need to build every time to see the results, or thinking of the best way to load data in DB, or may be you are annoyed because there are lots of markdown files, screenshots etc that are taking a lot of space in your container.

Docker: Tips on Using Docker Compose, Build Argument and Environment Variable

Imagine, you have some microservices and you want to use one’s API from another, how can you do that if all of them are running in different docker container? Docker Compose has a solution for you. Suppose you want to deploy a project in test, stage and production environment with same Dockerfile for all of them through a single Dockerfile.

Docker: Tips on Writing Dockerfile, Reduce Sizes and Build Time of Images

For the past couple of years, I have been creating Dockerfile for different projects. based on those experiences I am going to share some tips on writing docker files to communication between containers. FYI: before reading this article, please read the article from official docker blog site to learn about best practices for creating Dockerfile.

Manjaro KDE Virtual Desktop Management and Visual Improvement Shortcuts

For the past couple of days, I am testing out Manjaro linux. Most of the time, I was working with KDE Plasma Edition. In my opinion, it is the most beautiful desktop environment from Manjaro official distributions. I am a fan of Ubuntu Unity workspaces and wanted to use similar features in KDE plasma as well.

Manjaro Tips and Tricks

Recently I tried out Manjaro operating system, and did some experiment with XFCE and KDE desktop environments. Here I am going to share some tips and tricks to setup Manjaro in you machine. This article is part of a series based on Manjaro. Tips shared here are applicable for all Manjaro desktop distributions.

Django Access URL Arguments Through Middleware

Lets say you have an API which is being used by many stores. Now for a specific store ABC, they want to have a different response format. So how would you make this implementation generic without making different views for store ABC? Simple, use middleware. How It Works First update the url like this:

Build and Configure Plugins Inside Solr Using Docker

Adding a plugin to Solr is relatively simple process. In this article, we are going to see how to add a plugin by creating a jar file using a docker container, then add that to a Solr which also runs inside docker container. Building the Plugin Workflow For Building the Plugin Steps for building a Solr plugin inside Solr docker file are like this:

Use VS Code Inside Docker Container for Development

VS Code is the popular IDE at the moment. You can use it for developing applications in almost any programming language. In addition to that, you can do remote development in Docker,VM etc. In this post, we are going to discuss about how to use Docker Environment in VS Code.

Django Serialize ForeignKeyField, ManyToManyField, Instance and Property Method

Django’s serialization framework provides a mechanism for “translating” Django models into other formats. Usually they are in json, yaml, XML, GeoJSON etc text based formats. Here, we are going to supercharge these serializers to do more things, even try to render properties and instance methods. This article will be useful to you if you are:

Deploy Django App in Sub Directory Using Openshift

The Problem When you are using Openshift, you will be using routes to expose a route from a service. Let’s say you want to expose a path in example.com/dummy. When you do that for a django application(without any reverse proxy server), it usually becomes a problem, because the sub directory does not work well with Django’s urls.

Install MySQL and MySQLClient(Python) in MacOS

Connecting to MySQL from Python in MacOS is a very problemtic and painful process. In this post, we are going to see how to install MySQL and connect it from a Python Application using mysqlclient: Step One: Install Brew You need to install Homebrew in you local machine. You can do it by:

Create Proxy Object in Python

Lets say you have been using a class named ClassA, objects created from that class has been used in many places. Now you have a requirement that you need to make a simillar class ClassB, which has almost simillar functionality. Objects of ClassA and ClassB will have simillar method signatures, and could be swapable.

Do Extra in S3 Using Django Storage and Boto3

Today, I am going to write about few useful snippets/functionalities which I have used for Amazon S3 or any S3 compitable storage using Boto3 and Django Storage. FYI, this post focuses on using S3 with Django. So without further ado, lets begin: Configuring S3 When using django storage, you don’t need to put configurations like ~/.

Django: Save Unknown Object in Database

Suppose you are dealing with a poorly written API, which returns different type of objects at different scenarios. For example, sometimes it returns a string(ie. abcd), sometimes it returns a list(ie. [1,2,3,4]), or maybe None. Now, you are suppose to save that data, which you can’t predict. As you can’t predict the data type of object, you can’t design your Django Model accordingly.

Django: Ordering by Linked List for Model Objects

Linked list is a data structure where each object has points to next. In django, linked list can provide an interesting solution when it comes to custom ordering/grouping issues. Lets think of an example like this= you are making a blog site, and you have made a Post Model like this:

Django: Changing User Model in Mid-Project

Every now and then I see some questions pop up in stackoverflow with title: ValueError: Related model ‘app.User’ cannot be resolved This is a very common issue and pops up when the user tries to run the migration. So, lets talk more about what is this problem and how can we solve it.

Testing for Django Rest Framework with Factory Boy and Faker

“I’m not a great programmer; I’m just a good programmer with great habits.” ― Kent Beck Test Driven Development - in short TDD is a practice where you write the tests first then the actual code. It makes the code less vulnerable and it makes a testable software rather than writing the software, then test it.

Bye Bye Ghost, Hello Hugo

“The Only Thing That Is Constant Is Change -” ― Heraclitus This blog site has changed quite a lot since the begining. I initially developed it as a Django application. It was part of my learning process, and I hosted it in a private shared server. But unfortunatly, good days came to an end, I had to give up that hosting for costing.

Control Application Flow from Redux Middleware using React Router 4

While developing application in ReactJs with Redux and React Router 4, what I felt is that, there is no direct relationship between application’s state and routing. We needed to create those connections inside Components. In each component, we needed to check application state and based on that we made decisions where to go next(if needed).

Migration From Ghost 1.25.2 to 2.2

This post is no longer valid as I have moved to Hugo :) I have just migrated the site from Ghost 1.25.2 to 2.2. As every growth comes with some inevitable pain, thus you can expect a lot of things are broken. Here all the URLS have been updated.

Automated Deployment to Openshift Using Jenkins and Webhook

The last post was about defining the pipelines. Now it is time to execute them. Also, at the end, we are going to show how to integrate webhook in your repository, so that for a specific event(like Push, Pull request merge etc) it will trigger the pipelines to automatically deploy the latest code to servers.

Writing Jenkins Pipeline For Openshift Deployment

Pipeline is a set of instructions, which will be executed as per given sequence and produce a output. Jenkins Pipeline is simply writing these instructions in Jenkins. These pipelines can be written in Groovy. In previous post we have defined deployment structure and made necessary preparations for deployment of a python+gunicorn+nginx+jenkins based project.

Deploy A Python App to Openshift: Planning and Preparations

Deploying a Python application to Openshift is fairly easy. Write a Dockerfile and run oc new-app /path/to/Dockerfile, that’s it!! But if you want implement a full fledged modern CI/CD using Jenkins and openshift, you need to do little more than that. So let’s dive into it. We will explain about the whole process in three articles:

Deploy Django to Openshift Using Jenkins Pipeline(CI/CD)

Using Jenkins pipeline, you can easily automate your deployments in openshift. In this post, we are going to use this powerful tool to deploy a Django project. But before we start, one thing I need to mention is that, this project has been tested on in Minishift 1.2.0, Openshift v3.

Boilerplate For Angular 1.X and Django(Powered by Webpack2, Docker, Yarn, DRF)

This post is deprecated. Use it at your own risk Simple and clean boilerplate for using django and angular 1.x togather, with basic structure comes built-in. Tech Stack Python3 Django 1.11.6, Django Rest Framework Webpack 2 JavaScript(ES6)/AngularJS 1.x Yarn Prerequisite Require Yarn. How to install Yarn: https://yarnpkg.

Migration from Ghost 0.11 to 1.XX Using Docker

Ghost had formulated lots changes when they released version 1.XX. So, when you try to upgrade from 0.11 to 1.XX, you can follow the following steps: Steps FYI, you can also use the following steps if you want run Ghost in Docker and import your existing settings. Just Skip Step 1, 2, 7, 8.

[Travel] Bandarban: In Search of Waterfalls

Bandarban is a heaven for trekking for Bangladeshi people. There are lots of hills around the district, and there are some amazing waterfall as well. I visited three amazing waterfalls named Nafakhum, AmiaKhum, Satbhaikhum. Khum means waterfall in Marma language. captured on the way to Thanchi Again we went to Bandarban town and from there we took a Chander Gari(A four wheel drive car) to Thanchi Upazila.

[Travel] Bandarban: Journey to Keokradong

Bandarban is a beautiful district of Bangladesh. You will see lots of amazing mountains which are part of Arakan range. Keokradong is the 3rd highest pick**(after Tajingdong and Saka Haphong, references are here) of Bangladesh, which is situated in Bandarbans. So today I am going to share my experience visiting there.

[Travel] Journey to the Land of Thunder Dragon

I went to Bhutan on September, 2015. It was my best trip ever, really ever. From Dhaka, we went there by DRUK AIR. We(me and two of my friends) landed in Paro Airport. It was really wonderful airport, it was small but clean and organised. After getting out of the plan, a chilling air struck us, and instantly we knew, we gonna like this country.

Deploy Django to Openshift 3 Powered by MySQL and Gunicorn

If you want to use Openshift for deploying Django, you can follow this post and simply do that. You don’t need to learn Kubernetes. We will use docker file only. No other fancy stuff. Also, before starting, I am also hoping you are little bit familiar with Openshift 3 and oc tools.

Use VS Code for Python and Django Development

Visual Studio Code is an editor developed by Microsoft. I have been using this editor for Python development for a few months now. Previously I have been using Pycharm Community Edition for development, but I had to switch to an editor which was less resource consuming than Pycharm, hence I found VS Code.

Ubuntu Hacks: Launching Chrome Apps at Startup

Chrome apps are really handy when you don’t want to overhaul your system with apps for every little purpose. Starting those apps at startup can be used instead of manually starting them everytime you want to use them. Like starting the messenger at startup of the system. To do that.

Play With Pillow

Who hasn’t heard PIL? It’s an image processing library made by python (Python Image Library). Pillow is an extension of it. Installation So installing pillow is really easy: pip install pillow Now we are going to do some image processing cool stuff: First Load Image: from PIL import Image, ImageOps img = Image.

Python: Selenium with PhantomJs to capture Screenshots

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. And Selenium is a portable software-testing framework for web applications. Selenium provides a record/playback tool for authoring tests without the need to learn a test scripting language (Selenium IDE)

Documentation Of Django Encrypt File

This is no longer maintained. Use it at your own risk. Django Encrypt File is a simple Library which can be used to encrypt uploaded files and store them. Installation Use pip to install it: pip install djangoencryptfile Or pip install https://github.com/ruddra/django-encrypt-file/tarball/0.7 Basic Usage from djangoencryptfile import EncryptionService from django.

Docker: Use Celery in Django(Redis as Broker)

In previous two posts, we have deployed Django with Postgres, Nginx, now its time to do some async stuff using Celery. In this post, I will do the magic tricks first, explain them later. Add Celery to Django To add celery, we need make a container for celery. We can re-use the Dockerfile of django for making celery’s container like this:

Serve Static Files by Nginx from Django using Docker

This is more of a follow up post of my previous blog. Before I start, I am assuming you have successfully deployed django using docker & nginx, but having some problems serving static files. Steps No worries, its easy. Just follow these steps: 1. In your django settings.py file, add static file directory i.

Deploy Django, Gunicorn, NGINX, Postgresql using Docker

This post mainly based on this blog: https://docs.docker.com/compose/django/. I will be extending this post by serving django+gunicorn using Nginx, also I will using Postgresql docker container to use it as database. Steps Lets not waste time and go to the following steps. 1. Let’s make an empty directory named myproject and add another folder inside name it src.

Make MacOS Notifications From RSS Feed Using Python

Today we are going to see how make OSX notifications from RSS Feed of any website using Python. So first of all, let us see how to make a simple notification. We are going to use AppleScript for this. Go to terminal and Type: >>osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"' We shall see a notification popping up in right up corner like this:

Make .CSV file using MySQL and Python

In this blog, I am going to show how to develop an application by which we can make .csv files from MySQL database using Python. Now, let us start by getting data from Database. Lets say we have a DB names csv_test and it contains a table named test with fields id and name, and for testing purpose let us have some values inserted into it like this:

Django 1.7 + Scrapy

This post is depricated. Use it at your own risk. Today I am going to share how to use Scrapy and Django together to crawl to a website and store scraped data to Database using Django. Project Setup Django First, let us build a Django application using the following commands.

Make a Blog using Django Part 2

Please follow the official tutorial for creating your first django application: https://docs.djangoproject.com/en/2.1/intro/tutorial01/ From previous post, you have configured and ran django, also added admin site to the django. To view the working source of this tutorial, check here at: https://github.com/ruddra/myblog/ Now click on the myblog section and click add to add new blog.

Make a Blog using Django Part 1

Please follow the official tutorial for creating your first django application: https://docs.djangoproject.com/en/2.1/intro/tutorial01/ “Making a blog using django” - is probably the most made tutorial of Django. This post is no different. But I made this in my own way using some of django’s built-in features so that less coding is required and making it more understandable with minimum packages to use.

Change Column Headers in Django Tables 2

Django Tables2 is a package which displays table directly from queryset. It shows column header based on object’s attribute name. But if someone wants to override it, how can he/she do that? Here is a easy solution. Suppose we have a model class like this: class SomeModel(models.Model): somevalue = models.

Dynamically constructing filters based on string input using SQLAlchemy

Here I am going to write a dynamic filter. This filter is made for python 3. It will take query or model class and filter condition as input, It will return filtered query based on those filter condition. This is constructed using this SO answer. Function class DynamicFilter(ModelHelper): def __init__(self, query=None, model_class=None, filter_condition=None): super().

How to configure Django and Tornado in PyCharm Community Edition

I think, Pycharm is THE best IDE for developing python. But unfortunately, the professional edition is not free. But community edition is good enough for doing debugging, integrating GIT etc. Normally its easy to use the community edition for django and tornado’s debugging/running if you know how to configure.

Some Useful Tools/Function for Django

This post is depricated as its based on Django 1.6 mostly. I am going to share some useful Django tools/functions which are very useful(were for me atleast) to get things done. Return any model class and its properties This method will return any model class if you have the name of the class.

Perodic Tasks By Celery 3.1 Example

This post is depricated. Please see the latest documentation(current celery is running version 4.2) I am assuming you have read celery docs from Celery Documentation As we know, celery can be used as a scheduler for executing asynchronous tasks in periodic cycles. Here I am going to share to do that with a code example.

Working with Formset

This post is depricated and may not be useful. As documentation says: A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid. So here I am going to show a very simple django formset implementation example.

Sample Ajax GET/POST Request in Django

This post is depricated and might not be useful. Let us make a test scenario here: A dropdown field which on change we are going to send a Get/Post request to Django and return response. Let us start coding…. <select id="select_dropdown"> <option value="joshua">joshua</option> <option value="peter">peter</option> .... .... </select> Let’s make an Ajax request after the change in the dropdown field.

Using IntellijIdea/Pycharm Within An Exisiting Virtualenv

To add a virtual environment to Intellij Idea, You have added virtualenv folder’s python to project SDK. Which means that virtualenv directory’s python( for example venv/bin/python2.7) needs to be added to Intellij Idea project path. Here is a demonstration: For PyCharm 2018 As per documentation from Pycharm: In the Project Interpreter page, click the cog icon and select Add.

Implementation of 'Forgot/Reset Password' feature in Django

This post is depricated in favour of django authentication views Django has its own implementation for reset/forgot password for its admin site. We are going to use that piece of code as reference to implement similar feature for a non admin-site authentication page. Although there are tons of good packages which will allow user to use their password reseting system.

Syncdb vs Migration

While surfing through Stackoverflow, I find a common question among Django users that, database not working properly; fields attribute changed, yet not working etc. Clearly because most of them used syncdb after altering fields. Well, lets make some things clear here about django syncdb and migration. What is syncdb? syncdb is a command which is executed in django shell to create tables for first time for apps which are added to INSTALLED_APPS of settings.

RichText Editor in Django Admin Site

I wanted to add a rich text editor within django administrator. It is not that hard to add a rich text editor, as there are editors like CKeditor, Tinymce. There are multiple plugins for django like django-ckeditor or django-tinymce etc. It seemed very complicated to use for me. So what I did here is that I have downloaded ckeditor standard edition and extracted it in my Static folder and loaded the js file within templates>admin>base.

Django Translation Using .PO File

When comes to using multiple languages in one single site, Django is very handy. You can use .po file to do your translation for you. Process is very simple: First create .po file. To make .po file I would suggest to use poedit or Rosetta. Here is another option that is using django’s very own Localisation.