Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

backend-task-done-till-week-2 #50

Open
wants to merge 9 commits into
base: best-backend
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions author.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "",
"entry_number": ""
"name": "Harsh Saini",
"entry_number": "2020PH10692"
}
Binary file added backend/mydjangoproject/.DS_Store
Binary file not shown.

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

8 changes: 8 additions & 0 deletions backend/mydjangoproject/.idea/modules.xml

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

8 changes: 8 additions & 0 deletions backend/mydjangoproject/.idea/mydjangoproject.iml

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

Binary file added backend/mydjangoproject/db.sqlite3
Binary file not shown.
Binary file added backend/mydjangoproject/firstApp/.DS_Store
Binary file not shown.
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.
5 changes: 5 additions & 0 deletions backend/mydjangoproject/firstApp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from .models import Product

# Register your models here.
admin.site.register(Product)
5 changes: 5 additions & 0 deletions backend/mydjangoproject/firstApp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig

class FirstappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'firstApp'
22 changes: 22 additions & 0 deletions backend/mydjangoproject/firstApp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.3 on 2023-07-16 13:43

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200)),
('description', models.CharField(max_length=500)),
],
),
]
18 changes: 18 additions & 0 deletions backend/mydjangoproject/firstApp/migrations/0002_product_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.3 on 2023-07-16 14:01

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='product',
name='price',
field=models.IntegerField(default=0),
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions backend/mydjangoproject/firstApp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models

# Create your models here.
class Product(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length=200)
description = models.CharField(max_length=500)
price = models.IntegerField(default=0)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ product.description }} for Rs. {{ product.price }}
34 changes: 34 additions & 0 deletions backend/mydjangoproject/firstApp/templates/firstApp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body style="padding: 20px">
{% if product_list %}
<ul style="list-style-type: none; padding-left: 0px">
{% for product in product_list %}
<li><a href="/{{ product.id }}/">{{ product.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No products are available.</p>
{% endif %}

{% if user.is_authenticated %}
<a href="{% url 'save_product' %}" class="btn btn-primary">Add product</a>
<a href="{% url 'logout' %}" class="btn btn-secondary">Logout</a>
{% else %}
<a href="{% url 'save_product' %}" class="btn btn-primary">Log in to add product</a>
{% endif %}

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions backend/mydjangoproject/firstApp/templates/firstApp/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<form style="padding: 20px" method="post">
{% csrf_token %}
<div class="form-group">
<label for="exampleInputText1">Username</label>
<input type="text" class="form-control" id="exampleInputText1" placeholder="Enter Username" name="username">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Enter Password" name="password">
</div>
<button type="submit" class="btn btn-primary">Log In</button> <br> <br>
<a href="{% url 'signup' %}">Don't have an account? Sign Up here!</a>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<form style="padding: 20px" method="post">
{% csrf_token %}
<div class="form-group">
<label for="exampleInputText1">Name</label>
<input type="text" class="form-control" id="exampleInputText1" placeholder="Enter product name" name="name">
</div>
<div class="form-group">
<label for="exampleInputDescription1">Description</label>
<input type="text" class="form-control" id="exampleInputDescription1" placeholder="Enter product description" name="description">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Price</label>
<input type="number" class="form-control" id="exampleInputPassword1" placeholder="Enter product price" name="price">
</div>
<button type="submit" class="btn btn-primary">Add Product</button>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
27 changes: 27 additions & 0 deletions backend/mydjangoproject/firstApp/templates/firstApp/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<form style="padding: 20px" method="post">
{% csrf_token %}
<div class="form-group">
<label for="exampleInputText1">Username</label>
<input type="text" class="form-control" id="exampleInputText1" placeholder="Username" name="username">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" name="email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password">
</div>
<button type="submit" class="btn btn-primary">Sign Up</button> <br> <br>
<a href="{% url 'loginPage' %}">Already have an account? Log In here!</a>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
3 changes: 3 additions & 0 deletions backend/mydjangoproject/firstApp/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.
12 changes: 12 additions & 0 deletions backend/mydjangoproject/firstApp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path

from . import views

urlpatterns = [
path("", views.index, name="index"),
path("login/", views.login, name="loginPage"),
path("signup/", views.signup, name="signup"),
path("save_product", views.save_product, name="save_product"),
path("logout/", views.logout, name="logout"),
path("<int:id>/", views.detail, name="detail"),
]
61 changes: 61 additions & 0 deletions backend/mydjangoproject/firstApp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.http import Http404
from .models import Product
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login as auth_login, logout as logout_user
from django.contrib.auth.decorators import login_required


def index(request):
product_list = Product.objects.all()
context = {"product_list": product_list}
return render(request, "firstApp/index.html", context)


def detail(request, id):
try:
product = Product.objects.get(pk=id)
except Product.DoesNotExist:
raise Http404("Product does not exist")
return render(request, "firstApp/detail.html", {"product": product})


def signup(request):
if request.method == 'POST':
uname = request.POST.get('username')
email1 = request.POST.get('email')
passw = request.POST.get('password')
my_user = User.objects.create_user(username=uname, email=email1, password=passw)
my_user.save()
return redirect('loginPage')
return render(request, "firstApp/signup.html")


def login(request):
if request.method == 'POST':
uname = request.POST.get('username')
passw = request.POST.get('password')
user = authenticate(request, username=uname, password=passw)
if user is not None:
auth_login(request, user)
return redirect('index')
else:
return HttpResponse("Kuch bhi mat daal")
return render(request, "firstApp/login.html")


def logout(request):
logout_user(request)
return redirect('index')

@login_required(login_url='loginPage')
def save_product(request):
if request.method == 'POST':
name = request.POST.get('name')
desc = request.POST.get('description')
price = request.POST.get('price')
p = Product(name=name, description=desc, price=price)
p.save()
return redirect('index')
return render(request, "firstApp/saveProduct.html")
22 changes: 22 additions & 0 deletions backend/mydjangoproject/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mydjangoproject.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions backend/mydjangoproject/mydjangoproject/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for mydjangoproject 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', 'mydjangoproject.settings')

application = get_asgi_application()
Loading