Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions django-postgres/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
./django_postgres/venv
./.idea
.vscode
48 changes: 48 additions & 0 deletions django-postgres/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions django-postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
To run the application, you need [docker](https://docs.docker.com/get-docker/) installed in your system.

Once docker is installed, run the following commands - ```docker-compose build``` and ```docker-compose up``` to start the server.
The Django application container will be accessible at port 8000 and at url - `https://127.0.0.1:8000` whereas locally, PostgreSQL will be accessible at port 6000 and at port 5432 in the container.

To apply migrations, run the following commands - ```docker-compose exec web /bin/sh``` which opens a command input to the container and now run - ```python3 manage.py makemigrations``` and ```python3 manage.py migrate```.

Make the following requests to the respective endpoints -

1. `GET /user/` - To get all the data at once.
2. `GET /user/uuid/` - To get the data of any particular user.
3. `POST /user/` - To create a new user.
4. `PUT /user/uuid/` - To update an existing user.
5. `DELETE /user/uuid/` - To delete an existing user.

The User model has the following fields -

1. id - UUID Field
2. name - String Field
3. email - Email Field
4. password - String Field
5. website - String Field

Pick up the data from `data.json` file to perform CRUD operation.
26 changes: 26 additions & 0 deletions django-postgres/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"name": "John Doe",
"email": "john.doe@example.com",
"password": "password123",
"website": "www.johndoe.com"
},
{
"name": "Jane Smith",
"email": "jane.smith@example.com",
"password": "smith567",
"website": "www.janesmith.com"
},
{
"name": "Alice Johnson",
"email": "alice.johnson@example.com",
"password": "alicePass",
"website": "www.alicejohnson.com"
},
{
"name": "Bob Brown",
"email": "bob.brown@example.com",
"password": "brownPass789",
"website": "www.bobbrown.com"
}
]
26 changes: 26 additions & 0 deletions django-postgres/django_postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use an official Python runtime as a parent image
FROM python:3.11.5-bullseye

# Set environment variables
ENV PYTHONUNBUFFERED 1

RUN apt-get update && apt-get install -y libpq-dev

# Create and set the working directory
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt /app/

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Copy the rest of your application code into the container
COPY . /app/

# Expose the port your application runs on
EXPOSE 8000

# Define the command to run your application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions django-postgres/django_postgres/application/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from .models import User
# Register your models here.

admin.site.register(User)
6 changes: 6 additions & 0 deletions django-postgres/django_postgres/application/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ApplicationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'application'
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.5 on 2023-09-11 05:15

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('userid', models.UUIDField(auto_created=True, primary_key=True, serialize=False, verbose_name='User ID')),
('name', models.CharField(max_length=50, verbose_name='User Name')),
('email', models.EmailField(max_length=254, verbose_name='User Email')),
('password', models.CharField(max_length=50, verbose_name='Password')),
('website', models.CharField(max_length=20, verbose_name='User Website')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-09-11 05:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('application', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='user',
old_name='userid',
new_name='id',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-09-11 15:10

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('application', '0002_rename_userid_user_id'),
]

operations = [
migrations.AlterField(
model_name='user',
name='website',
field=models.CharField(max_length=50, verbose_name='User Website'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.5 on 2023-09-12 19:01

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('application', '0003_alter_user_website'),
]

operations = [
migrations.AlterField(
model_name='user',
name='id',
field=models.UUIDField(auto_created=True, default=uuid.uuid4, primary_key=True, serialize=False, verbose_name='User ID'),
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions django-postgres/django_postgres/application/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db import models
from uuid import uuid4


class User(models.Model):
id = models.UUIDField(verbose_name="User ID", auto_created=True, primary_key=True, default=uuid4)
name = models.CharField(verbose_name='User Name', max_length=50, blank=False, null=False)
email = models.EmailField(verbose_name='User Email', blank=False, null=False)
password = models.CharField(verbose_name='Password', max_length=50, blank=False, null=False)
website = models.CharField(verbose_name="User Website", max_length=50)

def __str__(self):
return self.name
9 changes: 9 additions & 0 deletions django-postgres/django_postgres/application/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers
from .models import User


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("id", 'name', 'email', 'password', 'website')

3 changes: 3 additions & 0 deletions django-postgres/django_postgres/application/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.
8 changes: 8 additions & 0 deletions django-postgres/django_postgres/application/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from . import views


urlpatterns = [
path('<uuid>/', views.get_update_deleteUser, name="get_update_deleteUser"), # get, put, delete
path('', views.getAll_createUser, name='getAll_createUser') # get all, post
]
48 changes: 48 additions & 0 deletions django-postgres/django_postgres/application/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.shortcuts import render
from .models import User
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .serializers import UserSerializer
from uuid import uuid4
from django.http import JsonResponse
import json


@api_view(["GET", "PUT", "DELETE"])
def get_update_deleteUser(request, uuid: uuid4):

if request.method == "GET":
data = UserSerializer(User.objects.get(id=uuid)).data
return Response(data)

if request.method == "PUT":
user = User.objects.get(id=uuid)

user.name=request.data['name']
user.email=request.data['email']
user.password=request.data['password']
user.website=request.data['website']
user.save()

return JsonResponse({"message": "User Updated!!"})

if request.method == "DELETE":
User.objects.get(id=uuid).delete()
return JsonResponse({"message": "User Deleted!!"})

@api_view(["GET", "POST"])
def getAll_createUser(request):

if request.method == "GET":
data = UserSerializer(User.objects.all(), many=True)
return Response(data.data)

if request.method == "POST":
new_user = User.objects.create(name=request.data['name'],
email=request.data['email'],
password=request.data['password'],
website=request.data['website'])
data = UserSerializer(new_user)
return JsonResponse({"message": "User Created!!"})


Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions django-postgres/django_postgres/django_postgres/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for django_postgres project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_postgres.settings')

application = get_asgi_application()
Loading