Installation instructions
Django
sudo pip install django
PostgreSQL
sudo apt-get install postgresql postgresql-client postgresql-server-dev-9.4 [pgadmin3]Install database binding for django :
sudo apt-get install python-dev # necessary to compile the binder sudo pip install psycopg2
Configuration
Database access
Add a demo user from shell and set its password to 'user'sudo adduser spvuser
Log into postgres and create a database for that user
sudo -u postgres psql postgres=# CREATE USER spvclient WITH PASSWORD 'user'; postgres=# CREATE DATABASE spvdatabase OWNER spvclient ; postgres=# \qCheck that the new database is accessible
psql -d spvdatabase -U spvuser spvdatabase=> \q # quit
Finally, edit postgres configuration file
sudo nano /etc/postgres/main/9.4/pg_hba.conf
Replace ident or peer with trust
local all all trust
Develop your own site
Create project
django-admin startproject poc_supervisor
Run server (default: 127.0.0.1:8000) :
python manage.py runserver
Ctrl+C to quit
Create an app (let's call it supervisor):
python manage.py startapp supervisor
Edit supervisor/apps.py (app generic configuration information):
from django.apps import AppConfig class SupervisorConfig(AppConfig): name = "supervisor" verbose_name = "POC 5.5 Supervision interface"
[cd supervisor]
Create a view
- edit views.pyfrom django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.")
- create a file named urls.py with the following code to map view to URL
from django.conf.urls import url from . import views urlpatterns = [ # ex: /supervisor/ url(r'^$', views.index, name='index'), ]
[cd ..]
- edit poc_supervisor/urls.py to point new view to root
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^supervisor/', include('supervisor.urls')), url(r'^admin/', admin.site.urls), ]- Verify
python manage.py runserver
and open 127.0.0.1:8000/supervisor
Django Database settings
Edit poc_supervisor/settings.py and replace existing DATABASES descriptor with :DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.postgresql_psycopg2', 'NAME' : 'spvdatabase', 'USER' : 'spvuser', 'PASSWORD': 'user', 'HOST' : '', } }
Generate database tables for all INSTALLED_APPS
python manage.py migrate
Verify that tables have been created
psql -d spvdatabase -U spvuser spvdatabase=> \dt
You should see a list of tables
public | auth_group | table | spvuser public | auth_group_persmissions | table | spvuser ... public | django_session | table | spvuser
spvdatabase=> \q # quit
Create a database model for your app
For example for this model:
Edit supervisor/models.py
from django.db import models # Create your models here. ############################## ################ # # Custom validators # ############################################## def isStatusValid(value): """ Checks the validity of SystemData status field """ valid_status_list = ['DEFAULT', 'NORMAL', 'NOT_AVAILABLE', 'NO_DEVICE', 'PROTOCOL_ERROR'] isValid = False if value in valid_status_list: isValid = True return isValid class DeviceProxy(models.Model): name = models.CharField(max_length=64, unique=True) description = models.TextField() ip_address = models.IPAddressField() subnet_mask = models.IPAddressField() port = models.IntegerField(default=47808) def __str__(self): return self.name + " (%s)"%self.ip_address class SystemData(models.Model): identifier = models.IntegerField(primary_key=True) description = models.CharField(max_length=64, unique=True) resolution = models.DecimalField(max_digits=3, decimal_places=3) unit = models.CharField(max_length=10) def __str__(self): return self.identfier + " : %s"%self.description class SystemDataAcquisition(models.Model): # Delete all system data that belongs to deleted device proxy device_proxy = models.ForeignKey(DeviceProxy, on_delete=models.CASCADE) identifier = models.ForeignKey(SystemData, on_delete=models.CASCADE) timestamp = models.DateTimeField() status = models.CharField(max_length=32, validators=[isStatusValid]) value = models.DecimalField(max_digits=10, decimal_places=2) def __str__(self): return self.identifier + " = %s %s"%(str(self.value), self.unit)Add your app to INSTALLED_APPS in poc_supervisor/settings.py:
INSTALLED_APPS = ( 'supervisor.apps.SupervisorConfig', ...
Activate your model
python manage.py makemigrations supervisor python manage.py migrate
Check this link if you want to play with your model with the django shell.
Add an administration interface
python manage.py createsuperuser
and enter the user name (e.g. 'admin'), an email address for this user and a password twice. Verify :
python manage.py runserver
and open 127.0.0.1/admin and log yourself.
Add administration option on database entries
Edit supervisor/admin.pyfrom .models import SystemData, DeviceProxy admin.site.register(SystemData)br /> admin.site.register(DeviceProxy)Run verification step again.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.