Basic

Github keep tracking and what is for ?

26 Oct , 2014  

When working with github, we usually do “push” and “pull” followed by “origin master” or “origin branch-name”. We can just use “git pull” or “git push” and will followed by the current branch.

For global push config using current:

1
git config --global push.default current

Example, we currently at branch “dev”. To push update, we can do:

1
git push origin dev

If we tracking dev branch using :

1
git branch --set-upstream-to=origin/dev dev

Then all we need just

1
git push

Django

No python application found, check your startup logs for errors

25 Oct , 2014  

This errors occured in setup Django + UWSGI in Ubuntu 14.04. The reason because the uwsgi from aptitude is broken.

1
2
3
4
Sat Oct 25 12:38:28 2014 - --- no python application found, check your startup logs for errors ---
[pid: 5556|app: -1|req: -1/1] 36.78.164.116 () {42 vars in 681 bytes} [Sat Oct 25 12:38:28 2014] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 1 headers in 57 bytes (0 switches on core 0)
Sat Oct 25 12:38:29 2014 - --- no python application found, check your startup logs for errors ---
[pid: 5551|app: -1|req: -1/2] 36.78.164.116 () {42 vars in 681 bytes} [Sat Oct 25 12:38:29 2014] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 1 headers in 57 bytes (0 switches on core 0)

Here is the solution

1
2
3
4
sudo pip install uwsgi
cd /usr/bin
sudo mv uwsgi uwsgi-old
sudo ln -s /usr/local/bin/uwsgi uwsgi

Make sure you’re using right .ini wsgi :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[uwsgi]
# enable master process manager
master = true

# spawn 2 uWSGI worker processes
workers = 2

# bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket
socket = /tmp/dapurhemat.sock

# set mode of created UNIX socket
chmod-socket = 666

# place timestamps into log
log-date = true

# user identifier of uWSGI processes
uid = root

# group identifier of uWSGI processes
gid = root

; define variables to use in this script
project = dapurhemat

; number of worker processes
processes = 8

; plugins=python
; project-level logging to the logs/ folder

; django >= 1.4 project
chdir = /var/yodi/dapurhemat
wsgi-file=/var/yodi/dapurhemat/dapurhemat/wsgi.py

virtualenv=/root/.virtualenvs/dapurhemat

; run master process as root
enable-threads = true

vacuum = true
env = DJANGO_SETTINGS_MODULE=kargo.settings
pidfile2 = /tmp/dapurhemat.pid

You can test your app first with “python manage.py runserveR” before integrate with uwsgi

We also can check by manually running the “.ini” configuration:

1
uwsgi --ini yourfile.ini

If you got error like below:

1
2016/05/29 13:07:32 [error] 5589#0: *4 upstream prematurely closed connection while reading response header from upstream, client: 202.62.17.132, server: dapurhemat.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/dapurhemat.sock:", host: "dapurhemat.com"

Then, please install :

1
apt-get install uwsgi uwsgi-plugin-python

,

Django

Django UWSGI Ubuntu 14.04 in Amazon EC2

25 Oct , 2014  

Here is complete tutorial installing and setup django for production in EC2 Ubuntu 14.04. First, we need to login into EC2, setup a new instance and launch. Then login SSH and ready for action.

1. Always get latest updates

1
sudo apt-get update && sudo apt-get upgrade -y

2. Install Java

1
2
3
4
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

3. Install Python and PostgreSQL

1
sudo apt-get install libpq-dev python-dev postgresql postgresql-contrib nginx git zip unzip build-essential python libxslt1-dev python-pip libmysqlclient-dev imagemagick uwsgi uwsgi-plugin-python uwsgi-plugin-python

4. Setup Django UWSGI

1
2
cd /etc/uwsgi/apps-enabled
sudo vim yourapp.ini

This is some example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[uwsgi]
# enable master process manager
master = true

# spawn 2 uWSGI worker processes
workers = 4

# bind to UNIX socket at /run/uwsgi/<confnamespace>/<confname>/socket
socket = /tmp/myproject.sock

# set mode of created UNIX socket
chmod-socket = 666

