Commit 59e9de5e authored by Gab De Jesus's avatar Gab De Jesus

Fixed AJAX bug where it wouldnt get all array locs, added css

parent e4d9f3ff
...@@ -136,7 +136,7 @@ STATICFILES_DIRS = [ ...@@ -136,7 +136,7 @@ STATICFILES_DIRS = [
# Tells django where to put static files after collectstatic # Tells django where to put static files after collectstatic
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
LOGIN_REDIRECT_URL = 'reviews_index' LOGIN_REDIRECT_URL = 'homepage'
LOGOUT_REDIRECT_URL = 'homepage' LOGOUT_REDIRECT_URL = 'homepage'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
\ No newline at end of file
...@@ -12,6 +12,7 @@ from profs import views as prof_views ...@@ -12,6 +12,7 @@ from profs import views as prof_views
from reviews.forms import ReviewForm from reviews.forms import ReviewForm
from profs.models import Prof from profs.models import Prof
from courses.models import Course
from django.contrib.auth.models import User from django.contrib.auth.models import User
def index(request): def index(request):
...@@ -32,15 +33,24 @@ def search(request): ...@@ -32,15 +33,24 @@ def search(request):
if(request.method == 'POST'): if(request.method == 'POST'):
prof_query = request.POST.get('prof', None) prof_query = request.POST.get('prof', None)
course_query = request.POST.get('course', None) course_query = request.POST.get('course', None)
try: if prof_query:
# Query profs message = "You searched profs for: " + prof_query
prof_set = Prof.objects.annotate(search_name=Concat('first_name', Value(' '), 'last_name')) try:
profs = prof_set.filter(search_name__icontains=prof_query) # Query profs
prof_set = Prof.objects.annotate(search_name=Concat('first_name', Value(' '), 'last_name'))
profs = prof_set.filter(search_name__icontains=prof_query)
# profs = Prof.objects.filter(first_name__icontains=prof_query last_name__contains=prof_query) # profs = Prof.objects.filter(first_name__icontains=prof_query last_name__contains=prof_query)
return render(request, 'profs/index.html', {'profs': profs}) return render(request, 'profs/index.html', {'profs': profs, 'message': message})
except Prof.DoesNotExist: except Prof.DoesNotExist:
return HttpResponse('No such prof') return HttpResponse('No such prof')
else:
message = "You searched courses for: " + course_query
try:
courses = Course.objects.filter(name__icontains=course_query)
return render(request, 'courses/index.html', {'courses': courses, 'message': message})
except Course.DoesNotExist:
return HttpResponse('No such course')
else: else:
courses = course_views.all_course() courses = course_views.all_course()
return render(request, 'templates/browse.html', {'courses': courses}) return render(request, 'templates/browse.html', {'courses': courses})
......
{% extends 'templates/base.html' %} {% extends 'templates/base.html' %}
{% block content %} {% block content %}
<center><h1>Courses</h1></center> <center><h1>Courses</h1></center>
<!-- Create the table rows --> {% if message %}
<center><p>{{message}}</p></center>
{% endif %}
{% for course in courses %} {% for course in courses %}
<ul style="text-align: center; list-style: none; padding: 0"> <ul style="text-align: center; list-style: none; padding: 0">
<li><a href="">{{ course.name }}</a></li> <li><a href="">{{ course.name }}</a></li>
......
No preview for this file type
{% extends 'templates/base.html' %} {% extends 'templates/base.html' %}
{% block content %} {% block content %}
<center><h1>Professors</h1></center> <center><h1>Professors</h1></center>
{% if message %}
<center><p>{{message}}</p></center>
{% endif %}
<!-- Create the table rows --> <!-- Create the table rows -->
{% for prof in profs %} {% for prof in profs %}
<ul style="text-align: center; list-style: none; padding: 0"> <ul style="text-align: center; list-style: none; padding: 0">
......
<!-- Make the form for uploading a new review --> <!-- Make the form for uploading a new review -->
{% extends 'templates/base.html' %} {% extends 'templates/base.html' %}
{% block content %} {% block content %}
<h1>New review {% if curr_prof %} for {{ prof.first_name }} {{ prof.last_name }}{% endif %}</h1> <h1>Create a review {% if curr_prof %} for {{ prof.first_name }} {{ prof.last_name }}{% endif %}</h1>
<form method="POST"> <form method="POST">
{% csrf_token %} {% csrf_token %}
<p>{{ review_form.message }}</p> <p>{{ review_form.message }}</p>
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<p>{{ review_form.course }}</p> <p>{{ review_form.course }}</p>
{% endif %} {% endif %}
<button type="submit">Save</button> <button id="save" type="submit" style="display: none">Save</button>
{% if messages %} {% if messages %}
{% for message in messages %} {% for message in messages %}
{{ message }} {{ message }}
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
<!-- AJAX --> <!-- AJAX -->
<script type="text/javascript"> <script type="text/javascript">
var profDropDown = document.getElementById('prof'); var profDropDown = document.getElementById('prof');
// Runs when a prof is selected from drop down
function dropDownSelect() { function dropDownSelect() {
if(profDropDown.selectedIndex > 0) { if(profDropDown.selectedIndex > 0) {
var profName = profDropDown.options[profDropDown.selectedIndex].text; var profName = profDropDown.options[profDropDown.selectedIndex].text;
...@@ -34,8 +35,13 @@ ...@@ -34,8 +35,13 @@
} }
} }
function showSave() {
document.querySelector("#save").style.display = "inline-block";
}
function callXHR(profName) { function callXHR(profName) {
// Get the courses of the prof specified // Get the courses of the prof specified
document.querySelector("#save").style.display = "none";
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) { if (xhr.readyState === 4 && xhr.status === 200) {
...@@ -50,22 +56,41 @@ ...@@ -50,22 +56,41 @@
select.append(document.createElement('select')); select.append(document.createElement('select'));
var dropDown = document.querySelector('#show ~ select'); var dropDown = document.querySelector('#show ~ select');
dropDown.name = 'course'; dropDown.name = 'course';
dropDown.id="dropdown";
var json = JSON.parse(xhr.response); var json = JSON.parse(xhr.response);
// Add the courses returned by json to to the dropdown // Add the courses returned by json to to the dropdown
for (var i = 1; course = json[i]; i++) { Object.keys(json).map(function(objectKey, index) {
if (i == 1) { if (json[objectKey]) {
var placeholder = document.createElement('option'); if (index == 0) {
placeholder.value = ""; var placeholder = document.createElement('option');
placeholder.textContent = "Select a course"; placeholder.value = "";
dropDown.appendChild(placeholder); placeholder.textContent = "Select a course";
dropDown.appendChild(placeholder);
}
var option = document.createElement('option');
option.value = index;
option.textContent = json[objectKey];
dropDown.appendChild(option);
} }
var option = document.createElement('option'); });
option.value = i;
option.textContent = course;
dropDown.appendChild(option);
}
document.querySelector("#show").style.display = "none"; document.querySelector("#show").style.display = "none";
document.getElementById('dropdown').addEventListener("change", showSave);
console.log("XHR Request done"); console.log("XHR Request done");
// for (var i = 1; course = json[i]; i++) {
// if (i == 1) {
// var placeholder = document.createElement('option');
// placeholder.value = "";
// placeholder.textContent = "Select a course";
// dropDown.appendChild(placeholder);
// }
// var option = document.createElement('option');
// option.value = i;
// option.textContent = course;
// dropDown.appendChild(option);
// }
// document.querySelector("#show").style.display = "none";
// document.getElementById('dropdown').addEventListener("change", showSave);
// console.log("XHR Request done");
} }
}; };
......
...@@ -14,4 +14,58 @@ th { ...@@ -14,4 +14,58 @@ th {
tr:nth-child(even) { tr:nth-child(even) {
background-color: #dddddd; background-color: #dddddd;
} }
\ No newline at end of file
/* Base html */
#welcome, h2, h1{
color: #4788C5 !important;
font-weight: bold !important;
}
/* Navbar */
.navbar-brand {
font-weight: bold;
}
.navbar-brand, .nav-item a {
color: #4788C5 !important;
transition: 1s;
}
.nav-item a:hover {
text-decoration: underline;
}
#username {
color: black !important;
}
/* templates>index.html */
.content {
margin: 4em;
}
#search div{
display: inline-block;
margin: 2em 1em;
}
#search p {
display: inline;
margin: 2em;
color: grey;
}
#search .btn {
width: 70%;
margin: 1em;
padding: 0.9em;
font-size: 0.8em;
text-align: center;
font-weight: bold;
}
/* Browse */
.course {
margin: 2em;
}
...@@ -14,12 +14,6 @@ ...@@ -14,12 +14,6 @@
<a class="navbar-brand" href="{% url 'homepage' %}">Academe</a> <a class="navbar-brand" href="{% url 'homepage' %}">Academe</a>
<div class="collapse navbar-collapse" id="navbarNav"> <div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto"> <ul class="navbar-nav mr-auto">
<!-- <li class="nav-item">
<a class="nav-link" href="{% url 'profs_index' %}">Professors</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'courses_index' %}">Courses</a>
</li> -->
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'browse' %}">Browse</a> <a class="nav-link" href="{% url 'browse' %}">Browse</a>
</li> </li>
...@@ -27,11 +21,13 @@ ...@@ -27,11 +21,13 @@
<a class="nav-link" href="{% url 'review' %}">Create Review</a> <a class="nav-link" href="{% url 'review' %}">Create Review</a>
</li> </li>
</ul> </ul>
<form class="form-inline"> <form class="form-inline"><ul class="navbar-nav mr-auto">
<!--<input type="text" class="form-control" placeholder="Search">
<button class="btn btn-outline-success">Search</button> -->
<ul class="navbar-nav mr-auto">
{% if user.is_authenticated %} {% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" id="username">Hi, {{user.username}}!</a>
</li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'logout' %}">Logout</a> <a class="nav-link" href="{% url 'logout' %}">Logout</a>
</li> </li>
...@@ -48,7 +44,9 @@ ...@@ -48,7 +44,9 @@
</div> </div>
</div> </div>
</nav> </nav>
{% block content %} <div class="content">
{% endblock %} {% block content %}
{% endblock %}
</div>
</body> </body>
</html> </html>
\ No newline at end of file
{% extends 'templates/base.html' %} {% extends 'templates/base.html' %}
{% block content %} {% block content %}
<h2><center> Welcome to Academe! </center></h2> <h2 id="welcome"><strong><center> Welcome to Academe! </center><strong></h2>
<br> <br>
<h3><center> Search for Reviews </center></h3> <h3><center> Looking for a prof or course? We got you.<center></h3>
<form method="post" action="/search/" style="text-align: center"> <form id="search" method="post" action="/search/" style="text-align: center">
{% csrf_token %} {% csrf_token %}
<div> <div>
<br>
<input type="text" placeholder="Search a course" name="course"> <input type="text" placeholder="Search a course" name="course">
<input type="text" placeholder="Search a prof" name="prof">
<br> <br>
</div>
<div>
<button type = "submit" class = "btn btn-primary">Search Course</button> <button type = "submit" class = "btn btn-primary">Search Course</button>
<button type = "submit" class = "btn btn-primary">Search Prof</button>
</div> </div>
<!--<div> <p>or</p>
<div>
<input type="text" placeholder="Search a prof" name="prof"> <input type="text" placeholder="Search a prof" name="prof">
<br> <br>
<input class="btn-primary" id="submitsearch" type="Search Prof" name=""> <button type = "submit" class = "btn btn-primary">Search Prof</button>
</div> --> </div>
<br> <br>
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment