~sahana-eden-jp/sahana-eden/reload_s3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-

"""
    Document Library
"""

module = "doc"
#==============================================================================
resourcename = "document"
tablename = "%s_%s" % (module, resourcename)
table = db.define_table(tablename,
                        Field("name", length=128, notnull=True, unique=True, label=T("Name")),
                        Field("file", "upload", autodelete=True,),
                        Field("url", label=T("URL")),
                        person_id(label=T("Author")),
                        organisation_id(),
                        location_id(),
                        Field("date", "date"),
                        comments(),
                        Field("entered", "boolean", label=T("Entered")),
                        Field("checksum", readable=False, writable=False),
                        migrate=migrate, *s3_meta_fields())

table.name.requires = [IS_NOT_EMPTY(), IS_NOT_ONE_OF(db, "%s.name" % tablename)]

def shn_file_represent( file, table):
    if file:
        return A(table.file.retrieve(file)[0],
                 _href=URL(r=request, f="download", args=[file]))
    else:
        return NONE

table.file.represent = lambda file, table=table: shn_file_represent(file, table)
table.url.represent = lambda url: url and A(url,_href=url) or NONE

table.url.requires = [IS_NULL_OR(IS_URL()), IS_NULL_OR(IS_NOT_ONE_OF(db, "%s.url" % tablename))]

table.person_id.comment = shn_person_comment(T("Author"), T("The Author of this Document (optional)"))

table.location_id.readable = table.location_id.writable = False

table.entered.comment = DIV( _class="tooltip",
                             _title="%s|%s" % (T("Entered"),
                                               T("Has data from this Reference Document been entered into Sahana?")))

# -----------------------------------------------------------------------------
def document_represent(id):
    if not id:
        return NONE
    represent = shn_get_db_field_value(db = db,
                                       table = "doc_document",
                                       field = "name",
                                       look_up = id)
    #File
    #Website
    #Person
    return A ( represent,
               _href = URL(r=request, c="doc", f="document", args = [id], extension = ""),
               _target = "blank"
               )

DOCUMENT = T("Reference Document")
ADD_DOCUMENT = T("Add Reference Document")

document_comment = DIV( A( ADD_DOCUMENT,
                           _class="colorbox",
                           _href=URL(r=request, c="doc", f="document", args="create", vars=dict(format="popup")),
                           _target="top",
                           _title=T("If you need to add a new document then you can click here to attach one."),
                           ),
                        DIV( _class="tooltip",
                             _title="%s|%s" % (DOCUMENT,
                                T("A Reference Document such as a file, URL or contact person to verify this data. You can type the 1st few characters of the document name to link to an existing document.")),
                                #T("Add a Reference Document such as a file, URL or contact person to verify this data. If you do not enter a Reference Document, your email will be displayed instead."),
                             ),
                        #SPAN( I( T("If you do not enter a Reference Document, your email will be displayed to allow this data to be verified.") ),
                        #     _style = "color:red"
                        #     )
                        )

# CRUD Strings
LIST_DOCUMENTS = T("List Documents")
s3.crud_strings[tablename] = Storage(
    title_create = ADD_DOCUMENT,
    title_display = T("Document Details"),
    title_list = LIST_DOCUMENTS,
    title_update = T("Edit Document"),
    title_search = T("Search Documents"),
    subtitle_create = T("Add New Document"),
    subtitle_list = DOCUMENT,
    label_list_button = LIST_DOCUMENTS,
    label_create_button = ADD_DOCUMENT,
    label_delete_button = T("Delete Document"),
    msg_record_created = T("Document added"),
    msg_record_modified = T("Document updated"),
    msg_record_deleted = T("Document deleted"),
    msg_list_empty = T("No Documents found"))

document_id = S3ReusableField("document_id",
                              table,
                              requires = IS_NULL_OR(IS_ONE_OF(db, "doc_document.id",
                                                              document_represent,
                                                              orderby="doc_document.name")),
                              represent = document_represent,
                              label = DOCUMENT,
                              comment = document_comment,
                              ondelete = "RESTRICT",
                              widget = S3AutocompleteWidget(request, module, resourcename)
                             )

def document_onvalidation(form):

    import cgi

    table = db.doc_document

    doc = form.vars.file
    url = form.vars.url

    if not hasattr(doc, "file"):
        id = request.post_vars.id
        if id:
            record = db(table.id == id).select(table.file, limitby=(0, 1)).first()
            if record:
                doc = record.file

    if not hasattr(doc, "file") and not doc and not url:
        form.errors.file = \
        form.errors.url = T("Either file upload or document URL required.")

    if isinstance(doc, cgi.FieldStorage) and doc.filename:
        f = doc.file
        form.vars.checksum = docChecksum(f.read())
        f.seek(0)
    if form.vars.checksum is not None:
        result = db(table.checksum == form.vars.checksum).select(table.name,
                                                                 limitby=(0, 1)).first()
        if result:
            doc_name = result.name
            form.errors["file"] = "%s %s" % (T("This file already exists on the server as"),
                                             doc_name)
    return