# place timestamps into log
log-date = true

# user identifier of uWSGI processes
uid = ubuntu

# group identifier of uWSGI processes
gid = ubuntu

; define variables to use in this script
project = myproject

; number of worker processes
processes = 2

plugins=python
; project-level logging to the logs/ folder

; django >= 1.4 project
chdir = /var/myproject
wsgi-file=/var/myproject/myproject_settings_folder/wsgi.py

; run master process as root
enable-threads = true
processes = 10

vacuum = true
;env = DJANGO_SETTINGS_MODULE=djtest.settings
;module = polatic.wsgi:application
env = DJANGO_SETTINGS_MODULE=myproject.settings
pidfile2 = /tmp/vasham.pid
;harakiri = 20 # respawn processes taking more than 20 seconds
;max-requests = 5000 # respawn processes after serving 5000 requests

REMEMBER! in Ubuntu 14.04, UWSGI WILL NOT WORKING!

Here is the quickfix:

1
2
3
4
sudo pip install uwsgi
cd /usr/bin
sudo mv uwsgi uwsgi-old
sudo ln -s /usr/local/bin/uwsgi uwsgi

4. Setup NGINX

Create a new file in “/etc/nginx/sites-enabled”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {                                                                  
    listen 80;                                                            
    server_name yourdomain;                                    
    access_log /var/log/nginx/access.log;                                  
    error_log /var/log/nginx/error.log;                                    
    root /yourpath/yourapp;                                                    
                                                                           
    client_body_timeout 646000;                                            
    client_header_timeout 646000;                                          
    send_timeout 64600;                                                    
    client_max_body_size 30m;                                              
    keepalive_timeout 63000;                                              
    proxy_connect_timeout 66000;                                          
    proxy_send_timeout 66000;                                              
    proxy_read_timeout 66000;                                              
                                                                           
    index  index.php index.html index.htm;                                
                                                                           
    # Check if a file exists at /var/www/domain/ for the incoming request.
    # If it doesn't proxy to Apache/Django.                                
    try_files $uri @django;                                                
                                                                           
    location /media  {                                                    
         alias /yourpath/yourapp/media;                                        
    }                                                                      
                                                                           
    location /static {                                                    
         alias /yourpath/yourapp/static;                                        
    }                                                                      
                                                                           
    location / {                                                          
        uwsgi_pass unix:/tmp/yourapp.sock;                          
        uwsgi_read_timeout 60000;                                          
        include uwsgi_params;                                              
    }                                                                      
                                                                           
}

5. Setup PostgreSQL

Edit file “/etc/postgresql/9.3/main/pg_hba.conf”. Edit this line and change it “trust” for enable access psql without password from server.

1
2
3
4
5
6
7
# Database administrative login by Unix domain socket
local   all             postgres                                trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust

To create new user :

1
2
sudo -i -u postgres
createuser --interactive
1
2
3
4
5
psql
create database YOUR_DATABASE;
ALTER USER your_user WITH PASSWORD 'somepassword';
GRANT ALL PRIVILEGES ON DATABASE your_database TO your user;
\q

Done! Now we good to go!

Django

Remove dash or empty label in Django Form ForeignKey

22 Oct , 2014  

To remove dash or empty selection in form foreignkey Django, we just need to declare “empty_label=None” in the form.
Here is the example :

