<repo_name>DMDWSK/SelfEvaluation<file_sep>/authentication/models.py
# from django.db import models
# from allactions import Departments
#
# # Create your models here.
# class Users(models.Model):
#     class Meta:
#         db_table = "Users"
#
#     name = models.CharField(max_length=60, default=None)
#     surname = models.CharField(max_length=60, default=None)
#     role = models.ForeignKey('RolesModel', on_delete=models.DO_NOTHING)
#     department = models.ManyToManyField()
<file_sep>/allactions/models.py
from django.db import models


class Departments(models.Model):
    class Meta:
        db_table = "Departments"
        ordering = ['id']
    department = models.CharField(max_length=20, default=None)


class Sections(models.Model):
    class Meta:
        db_table = "Sections"
        ordering = ['id']
    section = models.CharField(max_length=20, default=None)


class Stages(models.Model):
    class Meta:
        db_table = "Stages"
        ordering = ['id']
    section = models.ForeignKey(Sections, on_delete=models.CASCADE)
    stage = models.CharField(max_length=40,  default=None)
    section = models.ForeignKey(Sections, on_delete=models.CASCADE)


class Questions(models.Model):
    class Meta:
        db_table = "Questions"
        ordering = ['id']
    question = models.TextField(max_length=255, default=None)
    stage = models.ForeignKey(Stages, on_delete=models.CASCADE)
    department = models.ManyToManyField(Departments)
    hint = models.TextField(max_length=300, null=True, blank=True)


class Grades(models.Model):
    class Meta:
        db_table = "Grades"
        ordering = ['id']
    grade = models.CharField(max_length=60, default=None)
<file_sep>/answers/models.py
from django.db import models
from django.contrib.auth.models import User


class UserAnswers(models.Model):
    class Meta:
        db_table = "UserAnswers"
        ordering = ['id']
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    question = models.ForeignKey('allactions.Questions',on_delete=models.CASCADE)
    answer = models.BooleanField(default=False)
    grade = models.ForeignKey('allactions.Grades',on_delete=models.CASCADE)
<file_sep>/allactions/views.py
from django.shortcuts import render, redirect
from .models import Stages, Questions, Sections, Grades
from answers.models import UserAnswers



def enter(request):
    return render(request, "enter.html")


def send(request, section_id):
    i = 0
    stages = Stages.objects.filter(section_id=section_id)
    questions = Questions.objects.all()
    sections = Sections.objects.all()
    grades = Grades.objects.all()
    context = {"sections": sections, "stages": stages, "questions": questions, "grades": grades}
    if request.method == 'POST':
        print(request.POST)
        usr_grd = request.POST.getlist('select_box')
        for g in usr_grd:
            i += 1
            choice = request.POST.get('choice-{}'.format(i), 'No')
            answers = UserAnswers()
            if choice == 'yes':
                answers.answer = True
            else:
                answers.answer = False
            answers.grade = Grades.objects.get(grade=g)
            answers.question_id = i
            answers.user_id = request.user.id
            answers.save()
        if section_id == len(sections):
            return render(request,'finish.html')
        return redirect('/{}'.format(section_id+1))
    return render(request, 'main.html', context)

<file_sep>/allactions/migrations/0001_initial.py
# Generated by Django 2.1.5 on 2019-01-30 15:47

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Departments',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('department', models.CharField(default=None, max_length=20)),
            ],
            options={
                'db_table': 'Departments',
            },
        ),
        migrations.CreateModel(
            name='Questions',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('question', models.TextField(default=None, max_length=255)),
            ],
            options={
                'db_table': 'Questions',
            },
        ),
        migrations.CreateModel(
            name='Sections',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('section', models.CharField(default=None, max_length=20)),
            ],
            options={
                'db_table': 'Sections',
            },
        ),
        migrations.CreateModel(
            name='Steps',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('step', models.CharField(default=None, max_length=20)),
                ('section', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='allactions.Sections')),
            ],
            options={
                'db_table': 'Steps',
            },
        ),
        migrations.AddField(
            model_name='questions',
            name='step',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='allactions.Steps'),
        ),
    ]
