[Mpuls-commits] r1493 - in wasko/branches/2.0: mpulsweb/model waskaweb/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Feb 11 08:41:17 CET 2010
Author: torsten
Date: 2010-02-11 08:41:16 +0100 (Thu, 11 Feb 2010)
New Revision: 1493
Added:
wasko/branches/2.0/mpulsweb/model/document.py
Removed:
wasko/branches/2.0/waskaweb/model/document.py
Log:
Moved model.document to base.
Copied: wasko/branches/2.0/mpulsweb/model/document.py (from rev 1489, wasko/branches/2.0/waskaweb/model/document.py)
Property changes on: wasko/branches/2.0/mpulsweb/model/document.py
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: wasko/branches/2.0/waskaweb/model/document.py
===================================================================
--- wasko/branches/2.0/waskaweb/model/document.py 2010-02-11 07:40:40 UTC (rev 1492)
+++ wasko/branches/2.0/waskaweb/model/document.py 2010-02-11 07:41:16 UTC (rev 1493)
@@ -1,259 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# Copyright 2007, 2008 Intevation GmbH, Germany, <info at intevation.de>
-#
-# This file is part of mpuls WASKA (CoMPUter-based case fiLeS -
-# Web-Anwendungs-Server fuer Kompetenzagenturen).
-#
-# mpuls WASKA is free software: you can redistribute it and/or modify it under
-# the terms of the GNU Affero General Public License as published by the
-# Free Software Foundation, either version 3 of the License, or (at your
-# option) any later version.
-#
-# mpuls WASKA is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
-# License for more details.
-#
-# You should have received a copy of the GNU Affero General Public
-# License along with mpuls WASKA. If not, see <http://www.gnu.org/licenses/>.
-#
-# mpuls WASKA has been developed on behalf of the
-# Projekttraeger im Deutschen Zentrum fuer Luft- und Raumfahrt e.V. (PT-DLR)
-# within the programme Kompetenzagenturen (Durchfuehrungsphase) funded by
-# the Bundesministerium fuer Familie, Senioren, Frauen und Jugend and
-# European Social Fund resources.
-#
-# Authors:
-# Sascha L. Teichmann <teichmann at intevation.de>
-#
-
-from mimetypes import guess_type
-import mpulsweb.lib.helpers as h
-import psycopg2 as dbapi
-import cStringIO as StringIO
-
-from mpulsweb.lib.db import db
-
-MAX_SIZE = 10*1024*1024
-BLOCK_SIZE = 4*1024
-
-CREATE_GLOBAL = 'SELECT * FROM create_global_dokument_ds(%(name)s)'
-CREATE_CASE = 'SELECT * FROM create_fall_dokument_ds(%(id)s, %(uuid)s, %(name)s)'
-
-UPDATE_GLOBAL = '''
-UPDATE ka_global_dokumente_tbl_view
-SET daten = %(bytes)s, groesse = %(size)s, name = %(name)s, mime = %(mime)s
-WHERE id = %(id)s
-'''
-
-UPDATE_CASE = '''
-UPDATE ka_fall_dokumente_tbl_view
-SET daten = %(bytes)s, groesse = %(size)s, name = %(name)s, mime = %(mime)s
-WHERE id = %(id)s AND master_id = %(case)s
-'''
-
-LIST_GLOBAL = '''
-SELECT id, master_id, groesse, mime, name
-FROM ka_global_dokumente_tbl_view
-'''
-
-LIST_CASE = '''
-SELECT id, master_id, groesse, mime, name
-FROM ka_fall_dokumente_tbl_view
-WHERE master_id = %(master_id)s
-'''
-
-DELETE_GLOBAL = '''
-SELECT delete_global_dokument_ds(%(id)s)
-'''
-
-DELETE_CASE = '''
-SELECT delete_fall_dokument_ds(%(id)s)
-'''
-
-LOAD_GLOBAL = '''
-SELECT master_id, groesse, mime, daten, name
-FROM ka_global_dokumente_tbl_view
-WHERE id = %(id)s
-'''
-
-LOAD_CASE = '''
-SELECT master_id, groesse, mime, daten, name
-FROM ka_fall_dokumente_tbl_view
-WHERE id = %(id)s
-'''
-
-class DocumentException(Exception):
-
- def __init__(self, value):
- self.value = value
-
- def __str__(self):
- return repr(self.value)
-
-def listDocuments(case=None):
-
- if not case is None: case = int(case)
-
- documents = []
-
- con, cur = None, None
- try:
- con = db.getConnection()
- cur = con.cursor()
- if case is None:
- cur.execute(LIST_GLOBAL)
- else:
- cur.execute(LIST_CASE, { 'master_id': case })
-
- while True:
- row = cur.fetchone()
- if not row: break
- id, case, size, mime, name = row
- documents.append(Document(id, case, size, mime, name))
- finally:
- db.recycleConnection(con, cur)
-
- return documents
-
-def deleteDocument(id, case=False):
- id = int(id)
- con, cur, has_commited = None, None, False
- try:
- con = db.getConnection()
- cur = con.cursor()
- if case: cur.execute(DELETE_CASE, { 'id': id })
- else: cur.execute(DELETE_GLOBAL, { 'id': id })
- con.commit()
- has_commited = True
- finally:
- if not has_commited and con:
- try: con.rollback()
- except: pass
-
- db.recycleConnection(con, cur)
-
-class Document:
- def __init__(self, id=None, case=None, size=None, mime=None, name=None):
- self.id = id
- self.case = case
- self.size = size
- self.mime = mime
- self.name = name
- self.data = None
-
- def isCaseAttached(self):
- return not self.case is None
-
- def getName(self, encoding="utf-8"):
- name = self.name
- if name is None: return None
- if encoding:
- return unicode(str(name), encoding)
- return name
-
- def create(self, name, src, case=None, uuid=None):
-
- name = name.strip().rsplit('\\', 1)[-1].rsplit('/', 1)[-1]
-
- if not name:
- raise DocumentException("Kein Dokumentname vorhanden.")
-
- self.name = name
-
- mime = guess_type(name)
- if mime: mime = mime[0]
- else: mime = "application/octet-stream"
-
- self.mime = mime
-
- if not case is None:
- case = int(case)
- self.case = case
-
- out = StringIO.StringIO()
- total_size = 0
- while True:
- b = src.read(BLOCK_SIZE)
- l = len(b)
- if l == 0: break
- total_size += l
- if total_size > MAX_SIZE:
- raise DocumentException(
- u"Datei ist zu groß. Maximal %sMB sind erlaubt." \
- % h.format_number(MAX_SIZE/float(1024*1024)))
- out.write(b)
- bytes = out.getvalue()
- out.close(); out = None
-
- self.size = total_size
-
- has_commited, con, cur = False, None, None
-
- try:
- con = db.getConnection()
- cur = con.cursor()
-
- if case is None:
- cur.execute(CREATE_GLOBAL, {'name': name})
- else:
- cur.execute(CREATE_CASE, { 'id': case, 'uuid': uuid, 'name': name})
-
- row = cur.fetchone()
- if row is None:
- raise DocumentException("Konnte neues Dokument nicht anlegen.")
-
- id = row[0]
- fields = {
- 'bytes': dbapi.Binary(bytes),
- 'size': total_size,
- 'name': name,
- 'mime': mime,
- 'id': id }
-
- if case is None:
- stmnt = UPDATE_GLOBAL
- else:
- stmnt = UPDATE_CASE
- fields['case'] = case
-
- cur.execute(stmnt, fields)
-
- con.commit()
- has_commited = True
-
- self.id = id
- finally:
- if cur:
- try: cur.close()
- except: pass
-
- if not has_commited and con:
- try: con.rollback()
- except: pass
-
- db.recycleConnection(con)
-
- def load(self, asCase=False):
- if self.id is None:
- raise DocumentException(u"ID wird benötigt")
- con, cur = None, None
- try:
- con = db.getConnection()
- cur = con.cursor()
- if asCase: cur.execute(LOAD_CASE, { 'id': self.id })
- else: cur.execute(LOAD_GLOBAL, { 'id': self.id })
- row = cur.fetchone()
- if not row:
- raise DocumentException(
- u"Das angeforderte Dokument kann nicht gefunden werden.")
- self.case = row[0]
- self.size = row[1]
- self.mime = row[2]
- self.data = row[3]
- self.name = row[4]
- finally:
- db.recycleConnection(con, cur)
-
-# vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
More information about the Mpuls-commits
mailing list