Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Endpoints #19

Open
wants to merge 15 commits into
base: cpanel
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ __pycache__/
.vscode/
venv/
*.pyc
.idea
db.sqlite3
stderr.log
1 change: 1 addition & 0 deletions ACM_Website/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'acm.my_middleware.CustomCorsMiddleware'
]

ROOT_URLCONF = 'ACM_Website.urls'
Expand Down
23 changes: 20 additions & 3 deletions SMP/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
from django.contrib import admin
from acm.models import SIG
from django.urls import path
from .models import *
from SMP.views import new_smp

admin.site.register(SMP)
admin.site.register(SMP_des)

@admin.register(SMP)
class SMPAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
return False

def get_urls(self):
urls = super().get_urls()
urls = [
path('add_smp/', self.admin_site.admin_view(new_smp))
] + urls
return urls


@admin.register(SMP_des)
class SMP_desAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
return False
10 changes: 10 additions & 0 deletions SMP/templates/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "admin/index.html" %}

{% block content %}
<ul class="object-tools">
<li>
<a class="addlink" href="/admin/SMP/smp/add_smp">Add SMP</a>
</li>
</ul>
{{ block.super }}
{% endblock %}
26 changes: 8 additions & 18 deletions SMP/templates/smp_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
{% endblock %}
{% block content %}
<br><br><br><br><br>
<!-- If password validation is done, show the SMP form-->
{% if validated %}
<form action="" method="POST" id="main_content">
{% csrf_token %}

Expand All @@ -47,8 +45,8 @@
{% for f in smp_form %}
{% if f.name in static_fields %}
<div class="container center_div">
<div class="form-group row" align="center">
<label for="{{f.name}}">{{f.name}}</label>
<div class="form-group row" align="center">
<label for="{{f.name}}">{{f.name}}</label>
<input type="text" class="form-control" name="{{f.name}}" maxlength="{{f.max_length}}" id="{{f.name}}">
</div>
</div>
Expand All @@ -57,16 +55,16 @@
{% endif %}
{% endfor %}

{% for field in dynammic_fields %}
<div class="container center_div">
{% for field in dynamic_fields %}
<div class="container center_div">
<h3>{{field}}</h3>
</div>
{% for f in smp_form %}
{% if field in f.name %}
{% if f.is_hidden %}
<div class="form-group" id="{{f.name}}_div" style="display:none;">
<div class="form-group" id="{{f.name}}_div" style="display:none;">
{% else %}
<div class="form-group" id="{{f.name}}_div" >
<div class="form-group" id="{{f.name}}_div" >
{% endif %}
<div class="container center_div">
<label for="{{f.name}}">{{f.label}}</label>
Expand Down Expand Up @@ -98,9 +96,9 @@ <h3>Weekly Plan</h3>
{% concatenate "Week_" i "_Description" as q %}
{% if q in f.name %}
{% if f.is_hidden %}
<div class="form-group" id="{{f.name}}_div" style="display:none;">
<div class="form-group" id="{{f.name}}_div" style="display:none;">
{% else %}
<div class="form-group" id="{{f.name}}_div" >
<div class="form-group" id="{{f.name}}_div" >
{% endif %}
<div class="container center_div">
<label for="{{f.name}}">{{f.label}}</label>
Expand All @@ -124,14 +122,6 @@ <h3>Weekly Plan</h3>
<button type="submit">Submit</button>
</div>
<form>
<!-- If password validation is not done, show the password form-->
{% else %}
<form method="POST" action="">
{% csrf_token %}
{{password_form}}
<button type="submit">Submit</button>
</form>
{% endif %}
{% endblock %}


4 changes: 1 addition & 3 deletions SMP/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from django.urls import path
from .views import *

