Skip to content

Commit

Permalink
Created Rides db, rrides view, routes db, routes view and mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
amisha-w committed Oct 7, 2022
1 parent c30016f commit 88640ed
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 54 deletions.
3 changes: 2 additions & 1 deletion PackTravel/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'user/static')
os.path.join(BASE_DIR,'user/static'),
os.path.join(BASE_DIR,'publish/static')
]

LOGIN_REDIRECT_URL = '/'
Expand Down
5 changes: 4 additions & 1 deletion PackTravel/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
import imp
from user import views as userView

from django.contrib import admin
from django.urls import path
from search import views as searchViews
Expand All @@ -29,5 +30,7 @@
path('register/', userView.register, name='register'),
path('logout/', userView.logout, name='logout'),
path('login/', userView.login, name='login'),
path('route/', publishViews.route, name='route')
path('create_ride/', publishViews.create_ride, name='create_ride'),
path('add_route/', publishViews.add_route, name='add_route'),
path('display_ride/', publishViews.display_ride, name='display_ride')
]
Binary file modified db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion publish/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# from django.contrib.auth.models import User
from .models import Ride

class CreateNewRide(forms.ModelForm):
class RideForm(forms.ModelForm):
destination = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Enter your start destination', 'class': 'form-control'}))
rideDate = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': 'Enter ride date', 'class': 'form-control'}))

Expand Down
6 changes: 6 additions & 0 deletions publish/static/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 101 additions & 25 deletions publish/views.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,117 @@
from http.client import HTTPResponse
from django.shortcuts import render,redirect
from numpy import dtype
import requests
import json
from django.contrib.auth import login, authenticate
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm

from publish.forms import CreateNewRide
from publish.forms import RideForm
from user.views import userDB, index
from utils import get_client
# from django.http import HttpResponse

# Create your views here.
# from django.http import HttpResponse
client = get_client()
db = client.SEProject
ridesDB = db.rides
routesDB = db.routes

def publish_index(request):
return render(request, 'publish/publish.html')

def route(request):
return render(request, 'publish/route.html')

