Python django

Install python
Install python setup tools - sudo curl https://bootstrap.pypa.io/ez_setup.py -o - | python
Install virtualenv - sudo easy_install virtualenv
Initialize virtualenv - virtualenv --no-site-packages django-purandar
Install django - 
  • cd django-purandar/
  • easy_install django
  • cd bin
Create Project
  • django-admin startproject django-test
  • python manage.py runserver
  • access server at http://127.0.0.1:8000/
  • deactivate
Ref: https://docs.djangoproject.com/en/1.8/intro/install/
  • source bin/activate
  • cd django-test
  • vim settings.py
  • edit - DATABASES, ENGINE: add sqlite3, Name: path for storage.db file
create app - 
python manage.py startapp <appname>
This will create base files for the app.  
__init__.py, models.py, tests.py and views.py
__init__.py - just mentions that this folder is a package/module so that this can be imported to other programs.
models.py - for db related information like
class Article(models.Model):
  title = models.CharField(max_length=200)
  body = models.TextField()
  pub_date = models.DateTimeField('date published')

  likes = models.IntegerField()

Now tell django to create the respective DB from models.py
python manage.py syncdb
This will create the tables and create the db file in the respective path provided in setting.py earlier.
Browse the DB file using "sqlite browser" 
Notice that no <app> related table is created there, that is cause we haven't added our application to the settings.py file.
python manage.py makemigrations
python manage.py migrate
python manage.py syncdb

FYI: below command can be run to obtain actual sql query to run in DB
python manage.py sql <appname>

Due to version conflict If: CommandError: App 'article' has migrations. Only the sqlmigrate and sqlflush commands can be used when an app has migrations.
Delete or rename the migrations folder within the <app> dir structure.
python manage.py sql <appname>

If any model definition is changed, then try python manage.py reset <appname> - careful this will remove all of data in DB

Importing/Adding data to app DB, by running Shell
python manage.py shell
>> from <appname>.models import <ModelName>
>> <ModelName>.objects.all()
Displays all objects in DB.
To insert data
>> var = <ModelName>(key=value, key=value,...)
>> var.save()
>> var.id
>> <ModelName>.objects.all()

To get the correct ID to be displayed.  Define __unicode__(self) function in Models.py
>> from <appname>.models import <ModelName>
>> <ModelName>.objects.all()
Should now display what ever is returned in unicode function.
  def __unicode__(self):
    return self.<key>

1) Views
Create view - <app> folder views.py
def hello(request):
  name = "Purandar"
  html = "<html><body>Hi %s, this seems to have worked!</body></html>"%name

  return HttpResponse(html)
assign url - <project_test> folder urls.py

url(r'^hello/$', '<appName>.views.hello'),

2) Templates View
Define html templates in a folder and define the path in <Project_test>/setting.py
'DIRS': ['/Users/purandarkakde/python/django-purandar/django_test/templates'],
Define hello.html in templates folder
<html>
<body>

Hi {{ name }},
this TEMPLATE seems to have worked!

</body>
</html>
Import the templates in <appName>/views.py
from django.template.loader import get_template

from django.template import Context
def hello_template(request):
  name = "Purandar"
  t = get_template('hello.html')
  html = t.render(Context({'name': name}))
  return HttpResponse(html)

Add the url in <Project_test>/urls.py
url(r'^hello_template/$', 'article.views.hello_template'),
Runserver and Access: http://127.0.0.1:8000/hello_template/

3) Class View
Add view definition in <appName>/view.py file

from django.views.generic.base import TemplateView

class HelloTemplate(TemplateView):
  template_name = 'hello_class.html'

  def get_context_data(self, **kwargs):
    context = super(HelloTemplate, self).get_context_data(**kwargs)
    context['name'] = "Purandar"

    return context

Define the URL
url(r'^hello_class_view/$', HelloTemplate.as_view()),

Define <hello_class.html> New Template in Templates folder.

4) Render Template method
from django.shortcuts import render_to_response
def hello_template_simple(request):
  name = "Purandar"
  return render_to_response('hello.html', {'name':name})

Define its url in <projectName>/urls.py
url(r'^hello_template_simple/$','article.views.hello_template_simple'),

non root user login setup for Docker

Create docker Group, add user to group, restart docker service, login as docker.

sudo groupadd docker
gpasswd -a ${USER} docker
sudo service docker restart
or
sudo service docker.io restart
newgrp docker

Mac restart sshd service

services (sshd for example) are disabled in the configuration file so you will need to use the '-w'switch when loading them. Here is a sshd example:

Start the service
   $sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist 

You can stop the service using the 'unload' subcommand.
   $sudo launchctl unload  /System/Library/LaunchDaemons/ssh.plist 
To list the services, use the 'list' subcommand

Manage HISTORY commands - unix

  • Remove duplicate commands from history with, this can be added in .bash_profile file
  • Continuous duplicates entry can be removed with, export HISTCONTROL=ignoredups
  • Remove all duplicates from history with, export HISTCONTROL=erasedups
  • Note: These exports would effect only for the commands after they have been set, meaning previous old duplicates would remain in the history.  May be we can clear all history with history –c command, and then set/export these variables, but we will miss the initial existing history this way.  Any other ways to make the export affect previous initial history too ?
  • For now am just making a backup of previous history, by default all history will be stored in file ~/.bash_history
  • cp ~/.bash_history ~/.bash_history.bkp; history -c

  • Running a command without allowing it to exist in history, HISTCONTROL=ignorespace for this command to work, we need to provided a space before entering a command so that, that particular command is not remembered in history.  Example: ‘ pwd’ (notice space at the start)
  • Disable specific commands from being tracked in history export HISTIGNORE="<command>:<command>:<command>" Example: export HISTIGNORE="pwd:ls:ls -ltr:"
  • Disable history with, export HISTSIZE=0
  • Note: Multiple values for HISTCONTROL can be exported by using : (colon)  as delimiter.  Example in my case  export HISTCONTROL=ignorespace:erasedups
  • Note: Do not just export variable directly, it will overwrite existing values.  So export existing variable value and append to it using colon. ($HISTCONTROL:<new_value>)
# ignore continuous and also all duplicates from history
export HISTCONTROL=ignoredups:erasedups

# Do not store in history
export HISTCONTROL=$HISTCONTROL:ignorespace

Mac Screen Recording with Quicktime

 The key steps to pause video:
  • Click on Quicktime in the dock to make it active
  • press Esc to bring up the Recording bar
  • hold down Alt (option) to reveal the Pause button
  • click on the Pause button
  • click on the red button to resume recording

This works for me under MacOS 10.9.3

Unix save readonly opened by saving it has sudo user

:w !sudo tee %

:w !sudo tee > /dev/null %
The > /dev/null part explicitly throws away the standard output, since, as we don't need to pass anything to another piped command.

Better add this line to your .vmrc file and just us :w!! to save a file which was opened in readonly mode.

cmap w!! w !sudo tee > /dev/null %


Rebel Yell - Billy Idol

video