Changeset 8467 for main/waeup.kofa/trunk/src/waeup/kofa
- Timestamp:
- 18 May 2012, 00:13:37 (12 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/students
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py
r8436 r8467 26 26 import grok 27 27 from zope.event import notify 28 from zope.component import createObject, queryUtility , getUtility28 from zope.component import createObject, queryUtility 29 29 from zope.component.hooks import setSite, clearSite 30 30 from zope.catalog.interfaces import ICatalog … … 35 35 from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase 36 36 from waeup.kofa.app import University 37 from waeup.kofa.configuration import SessionConfiguration38 37 from waeup.kofa.students.student import Student 39 38 from waeup.kofa.students.studylevel import StudentStudyLevel 40 39 from waeup.kofa.university.faculty import Faculty 41 40 from waeup.kofa.university.department import Department 42 from waeup.kofa.interfaces import IUserAccount , IKofaUtils41 from waeup.kofa.interfaces import IUserAccount 43 42 from waeup.kofa.authentication import LocalRoleSetEvent 44 43 from waeup.kofa.hostels.hostel import Hostel, Bed, NOT_OCCUPIED 45 44 46 45 PH_LEN = 2059 # Length of placeholder file 46 47 SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg') 48 SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp') 47 49 48 50 def lookup_submit_value(name, value, browser): … … 522 524 # as birth certificate 523 525 self.browser.open(self.manage_clearance_path) 524 pseudo_image = StringIO('I pretend to be a graphics file')526 image = open(SAMPLE_IMAGE, 'rb') 525 527 ctrl = self.browser.getControl(name='birthcertificateupload') 526 528 file_ctrl = ctrl.mech_control 527 file_ctrl.add_file( pseudo_image, filename='my_birth_certificate.jpg')529 file_ctrl.add_file(image, filename='my_birth_certificate.jpg') 528 530 # The Save action does not upload files 529 531 self.browser.getControl("Save").click() # submit form … … 532 534 in self.browser.contents) 533 535 # ... but the correct upload submit button does 534 pseudo_image = StringIO('I pretend to be a graphics file')536 image = open(SAMPLE_IMAGE) 535 537 ctrl = self.browser.getControl(name='birthcertificateupload') 536 538 file_ctrl = ctrl.mech_control 537 file_ctrl.add_file( pseudo_image, filename='my_birth_certificate.jpg')539 file_ctrl.add_file(image, filename='my_birth_certificate.jpg') 538 540 self.browser.getControl( 539 541 name='upload_birthcertificateupload').click() … … 546 548 self.assertEqual( 547 549 self.browser.headers['content-type'], 'image/jpeg') 548 self.assertEqual(len(self.browser.contents), 31)550 self.assertEqual(len(self.browser.contents), 2787) 549 551 # Reuploading a file which is bigger than 150k will raise an error 550 552 self.browser.open(self.manage_clearance_path) 551 photo_content = 'A' * 1024 * 151 # A string of 21 KB size552 pseudo_image = StringIO(photo_content)553 # An image > 150K 554 big_image = StringIO(open(SAMPLE_IMAGE, 'rb').read() * 75) 553 555 ctrl = self.browser.getControl(name='birthcertificateupload') 554 556 file_ctrl = ctrl.mech_control 555 file_ctrl.add_file( pseudo_image, filename='my_birth_certificate.jpg')557 file_ctrl.add_file(big_image, filename='my_birth_certificate.jpg') 556 558 self.browser.getControl( 557 559 name='upload_birthcertificateupload').click() 558 560 self.assertTrue( 559 561 'Uploaded file is too big' in self.browser.contents) 560 # File names must meet several conditions561 pseudo_image = StringIO('I pretend to be a graphics file')562 # we do not rely on filename extensions given by uploaders 563 image = open(SAMPLE_IMAGE, 'rb') # a jpg-file 562 564 ctrl = self.browser.getControl(name='birthcertificateupload') 563 565 file_ctrl = ctrl.mech_control 564 file_ctrl.add_file(pseudo_image, filename='my.photo.jpg') 566 # tell uploaded file is bmp 567 file_ctrl.add_file(image, filename='my_birth_certificate.bmp') 565 568 self.browser.getControl( 566 569 name='upload_birthcertificateupload').click() 567 self.assertTrue('File name contains more than one dot' 568 in self.browser.contents) 570 self.assertTrue( 571 # jpg file was recognized 572 'File birth_certificate.jpg uploaded.' in self.browser.contents) 573 # File names must meet several conditions 574 bmp_image = open(SAMPLE_IMAGE_BMP, 'rb') 569 575 ctrl = self.browser.getControl(name='birthcertificateupload') 570 576 file_ctrl = ctrl.mech_control 571 file_ctrl.add_file(pseudo_image, filename='my_birth_certificate') 572 self.browser.getControl( 573 name='upload_birthcertificateupload').click() 574 self.assertTrue('File name has no extension' in self.browser.contents) 575 ctrl = self.browser.getControl(name='birthcertificateupload') 576 file_ctrl = ctrl.mech_control 577 file_ctrl.add_file(pseudo_image, filename='my_birth_certificate.bmp') 577 file_ctrl.add_file(bmp_image, filename='my_birth_certificate.bmp') 578 578 self.browser.getControl( 579 579 name='upload_birthcertificateupload').click() … … 586 586 # Managers can add and delete second file 587 587 self.browser.open(self.manage_clearance_path) 588 pseudo_image = StringIO('I pretend to be a graphics file')588 image = open(SAMPLE_IMAGE, 'rb') 589 589 ctrl = self.browser.getControl(name='birthcertificateupload') 590 590 file_ctrl = ctrl.mech_control 591 file_ctrl.add_file( pseudo_image, filename='my_acceptance_letter.jpg')591 file_ctrl.add_file(image, filename='my_acceptance_letter.jpg') 592 592 self.browser.getControl( 593 593 name='upload_acceptanceletterupload').click() … … 597 597 ctrl = self.browser.getControl(name='acceptanceletterupload') 598 598 file_ctrl = ctrl.mech_control 599 file_ctrl.add_file(pseudo_image, filename='my_acceptance_letter.jpg') 599 image.seek(0) 600 file_ctrl.add_file(image, filename='my_acceptance_letter.jpg') 600 601 self.browser.getControl( 601 602 name='upload_acceptanceletterupload').click() … … 610 611 # Managers can upload a file via the StudentBaseManageFormPage 611 612 self.browser.open(self.manage_student_path) 612 pseudo_image = StringIO('I pretend to be a graphics file')613 image = open(SAMPLE_IMAGE_BMP, 'rb') 613 614 ctrl = self.browser.getControl(name='passportuploadmanage') 614 615 file_ctrl = ctrl.mech_control 615 file_ctrl.add_file( pseudo_image, filename='my_photo.bmp')616 file_ctrl.add_file(image, filename='my_photo.bmp') 616 617 self.browser.getControl( 617 618 name='upload_passportuploadmanage').click() … … 620 621 ctrl = self.browser.getControl(name='passportuploadmanage') 621 622 file_ctrl = ctrl.mech_control 622 file_ctrl.add_file(pseudo_image, filename='my_photo.jpg') 623 image = open(SAMPLE_IMAGE, 'rb') 624 file_ctrl.add_file(image, filename='my_photo.jpg') 623 625 self.browser.getControl( 624 626 name='upload_passportuploadmanage').click() … … 631 633 self.browser.open(self.student_path + '/clearance.pdf') 632 634 self.assertEqual(self.browser.headers['Status'], '200 Ok') 633 self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') 635 self.assertEqual(self.browser.headers['Content-Type'], 636 'application/pdf') 634 637 635 638 def test_manage_course_lists(self): … … 1426 1429 IWorkflowInfo(self.student).fireTransition('admit') 1427 1430 self.browser.open(self.student_path + '/change_portrait') 1428 pseudo_image = StringIO('I pretend to be a graphics file')1431 image = open(SAMPLE_IMAGE, 'rb') 1429 1432 ctrl = self.browser.getControl(name='passportuploadedit') 1430 1433 file_ctrl = ctrl.mech_control 1431 file_ctrl.add_file( pseudo_image, filename='my_photo.jpg')1434 file_ctrl.add_file(image, filename='my_photo.jpg') 1432 1435 self.browser.getControl( 1433 1436 name='upload_passportuploadedit').click() -
main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_student.py
r8452 r8467 130 130 131 131 def create_passport_img(self, student): 132 # create some fakedpassport file for `student`132 # create some passport file for `student` 133 133 storage = getUtility(IExtFileStore) 134 image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg') 135 self.image_contents = open(image_path, 'rb').read() 134 136 file_id = IFileStoreNameChooser(student).chooseName( 135 137 attr='passport.jpg') 136 storage.createFile(file_id, StringIO( 'I am a fake image.'))138 storage.createFile(file_id, StringIO(self.image_contents)) 137 139 138 140 def test_backup_single_student_data(self): … … 144 146 del_dir = self.app['datacenter'].deleted_path 145 147 del_img_path = os.path.join( 146 del_dir, 'media', 'students', '0011 1', 'A111111',148 del_dir, 'media', 'students', '00110', 'A111111', 147 149 'passport_A111111.jpg') 148 150 … … 151 153 self.assertEqual( 152 154 open(del_img_path, 'rb').read(), 153 'I am a fake image.')155 self.image_contents) 154 156 155 157 # The student data were put into CSV files -
main/waeup.kofa/trunk/src/waeup/kofa/students/viewlets.py
r8435 r8467 22 22 from zope.i18n import translate 23 23 from waeup.kofa.interfaces import ( 24 IKofaObject, IExtFileStore, IFileStoreNameChooser , IKofaUtils)24 IKofaObject, IExtFileStore, IFileStoreNameChooser) 25 25 from waeup.kofa.interfaces import MessageFactory as _ 26 26 from waeup.kofa.utils.helpers import string_from_bytes, file_size … … 29 29 PrimaryNavTab, ManageActionButton, AddActionButton) 30 30 from waeup.kofa.browser.layout import ( 31 default_primary_nav_template, 32 default_filedisplay_template, default_fileupload_template) 33 from waeup.kofa.students.workflow import (ADMITTED, PAID, 34 CLEARANCE, REQUESTED, RETURNING, CLEARED, REGISTERED, VALIDATED) 31 default_primary_nav_template, default_filedisplay_template, 32 default_fileupload_template) 33 from waeup.kofa.students.workflow import ( 34 ADMITTED, PAID, REQUESTED, RETURNING, CLEARED, REGISTERED, 35 VALIDATED) 35 36 from waeup.kofa.students.browser import ( 36 StudentClearanceManageFormPage, 37 write_log_message, StudentBaseManageFormPage,38 StudentFilesUploadPage,ExportPDFClearanceSlipPage, StudentsContainerPage,37 StudentClearanceManageFormPage, write_log_message, 38 StudentBaseManageFormPage, StudentFilesUploadPage, 39 ExportPDFClearanceSlipPage, StudentsContainerPage, 39 40 StudentsContainerManagePage, StudentBaseDisplayFormPage, 40 41 StudentClearanceDisplayFormPage, StudentPersonalDisplayFormPage, … … 44 45 StudentClearanceEditFormPage, StudentPersonalEditFormPage) 45 46 from waeup.kofa.students.interfaces import ( 46 IStudentsContainer, IStudent, 47 IStudentStudyCourse, IStudentAccommodation, IStudentStudyLevel, 48 ICourseTicket, IStudentOnlinePayment, IBedTicket, 47 IStudentsContainer, IStudent, IStudentStudyCourse, IStudentAccommodation, 48 IStudentStudyLevel, ICourseTicket, IStudentOnlinePayment, IBedTicket, 49 49 ) 50 51 from waeup.kofa.interfaces import MessageFactory as _ 50 from waeup.kofa.utils.helpers import get_fileformat 52 51 53 52 grok.context(IKofaObject) # Make IKofaObject the default context … … 512 511 grok.order(1) 513 512 grok.require('waeup.Authenticated') 514 pnav = 0 513 pnav = 0 515 514 tab_title = u'Some Text' 516 515 … … 553 552 targets += [ 554 553 {'url':student_url, 'title':'Base Data'}, 555 {'url':student_url + '/view_clearance', 'title':_('Clearance Data')}, 554 {'url':student_url + '/view_clearance', 555 'title':_('Clearance Data')}, 556 556 {'url':student_url + '/view_personal', 'title':_('Personal Data')}, 557 557 {'url':student_url + '/studycourse', 'title':_('Study Course')}, 558 558 {'url':student_url + '/payments', 'title':_('Payments')}, 559 {'url':student_url + '/accommodation', 'title':_('Accommodation Data')}, 559 {'url':student_url + '/accommodation', 560 'title':_('Accommodation Data')}, 560 561 {'url':student_url + '/history', 'title':_('History')}, 561 562 ] … … 581 582 """ 582 583 # Check some file requirements first 583 if upload.filename.count('.') == 0:584 view.flash(_('File name has no extension.'))585 return False586 if upload.filename.count('.') > 1:587 view.flash(_('File name contains more than one dot.'))588 return False589 basename, expected_ext = os.path.splitext(download_name)590 dummy, ext = os.path.splitext(upload.filename)591 ext.lower()592 if expected_ext:593 if ext != expected_ext:594 view.flash(_('${a} file extension expected.',595 mapping = {'a':expected_ext.replace('.','')}))596 return False597 else:598 if not ext.replace('.','') in ALLOWED_FILE_EXTENSIONS:599 view.flash(600 _('Only the following extension are allowed: ${a}',601 mapping = {'a':', '.join(ALLOWED_FILE_EXTENSIONS)}))602 return False603 download_name += ext604 584 size = file_size(upload) 605 585 if size > max_size: … … 607 587 return False 608 588 upload.seek(0) # file pointer moved when determining size 589 file_format = get_fileformat(None, upload.read(512)) 590 upload.seek(0) # same here 591 if file_format is None: 592 view.flash(_('Could not determine file type.')) 593 return False 594 basename, expected_ext = os.path.splitext(download_name) 595 if expected_ext: 596 if '.' + file_format != expected_ext: 597 view.flash(_('${a} file extension expected.', 598 mapping = {'a':expected_ext[1:]})) 599 return False 600 else: 601 if not file_format in ALLOWED_FILE_EXTENSIONS: 602 view.flash( 603 _('Only the following extensions are allowed: ${a}', 604 mapping = {'a':', '.join(ALLOWED_FILE_EXTENSIONS)})) 605 return False 606 download_name += '.' + file_format 609 607 store = getUtility(IExtFileStore) 610 608 file_id = IFileStoreNameChooser(context).chooseName(attr=download_name) 611 609 store.createFile(file_id, upload) 612 write_log_message(view, 'uploaded: %s (%s)' % (download_name,upload.filename)) 610 write_log_message(view, 'uploaded: %s (%s)' % ( 611 download_name,upload.filename)) 613 612 view.flash(_('File ${a} uploaded.', mapping = {'a':download_name})) 614 613 return True
Note: See TracChangeset for help on using the changeset viewer.