Changeset 2335


Ignore:
Timestamp:
9 Oct 2007, 17:34:04 (17 years ago)
Author:
joachim
Message:

use File Image Widget in clearance, don't use on live system

Location:
WAeUP_SRP
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/base/WAeUPTool.py

    r2320 r2335  
    693693    def showFsPicture(self,path):
    694694        """return a picture from the filesystem"""
    695         picture_path = "%s/import/%s" % (i_home,path)
     695        picture_path = os.path.join(i_home,path)
     696        response = self.REQUEST.RESPONSE
     697        if os.path.exists(picture_path):
     698            response.setHeader('Content-type','image/jpeg')
     699            return open(picture_path).read()
     700        picture_path = os.path.join(i_home,'import',path)
    696701        if os.path.exists(picture_path):
    697702            return open(picture_path).read()
     703       
    698704
    699705    ###)
  • WAeUP_SRP/base/Widgets.py

    r2324 r2335  
    17401740###)
    17411741
     1742class FileImageWidget(CPSImageWidget): ###(
     1743    """Image widget with filesystem storage."""
     1744    meta_type = 'File Image Widget'
     1745    _properties = CPSImageWidget._properties +\
     1746    (
     1747     {'id': 'path', 'type': 'string', 'mode': 'w',
     1748      'label': 'Relative Path'},
     1749     {'id': 'id_field', 'type': 'string', 'mode': 'w',
     1750      'label': 'Field to build the id'},
     1751    )
     1752    path = "images"
     1753    storage_path = "%s/%s" % (i_home,path)
     1754    id_field = ""
     1755
     1756    def getImageInfo(self, datastructure): ###(
     1757        """Get the file info from the datastructure."""
     1758        #import pdb; pdb.set_trace()
     1759        widget_id = self.getWidgetId()
     1760        if  datastructure.has_key(widget_id):
     1761            fileupload = datastructure[widget_id]
     1762            dm = datastructure.getDataModel()
     1763            field_id = self.fields[0]
     1764            if self.id_field == "":
     1765                student_id = self.getStudentId()
     1766            else:
     1767                student_id = datastructure[self.id_field]
     1768            content_url = os.path.join(self.portal_url(),
     1769                                       "viewimage",
     1770                                       self.path,
     1771                                       student_id,
     1772                                       "%s_%s.jpg" % (self.getWidgetId(),student_id),
     1773                                       )
     1774            image_title = "%s_%s.jpg" % (widget_id,student_id)
     1775            current_filename = os.path.join(student_id,
     1776                                            image_title)
     1777            file_path = os.path.join(self.storage_path,
     1778                                     current_filename)
     1779        else:
     1780            file_path = "XXX"
     1781            image_title = ""
     1782        # read the file from the filesystem
     1783        if not os.path.exists(file_path):
     1784            height = -1
     1785            width = -1
     1786            empty_file = True
     1787            session_file = False
     1788            current_filename = ''
     1789            content_url = ''
     1790            size = 0
     1791            mimetype = ''
     1792            last_modified = ''
     1793            height = ''
     1794            width = ''
     1795        else:
     1796            image = open(file_path)
     1797            from OFS.Image import getImageInfo as getImageInfoOFS
     1798            image.seek(0)
     1799            data = image.read(2000)
     1800            size = len(data)
     1801            empty_file = size == 0
     1802            session_file = False
     1803            last_modified = ''
     1804            image.close()
     1805            mimetype, width, height = getImageInfoOFS(data)
     1806            registry = getToolByName(self, 'mimetypes_registry')
     1807            mimetype = (registry.lookupExtension(current_filename.lower()) or
     1808                        registry.lookupExtension('file.bin'))
     1809            if width < 0:
     1810                width = None
     1811            if height < 0:
     1812                height = None
     1813
     1814            if (self.allow_resize
     1815                and height is not None
     1816                and width  is not None):
     1817                z_w = z_h = 1
     1818                h = int(self.display_height)
     1819                w = int(self.display_width)
     1820                if w and h:
     1821                    if w < width:
     1822                        z_w = w / float(width)
     1823                    if h < height:
     1824                        z_h = h / float(height)
     1825                    zoom = min(z_w, z_h)
     1826                    width = int(zoom * width)
     1827                    height = int(zoom * height)
     1828                #import pdb;pdb.set_trace()
     1829        title = image_title
     1830        image_info = {
     1831            'empty_file': empty_file,
     1832            'session_file': session_file,
     1833            'current_filename': title,
     1834            'size': size,
     1835            'last_modified': last_modified,
     1836            'content_url': content_url,
     1837            'mimetype': mimetype,
     1838            }
     1839        alt = title or ''
     1840        #height = int(self.display_height)
     1841        #width = int(self.display_width)
     1842        if height is None or width is None:
     1843            tag = renderHtmlTag('img', src=image_info['content_url'],
     1844                                alt=alt, title=title)
     1845        else:
     1846            tag = renderHtmlTag('img', src=image_info['content_url'],
     1847                                width=str(width), height=str(height),
     1848                                alt=alt, title=title)
     1849
     1850        image_info['height'] = height
     1851        image_info['width'] = width
     1852        image_info['image_tag'] = tag
     1853        return image_info
     1854    ###)
     1855
     1856    def checkFileName(self, filename, mimetype):
     1857        return '', {}
     1858        if mimetype and mimetype.startswith('image'):
     1859            return '', {}
     1860        return 'cpsschemas_err_image', {}
     1861
     1862    def prepare(self, datastructure, **kw): ###(
     1863        """Prepare datastructure from datamodel."""
     1864        datamodel = datastructure.getDataModel()
     1865        widget_id = self.getWidgetId()
     1866        file_name = datamodel[self.fields[0]]
     1867        #import pdb; pdb.set_trace()
     1868        if self.allow_resize:
     1869            datastructure[self.getWidgetId() + '_resize'] = ''
     1870        datastructure[widget_id] = file_name
     1871        datastructure[widget_id + '_choice'] = 'change'
     1872        title = 'Passport Foto'
     1873        datastructure[widget_id + '_filename'] = title
     1874    ###)
     1875
     1876    def validate(self, datastructure, **kw): ###(
     1877        """Update datamodel from user data in datastructure.
     1878        """
     1879        logger = logging.getLogger('Widgets.FileImageWidget.validate')
     1880        datamodel = datastructure.getDataModel()
     1881        field_id = self.fields[0]
     1882        widget_id = self.getWidgetId()
     1883        store = False
     1884        fileupload = None
     1885        mimetype = None
     1886        old_file = datamodel[field_id]
     1887        # if old_file is not None:
     1888        #     old_filename = old_file.title
     1889        # else:
     1890        #     old_filename = ''
     1891        choice = datastructure[widget_id+'_choice']
     1892        fileupload = datastructure[widget_id]
     1893        is_upload = isinstance(fileupload, FileUpload)
     1894        #import pdb; pdb.set_trace()
     1895        if not is_upload and not datamodel[field_id]:
     1896            if self.is_required:
     1897                return self.validateError('Picture upload required', {},
     1898                                          datastructure)
     1899        if self.id_field == "":
     1900            student_id = self.getStudentId()
     1901        else:
     1902            student_id = datastructure[self.id_field]
     1903        if choice == 'delete':
     1904            if self.is_required:
     1905                return self.validateError('cpsschemas_err_required', {},
     1906                                          datastructure)
     1907            ext ='jpg'
     1908            filename = "%s_%s.%s" % (self.getWidgetId(),
     1909                                     student_id,
     1910                                     ext)
     1911            # Remove the file in the filesystem
     1912            student_path = os.path.join(self.storage_path,student_id)
     1913            full_path = os.path.join(student_path, filename)
     1914            if os.path.exists(full_path):
     1915                os.remove(full_path)
     1916            datamodel[field_id] = None
     1917        elif choice == 'keep':
     1918            fileupload = datastructure[widget_id]
     1919            if isinstance(fileupload, PersistableFileUpload):
     1920                # Keeping something from the session means we
     1921                # actually want to store it.
     1922                store = True
     1923            # else:
     1924            #     # Nothing to change, don't pollute datastructure
     1925            #     # with something costly already stored, which therefore
     1926            #     # doesn't need to be kept in the session.
     1927            #     self.unprepare(datastructure)
     1928        elif choice == 'change' and is_upload:
     1929            if not fileupload:
     1930                return self.validateError('cpsschemas_err_file_empty', {},
     1931                                          datastructure)
     1932            if not isinstance(fileupload, FileUpload):
     1933                return self.validateError('cpsschemas_err_file', {},
     1934                                          datastructure)
     1935            fileupload.seek(0, 2) # end of file
     1936            size = fileupload.tell()
     1937            if not size:
     1938                return self.validateError('cpsschemas_err_file_empty', {},
     1939                                          datastructure)
     1940            if self.size_max and size > self.size_max:
     1941                max_size_str = self.getHumanReadableSize(self.size_max)
     1942                err = 'This file is too big, the allowed max size is ${max_size}'
     1943                logger.info('%s tried to upload picture with size %dk' %(datastructure['reg_no'],int(size)/1000) )
     1944                err_mapping = {'max_size': max_size_str}
     1945                return self.validateError(err, err_mapping, datastructure)
     1946            store = True
     1947
     1948
     1949        # Find filename
     1950        if is_upload and store:
     1951            ext ='jpg'
     1952            filename = "%s_%s.%s" % (self.getWidgetId(),
     1953                                     student_id,
     1954                                     ext)
     1955            datamodel[field_id] = filename
     1956            registry = getToolByName(self, 'mimetypes_registry')
     1957            mimetype = registry.lookupExtension(filename.lower())
     1958            if mimetype is not None:
     1959                mimetype = str(mimetype) # normalize
     1960            #import pdb; pdb.set_trace()
     1961            file = self.makeFile(filename, fileupload, datastructure)
     1962            # Fixup mimetype
     1963            if mimetype and file.content_type != mimetype:
     1964                file.content_type = mimetype
     1965            # Store the file in the filesystem
     1966            student_path = os.path.join(self.storage_path,student_id)
     1967            if not os.path.exists(student_path):
     1968                os.mkdir(student_path)
     1969            full_path = os.path.join(student_path, filename)
     1970            #import pdb;pdb.set_trace()
     1971            pict = open(full_path,"w")
     1972            fileupload.seek(0)
     1973            pict.write(fileupload.read())
     1974            pict.close()
     1975        return True
     1976
     1977###)
     1978
     1979    def render(self, mode, datastructure, **kw): ###(
     1980        render_method = 'widget_image_render'
     1981        meth = getattr(self, render_method, None)
     1982        if meth is None:
     1983            raise RuntimeError("Unknown Render Method %s for widget type %s"
     1984                               % (render_method, self.getId()))
     1985        img_info = self.getImageInfo(datastructure)
     1986        return meth(mode=mode, datastructure=datastructure, **img_info)
     1987    ###)
     1988
     1989InitializeClass(FileImageWidget)
     1990
     1991widgetRegistry.register(FileImageWidget)
     1992###)
    17421993
    17431994###########
  • WAeUP_SRP/base/skins/waeup_student/getApplicationInfo.py

    r1871 r2335  
    1212return Info about the current Student
    1313"""
    14 import logging
    15 logger = logging.getLogger('Skins.getApplicationInfo')
    1614
    1715request = context.REQUEST
     
    2826requested_id = context.getStudentId()
    2927if requested_id and not context.isStaff() and member_id != requested_id:
     28    import logging
     29    logger = logging.getLogger('Skins.getApplicationInfo')
    3030    logger.info('%s tried to access application object of %s' % (member_id,requested_id))
    3131    student_id = requested_id
  • WAeUP_SRP/base/skins/waeup_utilities/viewimage.py

    r2114 r2335  
    55##bind script=script
    66##bind subpath=traverse_subpath
    7 ##parameters=path
     7##parameters=path=None
    88##title=
    99##
     
    1616    def set_trace():
    1717        pass
     18if path is None and traverse_subpath:
     19    path = "/".join(traverse_subpath)
    1820return context.waeup_tool.showFsPicture(path)
    1921#import logging
  • WAeUP_SRP/uniben/profiles/default/layouts/student_clearance.xml

    r1958 r2335  
    88 <property name="flexible_widgets"/>
    99 <property name="validate_values_expr"></property>
    10  <widget name="acc_inst" meta_type="Image Widget">
     10 <widget name="acc_inst" meta_type="File Image Widget">
    1111  <property name="title">Accepted by Institution</property>
    1212  <property name="fields">
     
    2525  <property name="allow_resize">True</property>
    2626 </widget>
    27  <widget name="acc_let" meta_type="Image Widget">
     27 <widget name="acc_let" meta_type="File Image Widget">
    2828  <property name="title">Acceptance Letter </property>
    2929  <property name="fields">
     
    4242  <property name="allow_resize">True</property>
    4343 </widget>
    44  <widget name="age_dec" meta_type="Image Widget">
     44 <widget name="age_dec" meta_type="File Image Widget">
    4545  <property name="title">Age Declaration</property>
    4646  <property name="fields">
     
    115115  <property name="hidden_readonly_layout_modes"/>
    116116 </widget>
    117  <widget name="alr_scan" meta_type="Image Widget">
     117 <widget name="alr_scan" meta_type="File Image Widget">
    118118  <property name="title">Scan of Advanced Level Result</property>
    119119  <property name="fields">
     
    132132  <property name="allow_resize">True</property>
    133133 </widget>
    134  <widget name="birth_certificate" meta_type="Image Widget">
     134 <widget name="birth_certificate" meta_type="File Image Widget">
    135135  <property name="title">Birth Certificate</property>
    136136  <property name="fields">
     
    165165  <property name="time_setting">False</property>
    166166 </widget>
    167  <widget name="cert" meta_type="Image Widget">
     167 <widget name="cert" meta_type="File Image Widget">
    168168  <property name="title">Certificate</property>
    169169  <property name="fields">
     
    196196  <property name="prefix">CLR</property>
    197197 </widget>
    198  <widget name="cred" meta_type="Image Widget">
     198 <widget name="cred" meta_type="File Image Widget">
    199199  <property name="title">Credential</property>
    200200  <property name="fields">
     
    280280  <property name="time_setting">False</property>
    281281 </widget>
    282  <widget name="evid" meta_type="Image Widget">
     282 <widget name="evid" meta_type="File Image Widget">
    283283  <property name="title">Evidence of Name</property>
    284284  <property name="fields">
     
    369369  <property name="hidden_readonly_layout_modes"/>
    370370 </widget>
    371  <widget name="fst_sit_scan" meta_type="Image Widget">
     371 <widget name="fst_sit_scan" meta_type="File Image Widget">
    372372  <property name="title">Scan of First Sitting Result</property>
    373373  <property name="fields">
     
    440440  <property name="hidden_readonly_layout_modes"/>
    441441 </widget>
    442  <widget name="hq_scan" meta_type="Image Widget">
     442 <widget name="hq_scan" meta_type="File Image Widget">
    443443  <property name="title">Scan of Higher Qualification Result</property>
    444444  <property name="fields">
     
    498498  <property name="vocabulary">high_qual</property>
    499499 </widget>
    500  <widget name="jamb_let" meta_type="Image Widget">
     500 <widget name="jamb_let" meta_type="File Image Widget">
    501501  <property name="title">JAMB Letter</property>
    502502  <property name="fields">
     
    515515  <property name="allow_resize">True</property>
    516516 </widget>
    517  <widget name="jamb_slip" meta_type="Image Widget">
     517 <widget name="jamb_slip" meta_type="File Image Widget">
    518518  <property name="title">JAMB Slip</property>
    519519  <property name="fields">
     
    546546  <property name="vocabulary">local_gov_areas</property>
    547547 </widget>
    548  <widget name="lga_ident" meta_type="Image Widget">
     548 <widget name="lga_ident" meta_type="File Image Widget">
    549549  <property name="title">LGA Identification</property>
    550550  <property name="fields">
     
    593593  <property name="vocabulary">states</property>
    594594 </widget>
    595  <widget name="ref_let" meta_type="Image Widget">
     595 <widget name="ref_let" meta_type="File Image Widget">
    596596  <property name="title">Referee Letter</property>
    597597  <property name="fields">
     
    610610  <property name="allow_resize">True</property>
    611611 </widget>
    612  <widget name="res_stat" meta_type="Image Widget">
     612 <widget name="res_stat" meta_type="File Image Widget">
    613613  <property name="title">Result Statement</property>
    614614  <property name="fields">
     
    684684  <property name="hidden_readonly_layout_modes"/>
    685685 </widget>
    686  <widget name="scd_sit_scan" meta_type="Image Widget">
     686 <widget name="scd_sit_scan" meta_type="File Image Widget">
    687687  <property name="title">Scan of Second Sitting Result</property>
    688688  <property name="fields">
     
    715715  <property name="vocabulary">exam_types</property>
    716716 </widget>
    717  <widget name="stat_dec" meta_type="Image Widget">
     717 <widget name="stat_dec" meta_type="File Image Widget">
    718718  <property name="title">Statutory Declaration of Good Conduct</property>
    719719  <property name="fields">
  • WAeUP_SRP/uniben/profiles/default/layouts/student_clearance_fe.xml

    r1978 r2335  
    88 <property name="flexible_widgets"/>
    99 <property name="validate_values_expr"></property>
    10  <widget name="acc_inst" meta_type="Image Widget">
     10 <widget name="acc_inst" meta_type="File Image Widget">
    1111  <property name="title">Accepted by Institution</property>
    1212  <property name="fields">
     
    2525  <property name="allow_resize">True</property>
    2626 </widget>
    27  <widget name="acc_let" meta_type="Image Widget">
     27 <widget name="acc_let" meta_type="File Image Widget">
    2828  <property name="title">Acceptance Letter </property>
    2929  <property name="fields">
     
    4242  <property name="allow_resize">True</property>
    4343 </widget>
    44  <widget name="age_dec" meta_type="Image Widget">
     44 <widget name="age_dec" meta_type="File Image Widget">
    4545  <property name="title">Age Declaration</property>
    4646  <property name="fields">
     
    116116  <property name="hidden_readonly_layout_modes"/>
    117117 </widget>
    118  <widget name="alr_scan" meta_type="Image Widget">
     118 <widget name="alr_scan" meta_type="File Image Widget">
    119119  <property name="title">Scan of Advanced Level Result</property>
    120120  <property name="fields">
     
    133133  <property name="allow_resize">True</property>
    134134 </widget>
    135  <widget name="birth_certificate" meta_type="Image Widget">
     135 <widget name="birth_certificate" meta_type="File Image Widget">
    136136  <property name="title">Birth Certificate</property>
    137137  <property name="fields">
     
    166166  <property name="time_setting">False</property>
    167167 </widget>
    168  <widget name="cert" meta_type="Image Widget">
     168 <widget name="cert" meta_type="File Image Widget">
    169169  <property name="title">Certificate</property>
    170170  <property name="fields">
     
    197197  <property name="prefix">CLR</property>
    198198 </widget>
    199  <widget name="cred" meta_type="Image Widget">
     199 <widget name="cred" meta_type="File Image Widget">
    200200  <property name="title">Credential</property>
    201201  <property name="fields">
     
    281281  <property name="time_setting">False</property>
    282282 </widget>
    283  <widget name="evid" meta_type="Image Widget">
     283 <widget name="evid" meta_type="File Image Widget">
    284284  <property name="title">Evidence of Name</property>
    285285  <property name="fields">
     
    355355  <property name="hidden_readonly_layout_modes"/>
    356356 </widget>
    357  <widget name="fst_sit_scan" meta_type="Image Widget">
     357 <widget name="fst_sit_scan" meta_type="File Image Widget">
    358358  <property name="title">Scan of First Sitting Result</property>
    359359  <property name="fields">
     
    426426  <property name="hidden_readonly_layout_modes"/>
    427427 </widget>
    428  <widget name="hq_scan" meta_type="Image Widget">
     428 <widget name="hq_scan" meta_type="File Image Widget">
    429429  <property name="title">Scan of Higher Qualification Result</property>
    430430  <property name="fields">
     
    484484  <property name="vocabulary">high_qual</property>
    485485 </widget>
    486  <widget name="jamb_let" meta_type="Image Widget">
     486 <widget name="jamb_let" meta_type="File Image Widget">
    487487  <property name="title">JAMB Letter</property>
    488488  <property name="fields">
     
    501501  <property name="allow_resize">True</property>
    502502 </widget>
    503  <widget name="jamb_slip" meta_type="Image Widget">
     503 <widget name="jamb_slip" meta_type="File Image Widget">
    504504  <property name="title">JAMB Slip</property>
    505505  <property name="fields">
     
    533533  <property name="vocabulary">local_gov_areas</property>
    534534 </widget>
    535  <widget name="lga_ident" meta_type="Image Widget">
     535 <widget name="lga_ident" meta_type="File Image Widget">
    536536  <property name="title">LGA Identification</property>
    537537  <property name="fields">
     
    599599  <property name="vocabulary">states</property>
    600600 </widget>
    601  <widget name="ref_let" meta_type="Image Widget">
     601 <widget name="ref_let" meta_type="File Image Widget">
    602602  <property name="title">Referee Letter</property>
    603603  <property name="fields">
     
    616616  <property name="allow_resize">True</property>
    617617 </widget>
    618  <widget name="res_stat" meta_type="Image Widget">
     618 <widget name="res_stat" meta_type="File Image Widget">
    619619  <property name="title">Result Statement</property>
    620620  <property name="fields">
     
    690690  <property name="hidden_readonly_layout_modes"/>
    691691 </widget>
    692  <widget name="scd_sit_scan" meta_type="Image Widget">
     692 <widget name="scd_sit_scan" meta_type="File Image Widget">
    693693  <property name="title">Scan of Second Sitting Result</property>
    694694  <property name="fields">
     
    721721  <property name="vocabulary">exam_types</property>
    722722 </widget>
    723  <widget name="stat_dec" meta_type="Image Widget">
     723 <widget name="stat_dec" meta_type="File Image Widget">
    724724  <property name="title">Statutory Declaration of Good Conduct</property>
    725725  <property name="fields">
Note: See TracChangeset for help on using the changeset viewer.