| 1 | #-*- coding: utf-8 -*- |
|---|
| 2 | # Copyright (C) 2007 Adriano Monteiro Marques <py.adriano@gmail.com> |
|---|
| 3 | # |
|---|
| 4 | # Author: Rodolfo da Silva Carvalho <rodolfo.ueg@gmail.com> |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program; if not, write to the Free Software |
|---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | |
|---|
| 20 | from os.path import join, abspath, dirname, exists, pardir |
|---|
| 21 | from umitWeb.Http import HttpResponse, Http404, HttpResponseRedirect |
|---|
| 22 | from umitWeb.WebLogger import getLogger |
|---|
| 23 | from umitWeb.Auth import authenticate, ERROR |
|---|
| 24 | from umitWeb.SecurityParser import Security |
|---|
| 25 | import mimetypes |
|---|
| 26 | |
|---|
| 27 | logger = getLogger("main") |
|---|
| 28 | |
|---|
| 29 | @authenticate() |
|---|
| 30 | def index(req): |
|---|
| 31 | response = HttpResponse() |
|---|
| 32 | response.loadTemplate("index.html") |
|---|
| 33 | return response |
|---|
| 34 | |
|---|
| 35 | def serve_media(req, path): |
|---|
| 36 | response = HttpResponse() |
|---|
| 37 | |
|---|
| 38 | filename = join(dirname(__file__), pardir, 'media', *(path.split("/"))) |
|---|
| 39 | |
|---|
| 40 | if not exists(filename): |
|---|
| 41 | raise Http404 |
|---|
| 42 | |
|---|
| 43 | response['Content-type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream' |
|---|
| 44 | response.write(open(filename, 'r').read()) |
|---|
| 45 | return response |
|---|
| 46 | |
|---|
| 47 | def login(req): |
|---|
| 48 | resp = HttpResponse() |
|---|
| 49 | if req.POST: |
|---|
| 50 | resp['Content-type'] = "text/plain" |
|---|
| 51 | user = Security.get_user(req.POST['login'], req.POST['password']) |
|---|
| 52 | |
|---|
| 53 | if req.GET.has_key("json"): |
|---|
| 54 | if user: |
|---|
| 55 | req.session['umit_user'] = user |
|---|
| 56 | resp.write('OK') |
|---|
| 57 | else: |
|---|
| 58 | resp.write('FAIL') |
|---|
| 59 | return resp |
|---|
| 60 | else: |
|---|
| 61 | if user: |
|---|
| 62 | req.session['umit_user'] = user |
|---|
| 63 | return HttpResponseRedirect("/") |
|---|
| 64 | else: |
|---|
| 65 | resp.loadTemplate("login.html") |
|---|
| 66 | return resp |
|---|
| 67 | |
|---|
| 68 | def logout(req): |
|---|
| 69 | if req.session.has_key("umit_user"): |
|---|
| 70 | del req.session['umit_user'] |
|---|
| 71 | return HttpResponseRedirect("/") |
|---|