Changeset 2335 for WAeUP_SRP/base
- Timestamp:
- 9 Oct 2007, 17:34:04 (17 years ago)
- Location:
- WAeUP_SRP/base
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/base/WAeUPTool.py
r2320 r2335 693 693 def showFsPicture(self,path): 694 694 """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) 696 701 if os.path.exists(picture_path): 697 702 return open(picture_path).read() 703 698 704 699 705 ###) -
WAeUP_SRP/base/Widgets.py
r2324 r2335 1740 1740 ###) 1741 1741 1742 class 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 1989 InitializeClass(FileImageWidget) 1990 1991 widgetRegistry.register(FileImageWidget) 1992 ###) 1742 1993 1743 1994 ########### -
WAeUP_SRP/base/skins/waeup_student/getApplicationInfo.py
r1871 r2335 12 12 return Info about the current Student 13 13 """ 14 import logging15 logger = logging.getLogger('Skins.getApplicationInfo')16 14 17 15 request = context.REQUEST … … 28 26 requested_id = context.getStudentId() 29 27 if requested_id and not context.isStaff() and member_id != requested_id: 28 import logging 29 logger = logging.getLogger('Skins.getApplicationInfo') 30 30 logger.info('%s tried to access application object of %s' % (member_id,requested_id)) 31 31 student_id = requested_id -
WAeUP_SRP/base/skins/waeup_utilities/viewimage.py
r2114 r2335 5 5 ##bind script=script 6 6 ##bind subpath=traverse_subpath 7 ##parameters=path 7 ##parameters=path=None 8 8 ##title= 9 9 ## … … 16 16 def set_trace(): 17 17 pass 18 if path is None and traverse_subpath: 19 path = "/".join(traverse_subpath) 18 20 return context.waeup_tool.showFsPicture(path) 19 21 #import logging
Note: See TracChangeset for help on using the changeset viewer.