- Timestamp:
- 16 Aug 2007, 21:38:55 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
WAeUP_SRP/trunk/Widgets.py
r2100 r2110 3 3 from cgi import escape 4 4 from types import * 5 import Globals 5 6 from Globals import InitializeClass 6 7 from ZPublisher.HTTPRequest import FileUpload … … 20 21 from Products.WAeUP_SRP.Academics import makeCertificateCode 21 22 #from Products.ExtFile.ExtFile import ExtFile 22 from re import compile 23 import logging 23 import logging,os,re 24 24 import operator 25 p_home = Globals.package_home(globals()) 26 i_home = Globals.INSTANCE_HOME 25 27 26 28 #from zLOG import LOG, DEBUG … … 489 491 d = {} 490 492 for k,v in voc.items(): 491 parts = k.split('_') 492 state = parts[0] 493 lga = ' '.join(parts[1:]) 493 parts = v.split(' / ') 494 if len(parts) != 2: 495 continue 496 state = parts[0].lower() 497 lga = "_".join(re.split('\W+',parts[1].lower())) 494 498 if state not in states: 495 499 states.append(state) 496 500 lgas.append(lga) 497 501 d[k] = v 502 498 503 return (d,states,lgas) 499 504 … … 522 527 return 0 523 528 elif widget_id == self.lga_field: 524 if vnot in lgas:529 if "_".join(re.split('\W+',v)) not in lgas: 525 530 datastructure.setError(widget_id, "'%s' not a valid lga" % v) 526 531 return 0 … … 615 620 valid = CPSStringWidget.validate(self, datastructure, **kw) 616 621 id_pat_str = r"\S" 617 inv_id_pat = compile(r"^%s$" % id_pat_str)622 inv_id_pat = re.compile(r"^%s$" % id_pat_str) 618 623 if not valid: 619 624 return 0 … … 666 671 value = makeCertificateCode(datastructure[widget_id]).upper() 667 672 id_pat_str = r"\S" 668 inv_id_pat = compile(r"^%s$" % id_pat_str)673 inv_id_pat = re.compile(r"^%s$" % id_pat_str) 669 674 err = 0 670 675 if len(value.split()) > 1: … … 1134 1139 v = datamodel[self.fields[0]] 1135 1140 widget_id = self.getWidgetId() 1136 #import pdb;pdb.set_trace()1137 1141 if v and type(v) is StringType: 1138 1142 try: … … 1476 1480 ###) 1477 1481 1478 class ExtFileWidget(CPSFileWidget): ###( 1479 """Photo widget.""" 1480 meta_type = 'Ext File Widget' 1481 1482 def getFileInfo(self, datastructure): ###( 1482 class NoZodbImageWidget(CPSImageWidget): ###( 1483 """Image widget with filesystem storage.""" 1484 meta_type = 'No Zodb Image Widget' 1485 _properties = CPSImageWidget._properties +\ 1486 ( 1487 {'id': 'path', 'type': 'string', 'mode': 'w', 1488 'label': 'Relative Path'}, 1489 {'id': 'id_field', 'type': 'string', 'mode': 'w', 1490 'label': 'Field to build the id'}, 1491 ) 1492 path = "images" 1493 storage_path = "%s/var/%s" % (i_home,path) 1494 id_field = "reg_no" 1495 1496 def getImageInfo(self, datastructure): ###( 1483 1497 """Get the file info from the datastructure.""" 1498 import pdb; pdb.set_trace() 1484 1499 widget_id = self.getWidgetId() 1485 1500 fileupload = datastructure[widget_id] … … 1487 1502 field_id = self.fields[0] 1488 1503 if fileupload: 1489 import pdb; pdb.set_trace()1490 1504 empty_file = False 1491 1505 session_file = isinstance(fileupload, PersistableFileUpload) … … 1506 1520 last_modified = '' 1507 1521 1508 # Find the URL for the file XXX Refactor this!1509 1510 1522 # get the adapter 1511 1523 for adapter in dm._adapters: … … 1515 1527 raise ValueError('No schema for field %r' % field_id) 1516 1528 1517 # get the content_url from the adapter1518 1529 content_url = None 1519 ob = dm.getProxy()1520 if ob is None:1521 # non proxy case1522 ob = dm.getObject()1523 if ob is None:1524 # Not stored in the ZODB.1525 # StorageAdapters that do not store the object in1526 # ZODB takes the entry_id instead of object.1527 # Get the entry_id from the datamodel context(typically1528 # a directory).1529 id_field = getattr(dm.getContext(), 'id_field', None)1530 if id_field:1531 try:1532 entry_id = datastructure[id_field]1533 except KeyError:1534 entry_id = None1535 else:1536 # No object passed, and no id_field1537 entry_id = None1538 if entry_id:1539 # some adapters does not have _getContentUrl1540 if getattr(adapter, '_getContentUrl', None) is not None:1541 content_url = adapter._getContentUrl(entry_id, field_id)1542 else:1543 if hasattr(adapter,"_getContentUrl"):1544 content_url = adapter._getContentUrl(ob, field_id,1545 current_filename)1546 else:1547 content_url = None1548 # get the mimetype1549 1530 registry = getToolByName(self, 'mimetypes_registry') 1550 1531 mimetype = (registry.lookupExtension(current_filename.lower()) or 1551 1532 registry.lookupExtension('file.bin')) 1552 1533 1553 file_info = {1534 image_info = { 1554 1535 'empty_file': empty_file, 1555 1536 'session_file': session_file, … … 1560 1541 'mimetype': mimetype, 1561 1542 } 1562 1563 return file_info 1543 if image_info['empty_file']: 1544 tag = '' 1545 height = 0 1546 width = 0 1547 else: 1548 file_path = "/%s/%s_%s.jpg" % (self.storage_path, 1549 datamodel[self.id_field], 1550 self.field_id,) 1551 # read the file from the filesystem 1552 if not os.path.exists(file_path): 1553 image = None 1554 else: 1555 image = open(file_path).read() 1556 from OFS.Image import getImageInfo 1557 image.seek(0) 1558 data = image.read(24) 1559 ct, width, height = getImageInfo(data) 1560 if width < 0: 1561 width = None 1562 if height < 0: 1563 height = None 1564 1565 if (self.allow_resize 1566 and height is not None 1567 and width is not None): 1568 z_w = z_h = 1 1569 h = int(self.display_height) 1570 w = int(self.display_width) 1571 if w and h: 1572 if w < width: 1573 z_w = w / float(width) 1574 if h < height: 1575 z_h = h / float(height) 1576 zoom = min(z_w, z_h) 1577 width = int(zoom * width) 1578 height = int(zoom * height) 1579 1580 title = image_info['current_filename'] 1581 alt = title or '' 1582 if height is None or width is None: 1583 tag = renderHtmlTag('img', src=image_info['content_url'], 1584 alt=alt, title=title) 1585 else: 1586 tag = renderHtmlTag('img', src=image_info['content_url'], 1587 width=str(width), height=str(height), 1588 alt=alt, title=title) 1589 1590 image_info['height'] = height 1591 image_info['width'] = width 1592 image_info['image_tag'] = tag 1593 return image_info 1564 1594 ###) 1565 1595 … … 1572 1602 file = None 1573 1603 else: 1574 io = ExtFile(file) 1575 if filename is None: 1576 filename = ofsfile.title 1577 headers = {'content-type': ofsfile.content_type} 1578 fs = SimpleFieldStorage(io, filename, headers) 1579 FileUpload(fs) 1580 datastructure[widget_id] = file 1604 import pdb; pdb.set_trace() 1605 datastructure[widget_id] = file_name 1581 1606 datastructure[widget_id + '_choice'] = '' 1582 1607 if file is not None: … … 1657 1682 filename = datamodel[field_id].title 1658 1683 1684 import pdb;pdb.set_trace() 1659 1685 # Set/update data 1660 import pdb;pdb.set_trace() 1686 file_path = "/%s/%s_%s.jpg" % (storage_path, 1687 datamodel[self.id_field], 1688 self.field_id,) 1661 1689 if store: 1662 # Create file 1663 #file = ExtFile(filename, fileupload, datastructure) 1664 table_store = False 1665 if not hasattr(datamodel._adapters[0],'_getContentUrl'): 1666 table_store = True 1667 ref_id = datamodel.get('reg_no') 1668 id = "%s_%s" % (ref_id,field_id) 1669 self.tempExtFile = ExtFile(id, field_id) 1670 if table_store: 1671 datastructure[widget_id] = self.tempExtFile 1672 self.tempExtFile.manage_file_upload(fileupload,content_type=mimetype) 1673 else: 1674 self._setObject(id, self.tempExtFile) 1675 self._getOb(id).manage_file_upload(fileupload, content_type) 1690 file = self.makeFile(filename, fileupload, datastructure) 1676 1691 # Fixup mimetype 1677 if mimetype and self.tempExtFile.content_type != mimetype: 1678 self.tempExtFile.content_type = mimetype 1679 # Store 1680 if not table_store: 1681 datamodel[field_id] = file 1682 elif datamodel[field_id] is not None: 1683 # Change filename 1684 if datamodel[field_id].title != filename: 1685 datamodel[field_id].title = filename 1692 if mimetype and file.content_type != mimetype: 1693 file.content_type = mimetype 1694 # Store the file in the filesystem 1695 if not os.path.exists(storage_path): 1696 os.mkdir(storage_path) 1697 open(file_path,"w").write(file) 1698 datamodel[field_id] = file_path 1699 # elif datamodel[field_id] is not None: 1700 # # Change filename 1701 # if datamodel[field_id].title != filename: 1702 # datamodel[field_id].title = filename 1703 1686 1704 1687 1705 return True 1688 1706 1689 1707 1690 InitializeClass( ExtFileWidget)1691 1692 widgetRegistry.register( ExtFileWidget)1708 InitializeClass(NoZodbImageWidget) 1709 1710 widgetRegistry.register(NoZodbImageWidget) 1693 1711 ###) 1694 1712
Note: See TracChangeset for help on using the changeset viewer.