1
2
3
4
instance = forms.ModelChoiceField(empty_label=None,
                queryset=Instance.objects.exclude(
                   ...some queryset..
                )

Windows

Solve office 2013 can’t open document problem

22 Oct , 2014  

I can’t open document from email. Apparently it because permission issue. The solution, open notepad and copy-paste this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Windows Registry Editor Version 5.00
 
; Fixes for Microsoft Office 2013 error when trying to open a document from the Internet
; by WindyLea
 
[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Excel\Security\ProtectedView]
"DisableInternetFilesInPV"=dword:00000001
"DisableAttachmentsInPV"=dword:00000001
"DisableUnsafeLocationsInPV"=dword:00000001
 
[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\PowerPoint\Security\ProtectedView]
"DisableInternetFilesInPV"=dword:00000001
"DisableAttachmentsInPV"=dword:00000001
"DisableUnsafeLocationsInPV"=dword:00000001
 
[HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Word\Security\ProtectedView]
"DisableInternetFilesInPV"=dword:00000001
"DisableAttachmentsInPV"=dword:00000001
"DisableUnsafeLocationsInPV"=dword:00000001

Save as “fixoffice.reg” and double-click on the file

Windows

Clear history in Remote Desktop Windows

22 Oct , 2014  

Quickfix to remote history remote desktop in Windows, go to regedit and go to :

1
HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Default

Delete all histories

Django

Django coercing to Unicode: need string or buffer, NoneType found

20 Oct , 2014  

This errors in Django ‘coercing to Unicode: need string or buffer, NoneType found’ take me 20 minutes to solved.
Look at our example models :

1
2
3
4
5
class Insurance(models.Model):
    code = models.CharField(max_length=255, blank=True, null=True, default=None, unique=True)

class Person(models.Model):
    insurance = models.ForeignKey(Insurance)

When insurance value is None, then you will get error :

1
coercing to Unicode: need string or buffer, NoneType found

The solution ?

If we want to still showing “None” value, then :

1
2
3
4
5
class Insurance(models.Model):
    code = models.CharField(max_length=255, blank=True, null=True, default=None, unique=True)

    def __unicode__(self):
        return unicode(self.code)

Or, we can exclude(code=None) in Form ModelChoiceField() that related with this models.

,

postgres

Setup pgpass in Windows

20 Oct , 2014  

To setup pgpass in windows, please go to :

1
C:\Users\<your-user>\AppData\Roaming

Create “postgresql” folder and create pgpass.conf file inside.

1
localhost:5432:*:postgres:<your-password>

Django

Add error in django form in views

18 Oct , 2014  

Here is an example how to modified or add errors in Django Form using views:

1
2
3
error_message = "Wrong password"
errors = form._errors.setdefault("password", ErrorList())
errors.append(mark_safe(error_message))

Django

Setup Django 1.7, Haystack and ElasticSearch

17 Oct , 2014  

Installing Django, Haystack and ElasticSearch is easy. But then i found problems which using Elasticsearch, my search results is empty. Also i have several headache problems like SearchQueryset returning 0 meanwhile indexing multiple apps is working.

Anyway, here is the correct way to setup Django, Haystack and ElasticSearch.

1. Django
We need to install elasticsearch python module with :

1
pip install elasticsearch

2. Elasticsearch in Windows
There are several helpful tutorials. For me:
1. I just download the zip files
2. setup JAVA_HOME (Download java first) and add java in PATH (Global environment system)
3. execute elasticsearch service in /bin

3. Haystack
Make sure follow this :

settings.py

1
2
3
4
5
6
7
8
9
10
11
12
13
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
        'URL': 'http://127.0.0.1:9200/',
        'INDEX_NAME': 'your-project-name',
    },
}

HAYSTACK_LIMIT_TO_REGISTERED_MODELS = False

# increase the default number of results (from 20)
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 40
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

search_indexes.py

1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime
from haystack import indexes
from .models import AppModel

class AppModelIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.SearchField(document=True, use_template=True)

    def get_model(self):
        return AppModel

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.filter(created__lte=datetime.datetime.now())

Use this for multple apps (just need to rename appname)

And it’s should be works!

system

Solve Skype Problem after changed computer date

17 Oct , 2014  

This is weird! When i changed my computer date, suddenly i can’t send message in conversation. To solve this issue, first download SQLiteBrowser for Windows

http://sourceforge.net/projects/sqlitebrowser/?source=typ_redirect

Then, go to :

1
C:\Users\<Your User>\AppData\Roaming\Skype\<your-skype-account>

Edit “main.db” with SQLitebrowser. Go to browse data and select messages.
Now delete messages that more than the date in your computer. Solved!

Django

Solve Aptana Unresolved import Django

16 Oct , 2014  

