diff --git a/PackTravel/settings.py b/PackTravel/settings.py index dbfcb6d2..e4d32325 100644 --- a/PackTravel/settings.py +++ b/PackTravel/settings.py @@ -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 = '/' diff --git a/PackTravel/urls.py b/PackTravel/urls.py index d69a513b..74a9a6f0 100644 --- a/PackTravel/urls.py +++ b/PackTravel/urls.py @@ -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 @@ -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') ] diff --git a/db.sqlite3 b/db.sqlite3 index a1580866..af8bf2ab 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/publish/forms.py b/publish/forms.py index 78b72918..28b62114 100644 --- a/publish/forms.py +++ b/publish/forms.py @@ -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'})) diff --git a/publish/static/plus.svg b/publish/static/plus.svg new file mode 100644 index 00000000..349e191c --- /dev/null +++ b/publish/static/plus.svg @@ -0,0 +1,6 @@ + + diff --git a/publish/views.py b/publish/views.py index e5680248..cf8064fe 100644 --- a/publish/views.py +++ b/publish/views.py @@ -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']}) + + + + diff --git a/search/views.py b/search/views.py index 2e4aa3a3..911fe886 100644 --- a/search/views.py +++ b/search/views.py @@ -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']}) \ No newline at end of file diff --git a/templates/home/home.html b/templates/home/home.html index 39353db0..da95b13b 100644 --- a/templates/home/home.html +++ b/templates/home/home.html @@ -7,6 +7,11 @@ + +
+ + {% include 'nav.html' %} + + + +Destination | +{{ride.destination}} | +
Date | +{{ride.date}} | +
Time | +{{ride.hour}}:{{ride.minute}} {{ride.ampm}} | +
Select | +Type | +Start Point | + +Start Time | +Duration | +Details | +
---|---|---|---|---|---|
+
+
+
+
+
+ |
+ {{ route.type }} | +{{ route.spoint }} | +{{ route.hour }}:{{ route.minute }} {{ route.ampm }} | +{{ route.duration }} minutes | +{{ route.details }} | +