# CCS CS Lab Main Views # # Douglas Thrift # # $Id$ from ccscslab.delicious.models import Bookmark from ccscslab.facebook.models import User from ccscslab.facebook.utils import get_facebook from ccscslab.laptops.models import Laptop from ccscslab.main.forms import LoginForm, RequestForm, PasswordForm from ccscslab.main.jsons import request_object_hook from ccscslab.main.models import Computer, Login, Person, Request, Role from ccscslab.main.utils import request_mail from ccscslab.wall.models import Post from ccscslab.webcam.models import Webcam import common from django.contrib.auth.decorators import login_required, permission_required from django.contrib.sitemaps.views import sitemap from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render_to_response from django.template import RequestContext from dt_django_http_login import http_permission_required from gzip import GzipFile import socket try: import simplejson as json except ImportError: import json def the_lab(request): from django.conf import settings if request.user.is_authenticated(): webcam = Webcam.objects.get(number = 0) logins = Login.objects.filter(local = True) laptops = Laptop.objects.filter(online = True) users = laptops.filter(person__name = settings.CCSCSLAB_DEFAULT_PERSON).exclude(user = None).distinct().values('user') if users: info = get_facebook().users.getInfo(map(lambda user: user['user'], users), ['name']) if info: names = {} for _info in info: names[_info.get('uid')] = _info.get('name') for laptop in laptops: if laptop.person.is_default() and laptop.user is not None: laptop.user.name = names.get(laptop.user.id) roles = Role.objects.all() posts = Post.objects.show()[:10] rows = len(posts) + 1 bookmarks = Bookmark.objects.all()[:5] else: webcam = None logins = None laptops = None roles = None posts = None rows = None bookmarks = None return render_to_response('main/the-lab', {'bookmarks': bookmarks, 'laptops': laptops, 'login_form': LoginForm(), 'logins': logins, 'posts': posts, 'roles': roles, 'rows': rows, 'secure': settings.CCSCSLAB_CALLBACK_SECURE, 'webcam': webcam}, context_instance = RequestContext(request)) def people(request): people = Person.objects.all() if request.user.is_authenticated(): for person in people: person.laptops = person.laptop_set.filter(online = True) info = get_facebook().users.getInfo(map(lambda user: user.id, User.objects.order_by('person', 'id')), ['name', 'pic_square']) if info: index = 0 for person in people: is_default = person.is_default() person.facebook_users = person.facebook_user_set.all() if is_default: names = {} for user in person.facebook_users: try: _info = info[index] if _info.get('uid') == user.id: user.name = _info.get('name') user.pic_square = _info.get('pic_square') if is_default: names[user.id] = user.name else: continue index += 1 except IndexError: break if is_default: for laptop in person.laptops: if laptop.user is not None: laptop.user.name = names.get(laptop.user.id) return render_to_response('main/people', {'people': people}, context_instance = RequestContext(request)) def people_redirect(request): redirect = '/people' person = request.REQUEST.get('person', '') if person != '': redirect += '/' + person return HttpResponseRedirect(redirect) def person(request, name): person = get_object_or_404(Person, name = name) if request.user.is_authenticated(): laptops = person.laptop_set.filter(online = True) users = person.facebook_user_set.all() if users: info = get_facebook().users.getInfo(map(lambda user: user.id, users), ['name', 'pic_square']) if info: is_default = person.is_default() if is_default: names = {} index = 0 for user in users: try: _info = info[index] if _info.get('uid') == user.id: user.name = _info.get('name') user.pic_square = _info.get('pic_square') if is_default: names[user.id] = user.name else: continue index += 1 except IndexError: break if is_default: for laptop in laptops: if laptop.user is not None: laptop.user.name = names.get(laptop.user.id) else: laptops = None users = None return render_to_response('main/person', {'bookmarks': Bookmark.objects.filter(user__in = person.delicious_user_set.all)[:5], 'laptops': laptops, 'people': Person.objects.all(), 'person': person, 'users': users}, context_instance = RequestContext(request)) def computers(request): return render_to_response('main/computers', {'computers': Computer.objects.all(), 'roles': Role.objects.all()}, context_instance = RequestContext(request)) def computers_redirect(request): redirect = '/computers' computer = request.REQUEST.get('computer', '') if computer != '': redirect += '/' + computer return HttpResponseRedirect(redirect) def computer(request, name): return render_to_response('main/computer', {'computer': get_object_or_404(Computer, name = name), 'computers': Computer.objects.all()}, context_instance = RequestContext(request)) def roles_redirect(request): role = request.REQUEST.get('role', '') if role != '': redirect = '/roles/' + role else: redirect = '/computers' return HttpResponseRedirect(redirect) def role(request, name): return render_to_response('main/role', {'role': get_object_or_404(Role, name = name), 'roles': Role.objects.all()}, context_instance = RequestContext(request)) def account(request): from django.conf import settings if request.method == 'POST': request_form = RequestForm(request.POST) if request_form.is_valid(): data = request_form.cleaned_data _request = Request.objects.get_or_create(name = data['name'], defaults = {'ip_address': '0.0.0.0'})[0] _request.full_name = data['full_name'] _request.temporary_password = data['password'] _request.ip_address = request.META['REMOTE_ADDR'] _request.dns_name = socket.getfqdn(_request.ip_address) _request.email = data['email'] _request.save() request_mail(_request) else: request_form = RequestForm() return render_to_response('main/account', {'request_form': request_form, 'secure': settings.CCSCSLAB_CALLBACK_SECURE}, context_instance = RequestContext(request)) @permission_required('main.change_request') def account_requests(request): from django.conf import settings requests = Request.objects.all() users = map(lambda request: request['facebook_user'], requests.exclude(facebook_user = None).values('facebook_user')) if users: info = get_facebook().users.getInfo(users, ['pic_square']) if info: index = 0 for _request in requests: try: _info = info[index] if _info.get('uid') == _request.facebook_user: _request.pic_square = _info.get('pic_square') else: continue index += 1 except IndexError: break return render_to_response('main/account-requests', {'requests': requests, 'url': settings.CCSCSLAB_CALLBACK}, context_instance = RequestContext(request)) @permission_required('main.change_request') def account_requests_fullfilled(request): data = [] if 'fullfilled' in request.POST and 'id' in request.POST: fullfilled = request.POST['fullfilled'] try: _request = Request.objects.get(id = int(request.POST['id'])) if fullfilled == 'No': _request.fullfilled = True data.append('Yes') elif fullfilled == 'Yes': _request.fullfilled = False data.append('No') _request.save() except (Request.DoesNotExist, ValueError): pass return HttpResponse(json.dumps(data), content_type = 'application/json') @http_permission_required('main.add_request', login_realm = 'Account Requests Update') def account_requests_update(request): if request.method == 'POST': for _request in json.loads(request.POST.get('r', '[]'), object_hook = request_object_hook): _request.save() request_mail(_request) return HttpResponse(content_type = 'text/plain') @login_required def password(request): from django.conf import settings exceptions = None if request.method == 'POST': password_form = PasswordForm(request.user, request.POST) if password_form.is_valid(): user = request.user.person_set.get().name data = password_form.cleaned_data computer = data['computer'] action = data['action'] new_password = data['password'] if action == 'change': old_password = data['old_password'] if computer == 'all': exceptions = common.change_all(user, old_password, new_password) else: try: common.change(computer, user, old_password, new_password) except Exception, exception: exceptions = {computer: exception} elif action == 'reset': if computer == 'all': exceptions = common.reset_all(user, new_password) else: try: common.reset(computer, user, new_password) except Exception, exception: exceptions = {computer: exception} else: assert False request.user.set_password(new_password) request.user.save() else: password_form = PasswordForm(request.user) return render_to_response('main/password', {'exceptions': exceptions, 'password_form': password_form, 'secure': settings.CCSCSLAB_CALLBACK_SECURE}, context_instance = RequestContext(request)) def login(request): from django.conf import settings from django.contrib.auth import login next = request.REQUEST.get('next') if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): login(request, login_form.cleaned_data['user']) if not next or '//' in next or ' ' in next: next = settings.LOGIN_REDIRECT_URL return HttpResponseRedirect(next) else: login_form = LoginForm() return render_to_response('main/login', {'login_form': login_form, 'next': next, 'secure': settings.CCSCSLAB_CALLBACK_SECURE}, context_instance = RequestContext(request)) def robots_txt(request): return render_to_response('main/robots.txt', {'disallows': ('/admin', '/awstats', '/facebook'), 'sitemaps': ('/sitemap.gz',)}, context_instance = RequestContext(request), mimetype = 'text/plain') def sitemap_gz(request, sitemaps, section = None): response = HttpResponse(content_type = 'application/x-gzip') response['Content-Encoding'] = 'x-gzip' gzip = GzipFile(mode = 'wb', fileobj = response) sitemap_response = sitemap(request, sitemaps, section) sitemap_response.render() gzip.write(sitemap_response.content) gzip.close() return response # vim: noexpandtab tabstop=4