Skip to content

Instantly share code, notes, and snippets.

View mansha99's full-sized avatar

Manish Sharma mansha99

View GitHub Profile
SECRET_MESSAGE=This is a very very secret message. Do not expose.
@mansha99
mansha99 / settings.py
Last active July 8, 2023 11:53
settings.py
import os
import environ #add this
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Add All lines given below
env = environ.Env(
DEBUG=(bool, False)
)
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# MESSAGE variable is getting its value from .env file
@mansha99
mansha99 / views.py
Created July 8, 2023 11:54
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
def hello_world(request):
# READ value from settings.py
message = settings.SECRET_MESSAGE
return HttpResponse(message)
@mansha99
mansha99 / project_app_signals.py
Last active July 9, 2023 09:03
project/app/signals.py
from django import dispatch
user_visit_signal = dispatch.Signal(["date_time","ip","device"])
@mansha99
mansha99 / project_app_receivers.py
Created July 9, 2023 09:04
project_app_receivers.py
from django.dispatch import receiver
from helloapp import signals
@receiver(signals.user_visit_signal)
def handle_user_visit(sender, date_time,ip,device, **kwargs):
message = "Visit to view {sender}, at {date_time} , IP is {ip} , Device is {device}".format(sender=sender,date_time=date_time,ip=ip,device=device)
print(message)
@mansha99
mansha99 / project_app_apps.py
Created July 9, 2023 09:06
project_app_apps.py
from django.apps import AppConfig
class HelloappConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "helloapp"
def ready(self):
from helloapp import receivers
@mansha99
mansha99 / project_app_views.py
Created July 9, 2023 09:08
project_app_views.py
from django.http import HttpResponse
from helloapp import signals
from django.utils import formats
from rest_framework.decorators import api_view
from django.http import JsonResponse
import datetime
def hello_world(request):
message = "Hello World"
date_time = datetime.datetime.now()
@mansha99
mansha99 / project_app_models.py
Created July 9, 2023 09:15
project_app_models.py
from django.db import models
from helloapp import signals
class Notice(models.Model):
title = models.CharField(max_length=80)
description = models.CharField(max_length=255)
def save(self, *args, **kwargs):
if self.title=="Oops":
signals.invalid_notice_signal.send(sender=None)
return
else:
@mansha99
mansha99 / project_app_receivers.py
Last active July 9, 2023 09:19
project_app_receivers.py
from django.dispatch import receiver
from helloapp import signals
from .models import Notice
from django.db.models import signals as modelSignals
#Predefined Model signal
@receiver(modelSignals.post_save, sender=Notice)
def valid_notice_created(sender, instance, created, **kwargs):
if created:
message = "Notice Created : id = {id} ".format(id=instance.id)
@mansha99
mansha99 / models.py
Created July 20, 2023 05:07
consultant/models.py
from django.db import models
class Consultant(models.Model):
name = models.CharField(max_length=255)
specialization = models.CharField(max_length=64)
def __str__(self):
return self.name
class Patient(models.Model):
name = models.CharField(max_length=255)