<file_sep>/database_python.py
#from . import models
import psycopg2
import datetime

def insert_table_ones(table, first_place, name):
    """ insert a new vendor into the vendors table """
    sql = """INSERT INTO "{}"({}) VALUES('{}');""".format(table, first_place, name)
    conn = None
    try:
        # connect to the PostgreSQL database
        conn = psycopg2.connect('dbname=formbase user=postgres password=<PASSWORD>')
        # create a new cursor
        cur = conn.cursor()
        # execute the INSERT statement
        cur.execute(sql)
        # commit the changes to the database
        conn.commit()
        # close communication with the database
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

def insert_table_users(table, place1st, place2nd, place3th, place4th, dat1st, data2nd, data3th):
    sql = """INSERT INTO "{}"({}, {}, {}, {}, {})
             VALUES('{}', '{}', '{}', '{}');""".format(table, place1st, place2nd, place3th, place4th, dat1st, data2nd, data3th, datetime.date.today())
    conn = None
    try:
        # connect to the PostgreSQL database
        conn = psycopg2.connect('dbname=formbase user=postgres password=root')
        # create a new cursor
        cur = conn.cursor()
        # execute the INSERT statement
        cur.execute(sql)
        # commit the changes to the databasecd
        conn.commit()
        # close communication with the database
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

def insert_table_usersdata(table, place1st, place2nd, place3th, place4th, place5th, dat1st, data2nd, data3th, data4th, data5th):
    sql = """INSERT INTO "{}"({}, {}, {}, {}, {})
             VALUES('{}', '{}', '{}', '{}', '{}');""".format(table, place1st, place2nd, place3th, place4th, place5th, dat1st, data2nd, data3th, data4th, data5th)
    conn = None
    try:
        # connect to the PostgreSQL database
        conn = psycopg2.connect('dbname=formbase user=postgres password=root')
        # create a new cursor
        cur = conn.cursor()
        # execute the INSERT statement
        cur.execute(sql)
        # commit the changes to the databasecd
        conn.commit()
        # close communication with the database
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

def insert_from_file():
    sql_que_tlt = """INSERT INTO "questions"(question, course_id, stage_id, tooltip) VALUES(%s, 1, %s, %s);"""
    sql_stg = """INSERT INTO "stages"(stage, part) VALUES(%s, %s) RETURNING id;"""
    sql_que = """INSERT INTO "questions"(question, course_id, stage_id) VALUES(%s, 1, %s);"""
    conn = None
    try:
        conn = psycopg2.connect('dbname=formbase user=postgres password=<PASSWORD>')
        cur = conn.cursor()

        file = open('questions.txt', 'r')
        flag = 'Skills'
        flag_dict = {'Skills':'Activities', 'Activities':'Skills'}
        stage_id = 0
        for row in file:
            row = row.replace('\n', '').replace("'", "''")
            if row[0] is '#' and row[1] is '#':
                flag = flag_dict[flag]
            elif row[0] is '#':
                cur.execute(sql_stg, (row[1:], flag))
                conn.commit()
                stage_id = cur.fetchone()[0]
            else:
                if flag is 'Activities':
                    if row[0] is not '*':
                        sql = row
                        continue
                    cur.execute(sql_que_tlt, (sql, stage_id, row[1:]))
                    conn.commit()
                    continue
                cur.execute(sql_que, (row, stage_id))
                conn.commit()

        cur.close()
        file.close()

    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()

