Commit f39e6e52 authored by David Haynes's avatar David Haynes 🙆

Create Dynamic Welcome Page for Users

- Displays for logged in users only
- Lists users that meet certain criteria with links to their profiles
- Data is cached
- Looks pretty
parent df310499
......@@ -15,7 +15,7 @@ from randomslugfield import RandomSlugField
from multiselectfield import MultiSelectField
from allauth.socialaccount.models import SocialAccount
# imports from your apps
from housing.models import Room
from housing.models import Room, Floor
class Major(TimeStampedModel):
......@@ -132,6 +132,7 @@ class Student(TimeStampedModel):
room = models.ForeignKey(Room, null=True, blank=True)
major = models.ForeignKey('Major', null=True, blank=True)
floor = models.ForeignKey(Floor, null=True, blank=True)
times_changed_room = models.PositiveIntegerField(default=0)
......@@ -220,7 +221,7 @@ class Student(TimeStampedModel):
def __unicode__(self):
return unicode(self.user.username)
#def save(self, *args, **kwargs):
# def save(self, *args, **kwargs):
#print('we be savin\'!')
#from django.db.models.signals import pre_save, post_save
#for signal in [pre_save, post_save]:
......
......@@ -6,7 +6,8 @@ from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
from django.contrib import admin
from django.views.decorators.cache import cache_page
# imports from your apps
from .views import HomePageView
admin.autodiscover()
admin.site.login = login_required(admin.site.login)
......@@ -18,8 +19,7 @@ urlpatterns = patterns('',
# project-level urls
url(r'^$',
cache_page(4)(TemplateView.as_view(template_name="index.html")),
name='index'),
cache_page(4)(HomePageView.as_view()), name='homepage'),
url(r'^about/$',
cache_page(60 * 15)(TemplateView.as_view(template_name='about.html')),
name='about'),
......
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.shortcuts import render
from django.views.generic import (View, DetailView, TemplateView)
# third party imports
from braces.views import LoginRequiredMixin
from cas.views import login as cas_login
from accounts.models import Student
class HomePageView(View):
def get(self, request, *args, **kwargs):
if self.request.user.is_authenticated():
view = LandingPage.as_view()
return view(request, *args, **kwargs)
else:
view = LandingPageNoAuth.as_view()
return view(request, *args, **kwargs)
class LandingPage(LoginRequiredMixin, TemplateView):
template_name = 'landing.html'
def get_context_data(self, **kwargs):
context = super(LandingPage, self).get_context_data(**kwargs)
me = Student.objects.get(user=self.request.user)
# Create Dictionaries to store Students that meet criteria
context["roomies"] = Student.objects.filter(room=me.room).exclude(user__username=me)
context["floories"] = Student.objects.filter(floor=me.get_floor()).exclude(user__username=me).exclude(room=me.room).order_by('room')
context["majormates"] = Student.objects.filter(major=me.major).exclude(user__username=me).order_by('?')[:8]
# Hack to Correctly Display Building plus Floor
floor = str(me.get_floor())
if floor[len(floor)-1:len(floor)] == "1":
context["home"] = "%s %s" %(me.get_building(), 'First')
elif floor[len(floor)-1:len(floor)] == "2":
context["home"] = "%s %s" %(me.get_building(), 'Second')
else:
context["home"] = "%s %s" %(me.get_building(), 'Third')
return context
class LandingPageNoAuth(DetailView):
template_name = 'index.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
{% extends 'layouts/base.html' %} {% block title %} SRCT Roomlist • Homepage {% endblock %} {% block content %}
{% load cache %}
{% load gravatar %}
<h2>Welcome Back {{ request.user.first_name }}!</h2>
<legend></legend>
{% cache 300 roommates request.user.username %}
{% if roomies|length == 1 %}
<h3>Your Roommate in {{ request.user.student.room }}</h3>
<legend></legend>
{% else %}
<h3>Your Roommates in {{ request.user.student.room }}</h3>
<legend></legend>
{% endif %}
<div class="text-center">
<div class="row">
{% for student in roomies %}
<div class="col-md-3">
<a href="{{ student.get_absolute_url }}"><img class="img-circle img-responsive center center-block" src="{% gravatar_url student.user.email 100 %}" alt="{{ student.user.first_name }} Gravatar picture"></img></a>
<h4>{{ student.user.first_name }} {{ student.user.last_name }}</h4>
</div>
{% empty %}
<h4>There are no other students registered in your room right now.</h4>
{% endfor %}
</div>
</div>
{% endcache %}
{% cache 300 floormates request.user.username %}
<h3>Your Floormates from {{ home }}</h3>
<legend></legend>
<div class="text-center">
<div class="row">
{% for student in floories %}
<div class="col-md-3">
<a href="{{ student.get_absolute_url }}"><img class="img-circle img-responsive center center-block" src="{% gravatar_url student.user.email 100 %}" alt="{{ student.user.first_name }} Gravatar picture"></a>
<h4>{{ student.user.first_name }} {{ student.user.last_name }}</h4>
<a href="{{ student.room.get_absolute_url }}"><h4>{{ student.room }}</h4></a>
</div>
{% empty %}
<h4>There are no other students registered on your floor right now.</h4>
{% endfor %}
</div>
</div>
{% endcache %}
{% cache 600 majormates request.user.username %}
<h3>Other Students in <a href="{{ request.user.student.major.get_absolute_url }}">{{ request.user.student.major }}</a></h3>
<legend></legend>
<div class="text-center">
<div class="row">
{% for student in majormates %}
<div class="col-md-3">
<a href="{{ student.get_absolute_url }}"><img class="img-circle img-responsive center center-block" src="{% gravatar_url student.user.email 100 %}" alt="{{ student.user.first_name }} Gravatar picture"></a>
<h4>{{ student.user.first_name }} {{ student.user.last_name }}</h4>
</div>
{% empty %}
<h4>There are no other students registered in your major right now.</h4>
{% endfor %}
</div>
</div>
{% endcache %}
{% endblock %}
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