#!/usr/bin/env python
from __future__ import unicode_literals

import db
import config
from clientjob.model import Job
from user.model import User
from main.model import Computer, OrganizativeUnit
from main.jobs import OpengnsysStartJob, OpengnsysStopJob

orm = db.connect()

# useradmin
admin_password = u'1234567'
admin = User(u'admin', admin_password, config.admin_email)
admin.state = u'active'
admin.super_user = True
orm.add(admin)
print (u"User '%s' was created with password '%s', use it for login. "
       u"You can (and should!) of course change it later") % (admin.name, admin_password)

orm.commit()
#testuser
test_user_password = u'1234567'
test_user = User(u'test', test_user_password, config.admin_email)
test_user.state = u'active'
orm.add(test_user)
print (u"User '%s' was created with password '%s', use it for login. "
       u"You can (and should!) of course change it later") % (test_user.name, test_user_password)

# user anonymous is needed so that permissions can be given to it. It will be in "disabled"
# state so it won't be possible to login with it.
anonymous_user = User(u'anonymous', u'', config.noreply_email)
orm.add(anonymous_user)


# some ou and computer examples.

uni = OrganizativeUnit(name=u'Universidad',
                       description=u'Universidad de Sevilla')
orm.add(uni)

facu = OrganizativeUnit(name=u'Facultad',
                        description=u'Facultad de Informatica')
orm.add(facu)

sugus = OrganizativeUnit(name=u'Aula A3.60',
                         description=u'Sugus')
orm.add(sugus)

alpha = OrganizativeUnit(name=u'Aula A4.10',
                         description = 'Sala Alpha')
orm.add(alpha)

fresa = Computer(name=u'fresa', mac=u'001122334455')
fresa.ip = u'192.168.56.101'
orm.add(fresa)

choco = Computer(name='chocolate', mac='001122334456', repository=True)
choco.ip = '192.168.1.3'
orm.add(choco)

ord1 = Computer(name='ord1', mac='661122334455', repository=True)
ord1.ip = '192.168.10.2'
orm.add(ord1)

ord2 = Computer(name='ord2', mac='661122334456')
ord2.ip = '192.168.10.3'
orm.add(ord2)

ord3 = Computer(name='ord3', mac='661122334457')
ord3.ip = '192.168.10.4'
orm.add(ord3)

orm.commit()

# uni -> facu +-> sugus +-> fresa
#             |         |-> choco *
#             |
#             +-> alpha +-> ord2
#             |         |-> ord3
#             +-> ord1 *

facu.parent = uni
sugus.parent = facu
alpha.parent = facu

fresa.ou = sugus
choco.ou = sugus
ord1.ou = facu
ord2.ou = alpha
ord3.ou = alpha

test_user.organizative_units = [sugus]

orm.commit()

# Reserve first 1024 jobs
orm.add(OpengnsysStartJob())
orm.add(OpengnsysStopJob())
for i in xrange(2, 1023):
    orm.add(Job())

orm.commit()
