se agrega create-tables

francisco/prueba1
Francisco Sandoval 2023-06-10 08:35:22 -04:00
parent 5d208aee78
commit 4505c74f4e
7 changed files with 147 additions and 8 deletions

View File

@ -0,0 +1,67 @@
# Generated by Django 4.2.2 on 2023-06-10 12:31
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Agency',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('agency_id', models.CharField(max_length=50)),
('agency_name', models.CharField(max_length=50)),
('agency_url', models.CharField(max_length=200)),
('agency_timezone', models.CharField(max_length=20)),
('agency_lang', models.CharField(blank=True, max_length=10, null=True)),
('agency_phone', models.CharField(blank=True, max_length=20, null=True)),
('agency_fare_url', models.CharField(blank=True, max_length=255, null=True)),
('agency_email', models.CharField(blank=True, max_length=255, null=True)),
],
options={
'verbose_name': 'Agencia',
'verbose_name_plural': 'Agencias',
'db_table': 'agencies',
'managed': False,
},
),
migrations.CreateModel(
name='Project',
fields=[
('project_id', models.AutoField(primary_key=True, serialize=False)),
('project_name', models.CharField(max_length=50)),
],
options={
'verbose_name': 'Proyecto',
'verbose_name_plural': 'Proyectos',
'db_table': 'projects',
'managed': False,
},
),
migrations.CreateModel(
name='Route',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('route_id', models.CharField(max_length=50)),
('route_short_name', models.CharField(blank=True, max_length=50, null=True)),
('route_long_name', models.CharField(blank=True, max_length=200, null=True)),
('route_desc', models.CharField(blank=True, max_length=50, null=True)),
('route_type', models.IntegerField()),
('route_url', models.CharField(blank=True, max_length=200, null=True)),
('route_color', models.CharField(blank=True, max_length=10, null=True)),
('route_text_color', models.CharField(blank=True, max_length=10, null=True)),
],
options={
'verbose_name': 'Ruta',
'verbose_name_plural': 'Rutas',
'db_table': 'routes',
'managed': False,
},
),
]

View File

@ -54,4 +54,4 @@ class Route(models.Model):
managed = False
db_table = 'routes'
verbose_name = 'Ruta'
verbose_name_plural = 'Rutas'
verbose_name_plural = 'Rutas'

View File

@ -22,3 +22,10 @@ class UserSerializer(serializers.ModelSerializer):
model = User
fields = ('id','username','first_name','last_name','email')
class AuthSerializer(serializers.Serializer):
username = serializers.Field(source = 'user.username')
password = serializers.Field(source = 'user.password')
def to_representation(self, instance):
# Implement serialization logic here
pass

View File

@ -2,12 +2,21 @@ from django.urls import path, include
from rest_framework import routers
from api import views
# token
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
router = routers.DefaultRouter()
router.register(r'projects', views.ProjectViewSet)
router.register(r'agencies', views.AgencyViewSet)
router.register(r'routes', views.RouteViewSet)
router.register(r'users', views.UserViewSet)
router.register(r'auth', views.AuthViewSet, basename='auth')
urlpatterns = [
path('', include(router.urls))
path('', include(router.urls)),
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]

View File

@ -1,8 +1,9 @@
# from django.shortcuts import render
import jwt
from django.contrib.auth.models import User
from rest_framework.response import Response
from rest_framework import viewsets
from .serializers import ProjectSerializer, AgencySerializer, RouteSerializer
from .serializers import UserSerializer
from .serializers import UserSerializer, AuthSerializer
from .models import Project, Agency, Route
# Create your views here.
@ -20,4 +21,23 @@ class RouteViewSet(viewsets.ModelViewSet):
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
serializer_class = UserSerializer
"""
Metodos de generacion de jwt en forma manual
"""
class AuthViewSet(viewsets.ViewSet):
serializer_class = AuthSerializer
def list(self, request):
return Response()
def create(self, request):
username = request.data.get('username')
user = User.objects.filter(username = username).first()
private_key = 'lapalabrasecreta'
payload = {
'user_id': user.username
}
token = jwt.encode(payload, private_key, algorithm="HS256")
return Response({ 'token': token })

View File

@ -0,0 +1,32 @@
set search_path to desarrollo1;
CREATE TABLE if not exists projects (
"project_id" serial NOT NULL primary key,
"project_name" character varying(50) NOT NULL
);
CREATE TABLE if not exists agencies (
"id" bigserial not null primary key,
"agency_id" character varying(50) NOT NULL,
"agency_name" character varying(50) NOT NULL,
"agency_url" character varying(200) NOT NULL,
"agency_timezone" character varying(20) NOT NULL,
"agency_lang" character varying(10),
"agency_phone" character varying(20),
"agency_fare_url" character varying(255),
"agency_email" character varying(255),
"project_id" integer NOT NULL references projects
);
CREATE TABLE if not exists routes (
"id" bigserial not null primary key,
"route_id" character varying(50) NOT NULL,
"route_short_name" character varying(50),
"route_long_name" character varying(200),
"route_desc" character varying(50),
"route_type" integer NOT NULL,
"route_url" character varying(200),
"route_color" character varying(10),
"route_text_color" character varying(10),
"agency_id" bigint NOT NULL references agencies (id)
);

View File

@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework_simplejwt',
'rest_framework',
'coreapi',
'api'
@ -116,7 +117,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'es-cl'
TIME_ZONE = 'UTC'
TIME_ZONE = 'America/Santiago'
USE_I18N = True
@ -135,5 +136,8 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'
}
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}