Skip to content

Commit

Permalink
Including the Dashboard module
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Mendez Quintero committed Jun 29, 2023
1 parent 8130064 commit 1a95502
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 4 deletions.
11 changes: 8 additions & 3 deletions AdAnalyzer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"pages.apps.PagesConfig",
"dashboard"
]

MIDDLEWARE = [
Expand Down Expand Up @@ -75,9 +76,13 @@
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'adana',
'USER': 'adana',
'PASSWORD': 'adana',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

Expand Down
Empty file added dashboard/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions dashboard/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Empresa, Competencia

admin.site.register(Empresa)
admin.site.register(Competencia)

5 changes: 5 additions & 0 deletions dashboard/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig

class DashboardConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "dashboard"
61 changes: 61 additions & 0 deletions dashboard/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 4.2.2 on 2023-06-28 19:26

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


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Empresa",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("nombre", models.CharField(max_length=200)),
],
),
migrations.CreateModel(
name="Competencia",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"competidor",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="competidor",
to="dashboard.empresa",
),
),
(
"empresa",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="empresa",
to="dashboard.empresa",
),
),
],
options={
"unique_together": {("empresa", "competidor")},
},
),
]
19 changes: 19 additions & 0 deletions dashboard/migrations/0002_empresa_usuarios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.2 on 2023-06-28 20:01

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("dashboard", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="empresa",
name="usuarios",
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL),
),
]
Empty file.
21 changes: 21 additions & 0 deletions dashboard/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db import models
from django.contrib.auth.models import User

#This is the model for Companies that are going to be registered even thought they are not clients in the system
class Empresa(models.Model):
nombre = models.CharField(max_length=200)
usuarios = models.ManyToManyField(User)

def __str__(self):
return self.nombre

#This is the model for the competitors it is a relation of a Company to another.
class Competencia(models.Model):
empresa = models.ForeignKey(Empresa, related_name='empresa', on_delete=models.CASCADE)
competidor = models.ForeignKey(Empresa, related_name='competidor', on_delete=models.CASCADE)

class Meta:
unique_together = ('empresa', 'competidor')

def __str__(self):
return f"{self.empresa.nombre} competidor: {self.competidor.nombre}"
3 changes: 3 additions & 0 deletions dashboard/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
26 changes: 26 additions & 0 deletions dashboard/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.shortcuts import render, redirect
from .models import Empresa
from django.contrib.auth.models import User

def asignar_usuarios(request, empresa_id):
empresa = Empresa.objects.get(id=empresa_id)
if request.method == 'POST':
usuarios_id = request.POST.getlist('usuarios')
usuarios = User.objects.filter(id__in=usuarios_id)
empresa.usuarios.set(usuarios)

# obtén la referencia al permiso de "view" para el modelo User
permiso_ver_usuario = Permission.objects.get(codename='view_user')

# para cada usuario de la empresa, agrega este permiso
for usuario in empresa.usuarios.all():
usuario.user_permissions.add(permiso_ver_usuario)

return redirect('dashboard:empresa_detalle', empresa_id=empresa_id)
else:
usuarios = User.objects.all()
return render(request, 'asignar_usuarios.html', {'empresa': empresa, 'usuarios': usuarios})

def inicio(request):
return render(request, 'inicio.html')

2 changes: 1 addition & 1 deletion pages/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f0f0f0;
background-color: #aaaaaa;
}
.container {
max-width: 800px;
Expand Down

0 comments on commit 1a95502

Please sign in to comment.