Refactor installation app to move code to model and display more information

This commit is contained in:
Roberto Rosario
2012-06-29 02:39:13 -04:00
parent 2c8832e879
commit e6d861abf5
6 changed files with 160 additions and 28 deletions

View File

@@ -1,7 +1,38 @@
from __future__ import absolute_import
import uuid
from south.signals import post_migrate
from project_tools.api import register_tool
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.db.utils import DatabaseError
from .links import installation_details
from .models import Installation
@receiver(post_migrate, dispatch_uid='trigger_first_time')
def trigger_first_time(sender, **kwargs):
details = Installation.objects.get()
details.is_first_run = True
details.uuid = unicode(uuid.uuid4())
details.save()
def check_first_run():
try:
details = Installation.objects.get()
except DatabaseError:
pass
else:
if details.is_first_run:
details.is_first_run = False
#details.save()
register_tool(installation_details)
check_first_run()

View File

@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Installation'
db.create_table('installation_installation', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('lock_id', self.gf('django.db.models.fields.CharField')(default=1, unique=True, max_length=1)),
('is_first_run', self.gf('django.db.models.fields.BooleanField')(default=False)),
('uuid', self.gf('django.db.models.fields.CharField')(max_length=48, blank=True)),
))
db.send_create_signal('installation', ['Installation'])
def backwards(self, orm):
# Deleting model 'Installation'
db.delete_table('installation_installation')
models = {
'installation.installation': {
'Meta': {'object_name': 'Installation'},
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_first_run': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'lock_id': ('django.db.models.fields.CharField', [], {'default': '1', 'unique': 'True', 'max_length': '1'}),
'uuid': ('django.db.models.fields.CharField', [], {'max_length': '48', 'blank': 'True'})
}
}
complete_apps = ['installation']

View File

View File

@@ -1,3 +1,88 @@
from django.db import models
import sys
import platform
# Create your models here.
import pbs
import psutil
try:
from pbs import lsb_release, uname
except pbs.CommandNotFound:
LSB = False
else:
LSB = True
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.datastructures import SortedDict
from common.models import Singleton
from common.utils import pretty_size
class Property(object):
def __init__(self, name, label, value):
self.name = name
self.label = label
self.value = value
def __unicode__(self):
return unicode(self.value)
def __str__(self):
return str(self.value)
class Installation(Singleton):
_properties = SortedDict()
is_first_run = models.BooleanField(default=False)
uuid = models.CharField(max_length=48, blank=True)
def add_property(self, property_instance):
self._properties[property_instance.name] = property_instance
def get_properties(self):
self.set_properties()
return self._properties
def set_properties(self):
self._properties = SortedDict()
if LSB:
self.add_property(Property('is_lsb', _(u'LSB OS'), True))
self.add_property(Property('distributor_id', _(u'Distributor ID'), lsb_release('-i','-s')))
self.add_property(Property('description', _(u'Description'), lsb_release('-d','-s')))
self.add_property(Property('release', _(u'Release'), lsb_release('-r','-s')))
self.add_property(Property('codename', _(u'Codename'), lsb_release('-c','-s')))
self.add_property(Property('sysinfo', _(u'System info'), uname('-a')))
else:
self.add_property(Property('is_posix', _(u'POSIX OS'), False))
self.add_property(Property('architecture', _(u'OS architecture'), platform.architecture()))
self.add_property(Property('python_version', _(u'Python version'), platform.python_version()))
self.add_property(Property('hostname', _(u'Hostname'), platform.node()))
self.add_property(Property('platform', _(u'Platform'), sys.platform))
self.add_property(Property('machine', _(u'Machine'), platform.machine()))
self.add_property(Property('processor', _(u'Processor'), platform.processor()))
self.add_property(Property('cpus', _(u'Number of CPUs'), psutil.NUM_CPUS))
self.add_property(Property('total_phymem', _(u'Total physical memory'), pretty_size(psutil.TOTAL_PHYMEM)))
self.add_property(Property('disk_partitions', _(u'Disk partitions'), '; '.join(['%s %s %s %s' % (partition.device, partition.mountpoint, partition.fstype, partition.opts) for partition in psutil.disk_partitions()])))
try:
self.add_property(Property('tesseract', _(u'tesseract version'), pbs.tesseract('-v').stderr))
except pbs.CommandNotFound:
self.add_property(Property('tesseract', _(u'tesseract version'), _(u'not found')))
try:
self.add_property(Property('unpaper', _(u'unpaper version'), pbs.unpaper('-V').stdout))
except pbs.CommandNotFound:
self.add_property(Property('unpaper', _(u'unpaper version'), _(u'not found')))
def __getattr__(self, name):
self.set_properties()
try:
return self._properties[name]
except KeyError:
raise AttributeError, name
class Meta:
verbose_name = verbose_name_plural = _(u'installation details')

View File

@@ -1,46 +1,25 @@
from __future__ import absolute_import
import sys
import platform
from pbs import CommandNotFound
try:
from pbs import lsb_release, uname
except CommandNotFound:
POSIX = False
else:
POSIX = True
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404
from django.contrib.contenttypes.models import ContentType
from django.db.models.loading import get_model
from django.http import Http404
from django.core.exceptions import PermissionDenied
from permissions.models import Permission
from .permissions import PERMISSION_INSTALLATION_DETAILS
from .models import Installation
def installation_details(request):
Permission.objects.check_permissions(request.user, [PERMISSION_INSTALLATION_DETAILS])
paragraphs = []
if POSIX:
paragraphs.append(_(u'POSIX OS'))
paragraphs.append(_(u'Distributor ID: %s') % lsb_release('-i','-s'))
paragraphs.append(_(u'Description: %s') % lsb_release('-d','-s'))
paragraphs.append(_(u'Release: %s') % lsb_release('-r','-s'))
paragraphs.append(_(u'Codename: %s') % lsb_release('-c','-s'))
paragraphs.append(_(u'System info: %s') % uname('-a'))
paragraphs.append(_(u'Platform: %s') % sys.platform)
paragraphs.append(_(u'Processor: %s') % platform.processor())
for name, instance in Installation().get_properties().items():
paragraphs.append('%s: %s' % (unicode(instance.label), instance.value))
print Installation().architecture
return render_to_response('generic_template.html', {
'paragraphs': paragraphs,
'title': _(u'Installation environment details')

View File

@@ -18,3 +18,4 @@ https://github.com/rosarior/python-gnupg/zipball/0.2.8
python-hkp==0.1.3
requests==0.13.1
pbs==0.105
psutil==0.5.0