app_name = 'SMP'
urlpatterns = [
path('', index),
path('<int:sig_id>/', home),
path('<int:sig_id>/<int:smp_id>/', des),
path('new-smp/', new_smp, name="new_smp"),
path('<int:sig_id>/<int:smp_id>/', des)
]
74 changes: 35 additions & 39 deletions SMP/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from django.shortcuts import render
from django.contrib import messages
from django.shortcuts import render, redirect
from .models import *
from acm.models import *
from django.http import HttpResponse
from django.http import JsonResponse
import json
from .forms import *
from django.shortcuts import redirect
from django.contrib import messages


form_links = ["https://forms.gle/D5nX9xEC716RNcey9", "https://forms.gle/j2sXQGktrSXZMoee8", "https://forms.gle/PRFU1w8FvK3bsH9YA", "https://forms.gle/DLQca6S7UM2qqh7j6", "https://forms.gle/ipCJj8NEUfMdmzGs7", "https://forms.gle/gQZkVYJrFiaDDiKm7"]
Expand All @@ -16,33 +15,33 @@ def index(request) :


def home(request, sig_id):
si = SIG.objects.filter(pk=sig_id)
sig = si[0]
sigo = SIG.objects.all()
smps = SMP.objects.filter(sig_id=sig_id)
data2= open('staticfiles/acm/json/smp.json',encoding='utf-8').read()
data2= json.loads(data2)
data = ''
for i in range(6):
if(int(data2[i]['id']) == sig_id):
data = data2[i]
contex = {'sig': sig, 'sigo': sigo, 'smps': smps,'data':data, 'link':form_links[sig_id-1]}
return render(request, 'spms.html', contex)
sig = SIG.objects.filter(pk=sig_id).values('id', 'name', 'image', 'mission_statement', 'vision_statement')[0]
sigo = all_sigs()
smps = list(SMP
.objects
.filter(sig_id=sig_id).values('id', 'sig_id', 'name', 'mentors', 'overview', 'platform_of_tutoring'))
with open('staticfiles/acm/json/smp.json') as f:
data2 = json.loads(f.read())
data = ''
for i in range(5):
if int(data2[i]['id']) == sig_id:
data = data2[i]
context = {'sig': sig, 'sigo': sigo, 'smps': smps, 'data': data}
return JsonResponse(context)


def des(request, sig_id, smp_id):
smp = (SMP.objects.filter(pk=smp_id))[0]
smp_des = SMP_des.objects.filter(smp_id=smp_id)
contex = {'smp': smp, 'smp_des': smp_des}
return render(request, 'smp_des.html', contex)
smp = SMP.objects.filter(pk=smp_id).values('id', 'sig_id', 'name', 'mentors', 'overview', 'platform_of_tutoring')[0]
smp_des = list(SMP_des.objects.filter(smp_id=smp_id).values('id', 'smp_id', 'sub_heading', 'sub_des'))
context = {'smp': smp, 'smp_des': smp_des}
return JsonResponse(context)


def new_smp(request):
validated = 0
static_fields = ['Name', 'Mentors', 'Overview', 'Platform']
dynammic_fields = ['Exercise', 'Prerequisite', 'Course-content']
dynamic_fields = ['Exercise', 'Prerequisite', 'Course-content']
if request.method == "POST":
password_form = PasswordForm(request.POST)
smp_form = SMPForm(request.POST)
if smp_form.is_valid():
sig = smp_form.cleaned_data["SIG"]
Expand All @@ -51,47 +50,44 @@ def new_smp(request):
overview = smp_form.cleaned_data["Overview"]
platform = smp_form.cleaned_data["Platform"]
SMP_object = SMP.objects.create(
sig_id=SIG.objects.get(pk=sig), name=name, mentors=mentors, overview=overview, platform_of_tutoring=platform)
sig_id=SIG.objects.get(pk=sig), name=name, mentors=mentors, overview=overview,
platform_of_tutoring=platform)
SMP_object.save()

for field in dynammic_fields:
for field in dynamic_fields:
for i in range(smp_form.cleaned_data[field.lower() + "_freq"]):
des = smp_form.cleaned_data[field+'_' + str(i+1)]
des = smp_form.cleaned_data[field + '_' + str(i + 1)]
if des:
SMP_des_object = SMP_des.objects.create(
smp_id=SMP_object, sub_heading=field, sub_des=des)
SMP_des_object.save()