For quick solution add both “site-packages” and your projects into your PyDev Python Path.
Then delete .project and .pydev in your project. Re-import your project and it’s must be solved!

Django

Solve soffice error python django in server

16 Oct , 2014  

When execute soffice from python, i got this errors :

1
2
3
4
5
6
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx
[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx

The solution is change uwsgi ini to execute django using “ubuntu” (user) instead of “www-data”

,

Django

Django Python pg_dump export database postgresql

13 Oct , 2014  

Here is a quick snippet to export postgresql:

1
command = 'export PGPASSWORD=PASSWORD_HERE\npg_dump -U USERNAME_HERE-h localhost DATABASE_HERE| gzip > %s' % FILEPATH_HERE

os.system(command)

Django

Beware Django Query Filter update() not using save validation

9 Oct , 2014  

Always remember when using this script :

1
Member.objects.filter(...).update({'phone': '082343-324234'})

If you have validation or rules in Model save(), then it will not being called.
REMEMBER that update() in filter will not emit any signal and pass save model.

To update model with kwargs, we can use :

1
2
 instance.__dict__.update(member)
 instance.save()

Django

Django how to reset app table using migration

9 Oct , 2014  

This is simple tricks to reset app table or solve default field value.

1. Commented all app below your app in settings.py

2. Commented your app model

3. Makemigration to generate migration that delete your app table

4. Un-comment and add modification

5. Makemigration to generate migration that create your app table

Example:

I have app “insurance”. So, first, i disable all app below insurance in settings APP :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Application definition
INSTALLED_APPS = (
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',
    'widget_tweaks',
    'bootstrap_pagination',
    'haystack',
    'insurances',
#    'territorials',
#    'companystructures',
)

2. Then I comment my insurance models.py and admin.py (if any)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CommonInfo(StripModelMixin, models.Model):
    remarks = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    status = models.BooleanField(default=True)

    class Meta:
        abstract = True

#class Insurance(CommonInfo):
#    name = models.CharField(max_length=255)
#    code = models.CharField(max_length=255, unique=True)
#
#    uuid = UUIDField(auto=True)

3. Make migrations to delete this table

1
python manage.py makemigrations insurances

4. Now my insurances table already deleted. Then we can re-generate new insurances models.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CommonInfo(StripModelMixin, models.Model):
    remarks = models.TextField(blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    status = models.BooleanField(default=True)

    class Meta:
        abstract = True

class Insurance(CommonInfo):
    name = models.CharField(max_length=255)
    code = models.CharField(max_length=255, unique=True)
    users= models.ForeignKey(Users)
    uuid = UUIDField(auto=True)
1
python manage.py makemigrations insurances

Migrate now!

5. Un-comment all app in settings.py and voila!

Database

Create and Drop Database PostgreSQL commandline shortcut

9 Oct , 2014  

We can create / drop database in PostgreSQL without login into user account. Here is the command (Windows):

1
2
createdb -U postgres <database-name>
dropdb -U postgres <database-name>

Python

Simple Python Task and Test

8 Oct , 2014  

1. How to rename value in list during loop ?
We can use iterate and using index

Django

Django 1.7 python standalone scripts

8 Oct , 2014  

Here is a guide to set standalone python scripts in Django 1.7. Fyi, here is my project structures :

1
2
3
4
5
6
project_name
   |__ app
        |__ __init__.py
        |__ standalone.py
        |__ models.py
        |__ views.py

Then my standalone script be like:

1
2
3
4
5
6
7
8
9
10
import sys
import os

# Setup environ
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
django.setup()

Some example :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import sys
import os

# Setup environ
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")
django.setup()

CURRENT = os.path.dirname(__file__)

from myapp.models import SomeModel

def importing(folderpath):
    folders = os.listdir(folderpath)

    for name in folders:
        try:
            SomeModel.objects.get(name=name)
        except SomeModel.DoesNotExist:
            print(name)

if __name__ == "__main__":
    folderpath = os.path.abspath(os.path.join(CURRENT, "..", "bulk_data"))
    importing(folderpath)

Django

Trick reset django app migration

7 Oct , 2014  

When you have mistakes and you want to reset table with django migration, think this way

1. Delete all tables
By commented all model in your app and disable any apps below your app in settings.py

2. python manage.py makemigrations your-app
This will generate migration to remove all your app tables

3. un-comment your models and makemigrations

Cheers! 🙂

Django

Setup Python and PIP in Windows Server

7 Oct , 2014  

Please follow this link for patching your Python :
http://stackoverflow.com/questions/3652625/installing-setuptools-on-64-bit-windows

For me, since i have Python already in another drive, i just need to update “System Environment” and add :

1
C:\Python27\;C:\Python27\Scripts;

Django

Django admin with grappelli delete data error

6 Oct , 2014  

When using django admin with Grappelli and delete data, i got this errors :

1
2
NoReverseMatch at /backoffice/documents/document/
Reverse for 'app_list' with arguments '()' and keyword arguments '{u'app_label': ''}' not found. 1 pattern(s) tried: [u'admin/(?P<app_label>membersauth)/$']

The solution, you just need to upgrade your django-grappelli

Django

Django unique True with blank True

6 Oct , 2014  

In Django, we can set field with unique=True. But when we want to make the field as optional, we usually think about blank = True which lead to problems. The solution is using default=None

1
email= models.CharField(max_length=255, blank=True, null=True, default=None, unique=True)

And in modelform, we can put this :

1
2
3
# UNIQUE if NOT NULL
def clean_email(self):
    return self.cleaned_data['email'] or None

BPM

IBM BPM Process Designer blank screen solved

6 Oct , 2014  

When running BPM Process Designer, it will going blank in Windows 8. Here are the solutions :

1. Go to the directory where Process Designer is installed and find the javaw.exe file.
For me this directory is:
C:\IBM\ProcessDesigner\v8.5\AppClient\java\jre\bin

2. COPY the file javaw.exe to javawpd.exe. You will now have 2 copies of this file.
3. Now edit your eclipse.ini file. Mine is in C:\IBM\ProcessDesigner\v8.5

4. Find the line which reads:
./AppClient/java/jre/bin/javaw.exe

and change to
./AppClient/java/jre/bin/javawpd.exe

and restart Process Designer.

,

Uncategorized

Setup Windows Server 2012

5 Oct , 2014  

Here are guide to setup windows server 2012 R2 for development.

1. Enable Wifi
Open PowerShell and execute :

1
add-windowsfeature wireless-networking

And reboot.

2. Enable Sound
Run “services.msc” and right click on “Windows Audio” and select “start”.
Right click and select properties, changes into Automatically to enable it permanently.

3. Install NET 3.5
Go to add role, select NET 3.5. In confirmation page, click on “Do you need to specify an alternate source path?”. If the target computer does not have access to Windows Update, click the Specify an alternate source path link to specify the path to the \sources\sxs

Uncategorized

Bootable usb windows from iso

4 Oct , 2014  

We can convert Windows 8 / Windows Server 2012 ISO into Bootable USB. First thing, we need to have the ISO. Then please download Windows 7 USB/DVD Download Tool:

http://www.microsoftstore.com/store/msusa/html/pbPage.Help_Win7_usbdvd_dwnTool

Install the software and open it. Then, prepare USB 8 GB at least and make sure to format using FAT32. This is important, since UEFI only boot FAT32 format.

Then all you need is import the ISO and click Next button!

Server

Setup Git in Windows 8

4 Oct , 2014  

Here are quickstep to setup git in windows :

1. Download git from http://git-scm.com/download/win
2. In Installation, make sure to enable git and linux command in command prompt (yes, the red text)

Now, git can be accessed from your command prompt. Next step is generate ssh key for windows.

Go to git bash (you can search in windows menu) and execute

1
ssh-keygen -t rsa -C "your-email"

Now, you can find the public key in C:\Users\\.ssh\

Django

Import Gunzip compressed file backup in Postgres PSQL

3 Oct , 2014  

Here is quick to import gz database backup files in PostgreSQL

1
2
gunzip file.sql.gz
psql -U postgres dbnam < file.sql