s3xrc.model.configure(table,
                      mark_required=["file"],
                      onvalidation=document_onvalidation)
#==============================================================================
resourcename = "image"
tablename = "%s_%s" % (module, resourcename)
table = db.define_table(tablename,
                        Field("name", length=128, notnull=True, unique=True),
                        Field("image", "upload", autodelete=True),
                        # Web2Py r2867+ includes this functionality by default
                        #Field("image", "upload", autodelete=True, widget=S3UploadWidget.widget),
                        Field("url"),
                        person_id(),
                        organisation_id(),
                        location_id(),
                        Field("date", "date"),
                        comments(),
                        Field("checksum", readable=False, writable=False),
                        migrate=migrate, *s3_meta_fields())

table.name.requires = [IS_NOT_EMPTY(), IS_NOT_ONE_OF(db, "%s.name" % tablename)]
table.name.label = T("Name")
table.url.requires = IS_NULL_OR(IS_URL())
table.url.label = T("URL")
table.person_id.label = T("Person")

# upload folder needs to be visible to the download() function as well as the upload
table.image.uploadfolder = os.path.join(request.folder, "uploads/images")
IMAGE_EXTENSIONS = ["png", "PNG", "jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "tif", "TIF", "tiff", "TIFF", "bmp", "BMP", "raw", "RAW"]
table.image.requires = IS_IMAGE(extensions=(IMAGE_EXTENSIONS))
#table.image.requires = IS_EMPTY_OR(IS_IMAGE(extensions=(IMAGE_EXTENSIONS)))
table.image.represent = lambda image: image and \
        DIV(A(IMG(_src=URL(r=request, c="default", f="download", args=image),
                  _height=60,
                  _alt=T("View Image")),
              _href=URL(r=request, c="default", f="download", args=image))) or \
        T("No Image")

ADD_IMAGE = T("Add Photo")
image_id = S3ReusableField("image_id", db.doc_image,
                requires = IS_NULL_OR(IS_ONE_OF(db, "doc_image.id", "%(name)s")),
                represent = lambda id: (id and [DIV(A(IMG(_src=URL(r=request, c="default", f="download", args=db(db.doc_image.id == id).select(db.doc_image.image,
                                                                                                                                               limitby=(0, 1)).first().image),
                                                          _height=40),
                                                      _class="zoom",
                                                      _href="#zoom-media_image-%s" % id),
                                                DIV(IMG(_src=URL(r=request, c="default", f="download", args=db(db.doc_image.id == id).select(db.doc_image.image,
                                                                                                                                             limitby=(0, 1)).first().image),
                                                        _width=600),
                                                    _id="zoom-media_image-%s" % id,
                                                    _class="hidden"))] or [""])[0],
                label = T("Image"),
                comment = DIV(A(ADD_IMAGE,
                                _class="colorbox",
                                _href=URL(r=request, c="doc", f="image", args="create", vars=dict(format="popup")),
                                _target="top",
                                _title=ADD_IMAGE),
                          DIV( _class="tooltip",
                               _title="%s|%s" % (ADD_IMAGE,
                                                 T("Upload an image, such as a photo")))),
                ondelete = "RESTRICT"
                )

# CRUD Strings
LIST_IMAGES = T("List Photos")
s3.crud_strings[tablename] = Storage(
    title_create = ADD_IMAGE,
    title_display = T("Photo Details"),
    title_list = LIST_IMAGES,
    title_update = T("Edit Photo"),
    title_search = T("Search Photos"),
    subtitle_create = T("Add New Photo"),
    subtitle_list = T("Photo"),
    label_list_button = LIST_IMAGES,
    label_create_button = ADD_IMAGE,
    label_delete_button = T("Delete Photo"),
    msg_record_created = T("Photo added"),
    msg_record_modified = T("Photo updated"),
    msg_record_deleted = T("Photo deleted"),
    msg_list_empty = T("No Photos found"))

def image_onvalidation(form):

    import cgi

    table = db.doc_image

    img = form.vars.image

    if not hasattr(img, "file"):
        id = request.post_vars.id
        if id:
            record = db(table.id == id).select(table.image,
                                               limitby=(0, 1)).first()
            if record:
                img = record.image

    if isinstance(img, cgi.FieldStorage) and img.filename:
        f = img.file
        form.vars.checksum = docChecksum(f.read())
        f.seek(0)
    if form.vars.checksum is not None:
        result = db(table.checksum == form.vars.checksum).select(table.name,
                                                                 limitby=(0, 1)).first()
        if result:
            image_name = result.name
            form.errors["image"] = "%s %s" % (T("This file already exists on the server as"), image_name)
    return

s3xrc.model.configure(table,
                      onvalidation=image_onvalidation)

#==============================================================================