root/branch/bass_boy/umitWeb/views/main.py @ 852

Revision 852, 2.6 kB (checked in by bass_boy, 6 years ago)

Fix bug about image display under windows.

Line 
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
20from os.path import join, abspath, dirname, exists, pardir
21import sys
22from umitWeb.Http import HttpResponse, Http404, HttpResponseRedirect
23from umitWeb.WebLogger import getLogger
24from umitWeb.Auth import authenticate, ERROR
25from umitWeb.SecurityParser import Security
26import mimetypes
27
28logger = getLogger("main")
29
30@authenticate()
31def index(req):
32    response = HttpResponse()
33    response.loadTemplate("index.html")
34    return response
35
36def serve_media(req, path):
37    response = HttpResponse()
38   
39    filename = join(dirname(__file__), pardir, 'media', *(path.split("/")))
40   
41    if not exists(filename):
42        raise Http404
43   
44    response['Content-type'] = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
45    response['Content-type'] += '; charset=utf-8'
46    cntFile = None
47    if sys.platform == 'win32':
48        if response['Content-type'].startswith("text"):
49            cntFile = open(filename, 'r')
50        else:
51            cntFile = open(filename, 'rb')
52    response.write(cntFile.read())
53    return response
54
55def login(req):
56    resp = HttpResponse()
57    if req.POST:
58        resp['Content-type'] = "text/plain"
59        user = Security.get_user(req.POST['login'], req.POST['password'])
60       
61        if req.GET.has_key("json"):
62            if user:
63                req.session['umit_user'] = user
64                resp.write('OK')
65            else:
66                resp.write('FAIL')
67            return resp
68        else:
69            if user:
70                req.session['umit_user'] = user
71                return HttpResponseRedirect("/")
72    else:
73        resp.loadTemplate("login.html")
74        return resp
75   
76def logout(req):
77    if req.session.has_key("umit_user"):
78        del req.session['umit_user']
79    return HttpResponseRedirect("/")
Note: See TracBrowser for help on using the browser.