# insert_table_ones('courses', 'course', 'Python')
# insert_table_ones('courses', 'course', '.NET')
# insert_table_ones('courses', 'course', 'Java')
# insert_table_ones('roles', 'role', 'Admin')
# insert_table_ones('roles', 'role', 'User')
# insert_table_ones('roles', 'role', 'Expert')
# insert_table_ones('roles', 'role', 'Recruiter')
# insert_table_ones('grades', 'grade', 'None')
# insert_table_ones('grades', 'grade', 'Beginner')
# insert_table_ones('grades', 'grade', 'Good')
# insert_table_ones('grades', 'grade', 'Strong')
# insert_table_users('authentication', 'login', 'password', 'role_id', 'date', 'admin', 'admin', '1')
# insert_table_usersdata('usersdata', 'name', 'last', 'age', 'email', 'user_id', 'Andre', 'UA', 33, '<EMAIL>', 1)
insert_from_file()<file_sep>/roleaccess/migrations/0001_initial.py
# Generated by Django 2.1.5 on 2019-01-31 13:14

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Permissions',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('permission', models.CharField(default=None, max_length=100)),
            ],
            options={
                'db_table': 'Permisisons',
            },
        ),
        migrations.CreateModel(
            name='Roles',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('role', models.CharField(default=None, max_length=60)),
                ('permission', models.ManyToManyField(to='roleaccess.Permissions')),
            ],
            options={
                'db_table': 'Roles',
            },
        ),
    ]
<file_sep>/allactions/migrations/0009_auto_20190202_1841.py
# Generated by Django 2.1.5 on 2019-02-02 16:41

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('allactions', '0008_auto_20190202_1836'),
    ]

    operations = [
        migrations.AlterField(
            model_name='questions',
            name='hint',
            field=models.TextField(blank=True, default=None, max_length=300),
        ),
    ]
<file_sep>/allactions/migrations/0003_auto_20190131_1533.py
# Generated by Django 2.1.5 on 2019-01-31 13:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('allactions', '0002_grades'),
    ]

    operations = [
        migrations.RenameModel(
            old_name='Steps',
            new_name='Stages',
        ),
        migrations.RenameField(
            model_name='questions',
            old_name='step',
            new_name='stage',
        ),
        migrations.RenameField(
            model_name='stages',
            old_name='step',
            new_name='stage',
        ),
        migrations.AddField(
            model_name='sections',
            name='department',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='allactions.Departments'),
            preserve_default=False,
        ),
        migrations.AlterModelTable(
            name='stages',
            table='Stages',
        ),
    ]
<file_sep>/roleaccess/models.py
from django.db import models


class Permissions(models.Model):
    class Meta:
        db_table = "Permisisons"
    permission = models.CharField(max_length=100, default=None)

class Roles(models.Model):
    class Meta:
        db_table = "Roles"
    role = models.CharField(max_length=60, default=None)
    permission = models.ManyToManyField(Permissions)
<file_sep>/allactions/urls.py
from django.urls import path,include
from . import views

urlpatterns = [
    path('', views.enter),
    path('<int:section_id>', views.send, name='section_page'),
]
<file_sep>/allactions/migrations/0007_auto_20190201_1834.py
# Generated by Django 2.1.5 on 2019-02-01 16:34

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('allactions', '0006_auto_20190201_1651'),
    ]

    operations = [
        migrations.AlterField(
            model_name='stages',
            name='stage',
            field=models.CharField(default=None, max_length=40),
        ),
    ]
<file_sep>/allactions/migrations/0011_auto_20190208_1549.py
# Generated by Django 2.1.5 on 2019-02-08 13:49

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('allactions', '0010_auto_20190202_1848'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='departments',
            options={'ordering': ['id']},
        ),
        migrations.AlterModelOptions(
            name='grades',
            options={'ordering': ['id']},
        ),
        migrations.AlterModelOptions(
            name='questions',
            options={'ordering': ['id']},
        ),
        migrations.AlterModelOptions(
            name='sections',
            options={'ordering': ['id']},
        ),
        migrations.AlterModelOptions(
            name='stages',
            options={'ordering': ['id']},
        ),
    ]