def createNewRide(request):
if request.method == "POST":
form = CreateNewRide(request.POST)
if form.is_valid():
userObj = {
"destination": form.cleaned_data["destination"],
"rideDate": form.cleaned_data["rideDate"]
if not request.session.has_key('username'):
request.session['alert'] = "Please login to create a ride."
return redirect('index')
return render(request, 'publish/publish.html', {"username": request.session['username']})

def display_ride(request):
ride = request.session['ride']
ride_id = ride['_id']
routes = get_routes(ride)
ride = ridesDB.find_one({'_id': ride['_id']})
context = {
"username": request.session['username'],
"ride": ride,
"routes": routes
}
return render(request, 'publish/route.html', context)

def get_routes(ride):
routes = []
if 'routes' not in ride:
return None
route_ids = ride['routes']
for route_id in route_ids:
route = routesDB.find_one({'_id': route_id})
route['id'] = route.pop('_id')
routes.append(route)
return routes

def create_ride(request):
if request.method == 'POST':
ride = {
"_id":
request.POST.get('name')+"_"+request.POST.get('destination')
+"_"+request.POST.get("date")+"_"+
request.POST.get("hour")+"_"+
request.POST.get("minute")+"_"+
request.POST.get("ampm")
,
"name": request.POST.get('name'),
"destination": request.POST.get('destination'),
"date": request.POST.get("date"),
"hour": request.POST.get("hour"),
"minute": request.POST.get("minute"),
"ampm": request.POST.get("ampm")
}
userDB.insert_one(userObj)
request.session['destination'] = userObj["destination"]
request.session['rideDate'] = userObj["rideDate"]
return redirect(index, username=request.session["username"])
else:
print(form.errors.as_data())
else:
if request.session.has_key('username'):
return index(request,request.session['username'])
form = CreateNewRide()
return render(request, 'user/register.html', {"form": form})
request.session['ride'] = ride
if ridesDB.find_one({'_id': ride['_id']})== None:
ridesDB.insert_one(ride)
return redirect(display_ride)

return render(request, 'publish/publish.html', {"username": request.session['username']})

def add_route(request):
if request.method == 'POST':

ride = request.POST.get('ride')
ride = ride.replace("\'", "\"")
ride = json.loads(ride)
ride_id = ride['_id']
ride = ridesDB.find_one({'_id': ride['_id']})
route = {
"_id": str(ride_id)
+"_"+request.POST.get('type')
+"_"+request.POST.get('spoint')
+"_"+request.POST.get("hour")
+"_"+request.POST.get("minute")
+"_"+request.POST.get("duration")
+"_"+request.POST.get("details")
+"_"+request.POST.get("ampm"),

"type": request.POST.get('type'),
"spoint": request.POST.get('spoint'),
"hour": request.POST.get("hour"),
"minute": request.POST.get("minute"),
"duration": request.POST.get("duration"),
"details": request.POST.get("details"),
"ampm": request.POST.get("ampm"),
"users": [request.session['username']]
}
request.session["route"] = route
request.session["ride"] = ride
#check if route is unique
if routesDB.find_one({'_id': route["_id"]})== None:
routesDB.insert_one(route)
if 'routes' not in ride:
ridesDB.update_one({"_id": ride_id}, {"$set": {"routes": [route['_id']]}})
else:
ride['routes'].append(route['_id'])
ridesDB.update_one({"_id": ride_id}, {"$set": {"routes": ride['routes']}})
return redirect(display_ride)

return render(request, 'publish/publish.html', {"username": request.session['username']})




2 changes: 1 addition & 1 deletion search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import requests

def search_index(request):
return render(request, 'search/search.html')
return render(request, 'search/search.html', {"username": request.session['username']})

5 changes: 5 additions & 0 deletions templates/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
</head>
<script>
{% if alertmsg %}
$(document).ready(function() { alert({{alertmsg}});});
{% endif %}
</script>
<style>
.btn-blue{
background-color: #5b3be9 !important;
Expand Down
4 changes: 2 additions & 2 deletions templates/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<li class="nav-item">
<a class="nav-link" href="/publish/">Create a Ride</a>
</li>
{% if not username %}
{% if not request.session.username %}
<li class="nav-item">
<a class="nav-link" href="/login/">Login</a>
</li>
Expand All @@ -88,7 +88,7 @@
<li class="nav-item">
<a class="nav-link" href="/route/">Route</a>
</li>
{% if username %}
{% if request.session.username %}
<li class="nav-item">

<a class="nav-link" href="/logout/">
Expand Down
55 changes: 35 additions & 20 deletions templates/publish/publish.html
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
<!DOCTYPE html>{% load static %}
<html lang="en">

<head>


<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>New PackTravel</title>

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">

</head>

<body style="background-color: #3A3B3C;">

{% include 'nav.html' %}



<div class="card mx-auto shadow-2-strong bg-white rounded" style="width: 60%; margin: 50px; padding: 50px;">
<h3>Create a New Ride</h3>
<h3>New Ride</h3>
<hr>
<form method="POST" class="form-group">
<form action="/create_ride/" method="POST" class="form-group">
{% csrf_token %}
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Destination</span>
<input class="form-control" type="text" placeholder="Enter your start destination">
<span class="form-label">Ride Name</span>
<input name="name" required class="form-control" type="text" placeholder="Enter a name for the ride">
</div>
</div>
<br>
</div>
<br>
<div class="row">
<div class="col-sm-5">
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Ride Date</span>
<input class="form-control" type="date" required placeholder="Enter ride date">
<span class="form-label">Destination</span>
<input name="destination" required class="form-control" type="text" placeholder="Enter your start destination">
</div>
</div>
<br>
</div>
<br>
<div class="form-group">
<span class="form-label">Ride Date</span>
<input class="form-control" name="date" type="date" required placeholder="Enter ride date">
</div>
<br>
Enter the Time to Reach
<div class="row"
<div class="col-sm-7">
<div class="row">
Enter the Time to Reach

<div class="col-sm-4">
<div class="form-group">
<span class="form-label">Hour</span>
<select class="form-control">
<select name="hour" required class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
Expand All @@ -67,7 +76,7 @@ <h3>Create a New Ride</h3>
<div class="col-sm-4">
<div class="form-group">
<span class="form-label">Min</span>
<select class="form-control">
<select name="minute" required class="form-control">
<option>00</option>
<option>05</option>
<option>10</option>
Expand All @@ -87,20 +96,25 @@ <h3>Create a New Ride</h3>
<div class="col-sm-4">
<div class="form-group">
<span class="form-label">AM/PM</span>
<select class="form-control">
<select name="ampm" class="form-control">
<option>AM</option>
<option>PM</option>
</select>
<span class="select-arrow"></span>
</div>
</div>
</div>

</div>
<br><br>
<div class="form-btn">
<button style="float: right;" class="btn btn-info btn-lg submit-btn">Next: Add a route</button>
<br>
</div>


</div>
<br><br>
<div class="form-btn">
<button class="btn btn-dark submit-btn">CREATE</button>
</div>
</form>
<!-- <form method="POST" class="form-group">
{% csrf_token %}
Expand All @@ -117,6 +131,7 @@ <h3>Create a New Ride</h3>
<button type="submit" class="btn btn-dark" >Login</button>
</form> -->
</div>



</body>
Expand Down
Loading

0 comments on commit 88640ed

Please sign in to comment.