diff options
-rw-r--r-- | requirements.txt | 1 | ||||
-rw-r--r-- | workshop_app/admin.py | 2 | ||||
-rw-r--r-- | workshop_app/models.py | 26 | ||||
-rw-r--r-- | workshop_app/views.py | 120 | ||||
-rw-r--r-- | workshop_portal/settings.py | 6 | ||||
-rw-r--r-- | workshop_portal/urls.py | 8 |
6 files changed, 158 insertions, 5 deletions
diff --git a/requirements.txt b/requirements.txt index 0c6283c..286829d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ appdirs==1.4.0 Django==1.10.4 +-e git+https://github.com/rodrigoamaral/django-fullcalendar.git@d116fcfc2093e57035932a9ea7c111d449fd7e4d#egg=django_fullcalendar packaging==16.8 pyparsing==2.1.10 six==1.10.0 diff --git a/workshop_app/admin.py b/workshop_app/admin.py index 8c38f3f..53d8ad5 100644 --- a/workshop_app/admin.py +++ b/workshop_app/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import Profile # Register your models here. +admin.site.register(Profile)
\ No newline at end of file diff --git a/workshop_app/models.py b/workshop_app/models.py index bd4b2ab..07db83a 100644 --- a/workshop_app/models.py +++ b/workshop_app/models.py @@ -1,5 +1,25 @@ -from __future__ import unicode_literals - +#from __future__ import unicode_literals from django.db import models +from django.contrib.auth.models import User + +position_choices = ( + ("student", "Student"), + ("instructor", "Instructor") + ) + +class Profile(models.Model): + """Profile for users""" + + user = models.OneToOneField(User) + institute = models.CharField(max_length=150) + department = models.CharField(max_length=150) + position = models.CharField(max_length=32, choices=position_choices) + + def __str__(self): + return u"{0} {1} | {2}".format(self.user.first_name, self.user.last_name, self.user.email) + + +def has_profile(user): + """ check if user has profile """ + return True if hasattr(user, 'profile') else False -# Create your models here. diff --git a/workshop_app/views.py b/workshop_app/views.py index 91ea44a..334a1dc 100644 --- a/workshop_app/views.py +++ b/workshop_app/views.py @@ -1,3 +1,119 @@ -from django.shortcuts import render +from django.shortcuts import render, redirect +from forms import UserRegistrationForm, UserLoginForm, ProfileForm +from .models import Profile, User, has_profile +from django.template import RequestContext +from django.contrib.auth import login, logout, authenticate +from django.contrib.auth.decorators import login_required +from django.contrib import messages -# Create your views here. + +def home(request): + '''Home''' + return render(request, "home.html") + +def is_instructor(user): + '''Check if the user is having instructor rights''' + if user.groups.filter(name='instructor').exists(): + return True + +def user_login(request): + '''Login''' + user = request.user + if user.is_authenticated(): + if user.groups.filter(name='instructor').count() > 0: + return redirect('/manage/') + return redirect('/view_profile/') + + if request.method == "POST": + form = UserLoginForm(request.POST) + if form.is_valid(): + user = form.cleaned_data + login(request, user) + if user.groups.filter(name='instructor').count() > 0: + return redirect('/manage/') + return redirect('/view_profile/') + else: + return render(request, 'workshop_app/login.html', {"form": form}) + else: + form = UserLoginForm() + return render(request, 'workshop_app/login.html', {"form": form}) + +def user_logout(request): + '''Logout''' + logout(request) + return render(request, 'workshop_app/logout.html') + +def user_register(request): + '''User Registeration form''' + if request.method == 'POST': + form = UserRegistrationForm(request.POST) + if form.is_valid(): + data = form.cleaned_data + username, password = form.save() + new_user = authenticate(username=username, password=password) + login(request, new_user) + return redirect('/home') + else: + return render(request, "workshop_app/register.html", {"form": form}) + else: + form = UserRegistrationForm() + + return render(request, "workshop_app/register.html", {"form": form}) + + +def book(request): + user = request.user + if user.is_authenticated(): + if user.groups.filter(name='instructor').count() > 0: + return redirect('/manage/') + return render(request, "workshop_app/booking.html") + else: + return redirect('/login/') + +def manage(request): + user = request.user + if user.is_authenticated(): + if user.groups.filter(name='instructor').count() > 0: + return render(request, "workshop_app/manage.html") + return redirect('/book/') + else: + return redirect('/login/') + +@login_required +def view_profile(request): + """ view moderators and users profile """ + return render(request, "workshop_app/view_profile.html") + +@login_required +def edit_profile(request): + """ edit profile details facility for instructor and students """ + + user = request.user + if is_instructor(user): + template = 'workshop_app/manage.html' + else: + template = 'workshop_app/user.html' + context = {'template': template} + if has_profile(user): + profile = Profile.objects.get(user_id=user.id) + else: + profile = None + + if request.method == 'POST': + form = ProfileForm(request.POST, user=user, instance=profile) + if form.is_valid(): + form_data = form.save(commit=False) + form_data.user = user + form_data.user.first_name = request.POST['first_name'] + form_data.user.last_name = request.POST['last_name'] + form_data.user.save() + form_data.save() + + return render(request, 'workshop_app/profile_updated.html', context) + else: + context['form'] = form + return my_render_to_response('workshop_app/edit_profile.html', context) + else: + form = ProfileForm(user=user, instance=profile) + context['form'] = form + return render(request, 'workshop_app/edit_profile.html', context) diff --git a/workshop_portal/settings.py b/workshop_portal/settings.py index 923ee4f..5681433 100644 --- a/workshop_portal/settings.py +++ b/workshop_portal/settings.py @@ -37,6 +37,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'workshop_app', + 'fullcalendar', ] MIDDLEWARE = [ @@ -118,3 +120,7 @@ USE_TZ = True # https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = '/static/' + +LOGIN_URL = "/login/" + +LOGIN_REDIRECT_URL = "/profile"
\ No newline at end of file diff --git a/workshop_portal/urls.py b/workshop_portal/urls.py index f1a755d..a25a634 100644 --- a/workshop_portal/urls.py +++ b/workshop_portal/urls.py @@ -15,7 +15,15 @@ Including another URLconf """ from django.conf.urls import url from django.contrib import admin +from workshop_app import views urlpatterns = [ url(r'^admin/', admin.site.urls), + url(r'^login/$', views.user_login), + url(r'^logout/$', views.user_logout), + url(r'^register/$', views.user_register), + url(r'^book/$', views.book), + url(r'^manage/$', views.manage), + url(r'^view_profile/$', views.view_profile), + url(r'^edit_profile/$', views.edit_profile) ] |