for i in range(smp_form.cleaned_data["week_freq"]):
for j in range(smp_form.cleaned_data["week_"+str(i+1)+"_description_freq"]):
for j in range(smp_form.cleaned_data["week_" + str(i + 1) + "_description_freq"]):
des = smp_form.cleaned_data["Week_" +
str(i+1)+"_Description_"+str(j+1)]
str(i + 1) + "_Description_" + str(j + 1)]
if des:
SMP_des_object = SMP_des.objects.create(
smp_id=SMP_object, sub_heading="Week "+str(i+1), sub_des=des)
smp_id=SMP_object, sub_heading="Week " + str(i + 1), sub_des=des)
SMP_des_object.save()

messages.success(request, "SMP created")
return redirect('acm:home_page')

if password_form.is_valid():
# To be hidden somehow
if password_form.cleaned_data["key"] == "s@ng@madethiz":
validated = 1
else:
messages.MessageFailure(request, 'Incorrect password')

else:
password_form = PasswordForm()
smp_form = SMPForm()

sigo = SIG.objects.all()
context = {'password_form': password_form,
'smp_form': smp_form,
context = {'smp_form': smp_form,
'validated': validated,
'static_fields': static_fields,
'dynammic_fields': dynammic_fields,
'dynamic_fields': dynamic_fields,
'range_of_8': [1, 2, 3, 4, 5, 6, 7, 8],
'sigo': sigo, }

return render(request, 'smp_form.html', context)


# Common block
def all_sigs():
return list(SIG.objects.all().values('id', 'name', 'image', 'mission_statement', 'vision_statement'))
1 change: 1 addition & 0 deletions acm/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
admin.site.register(Projects)
admin.site.register(Special_people)
admin.site.register(Proposals)
admin.site.register(ProposalTimeline)
1 change: 1 addition & 0 deletions acm/middleware/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import corsMiddleware
9 changes: 9 additions & 0 deletions acm/middleware/corsMiddleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class corsMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
response["Access-Control-Allow-Origin"] = "*"

return response
24 changes: 24 additions & 0 deletions acm/migrations/0015_add_image_to_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.0 on 2021-08-05 06:12
import django
from django.core.validators import FileExtensionValidator
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('acm', '0014_projects_knowledge'),
]

operations = [
migrations.AddField(
model_name='Events',
name='image',
field=models.ImageField(upload_to='uploads/events', blank=True, null=True, validators=[
FileExtensionValidator(['jpg', 'jpeg', 'png'])]),
),
migrations.AlterField(
model_name='Events',
name='sig_id',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='acm.SIG', null=True)
)
]
49 changes: 49 additions & 0 deletions acm/migrations/0016_alter_special_people.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 2.0 on 2021-08-07 06:57
from django.core.validators import FileExtensionValidator
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('acm', '0015_add_image_to_events'),
]

operations = [
migrations.RenameField(
model_name='Special_people',
old_name='fb_link',
new_name='email_link'
),
migrations.RemoveField(
model_name='Proposals',
name='members'
),
migrations.AddField(
model_name='Proposals',
name='learning_outcomes',
field=models.TextField(blank=True)
),
migrations.AddField(
model_name='Proposals',
name='results',
field=models.TextField(blank=True)
),
migrations.AddField(
model_name='Proposals',
name='image',
field=models.ImageField(upload_to='uploads/proposals', blank=True, null=True, validators=[
FileExtensionValidator(['jpg', 'jpeg', 'png'])]),
),
migrations.CreateModel(
name='ProposalTimeline',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('phase', models.TextField(blank=True)),
('tasks', models.TextField(blank=True)),
('start_date', models.DateField(blank=True)),
('end_date', models.DateField(blank=True)),
('proposal_id', models.ForeignKey(on_delete=models.deletion.CASCADE, to='acm.Proposals'))
]
)
]
Loading