def get_protocol(self): if self.model.has_protocol_document(): protocol = self.model.protocol_document.resolve_document() protocol_modified = protocol.modified().asdatetime().astimezone( pytz.utc) if self.model.modified < protocol_modified: # Return current protocol return (u'{}.docx'.format(safe_unicode(protocol.Title())), protocol.file.open()) # Create new protocol operations = ProtocolOperations() if is_word_meeting_implementation_enabled(): command_class = MergeDocxProtocolCommand else: command_class = CreateGeneratedDocumentCommand command = command_class( self.context, self.model, operations, lock_document_after_creation=False) filename = u'{}.docx'.format(operations.get_title(self.model)) return (filename, StringIO(command.generate_file_data()))
def update_protocol_document(self): """Update or create meeting's protocol.""" from opengever.meeting.command import CreateGeneratedDocumentCommand from opengever.meeting.command import MergeDocxProtocolCommand from opengever.meeting.command import ProtocolOperations from opengever.meeting.command import UpdateGeneratedDocumentCommand if self.has_protocol_document( ) and not self.protocol_document.is_locked(): # The protocol should never be changed when it is no longer locked: # the user probably has made changes manually. return operations = ProtocolOperations() if is_word_meeting_implementation_enabled(): command = MergeDocxProtocolCommand( self.get_dossier(), self, operations, lock_document_after_creation=True) else: if self.has_protocol_document(): command = UpdateGeneratedDocumentCommand( self.protocol_document, self, operations) else: command = CreateGeneratedDocumentCommand( self.get_dossier(), self, operations, lock_document_after_creation=True) command.execute()
def update_protocol_document(self, overwrite=False): """Update or create meeting's protocol.""" from opengever.meeting.command import MergeDocxProtocolCommand from opengever.meeting.command import ProtocolOperations operations = ProtocolOperations() command = MergeDocxProtocolCommand(self.get_dossier(), self, operations) command.execute(overwrite=overwrite) return command
def get_protocol(self): if self.model.has_protocol_document(): protocol = self.model.protocol_document.resolve_document() protocol_modified = protocol.modified().asdatetime().astimezone( pytz.utc) if self.model.modified < protocol_modified: # Return current protocol return (u'{}.docx'.format(safe_unicode(protocol.Title())), protocol.file.open()) # Create new protocol operations = ProtocolOperations() command = MergeDocxProtocolCommand( self.context, self.model, operations, lock_document_after_creation=False) filename = u'{}.docx'.format(operations.get_title(self.model)) return (filename, StringIO(command.generate_file_data()))
class UpdateProtocol(grok.View): grok.context(IDocumentSchema) grok.name('update_protocol') grok.require('cmf.ModifyPortalContent') operations = ProtocolOperations() @classmethod def url_for(cls, meeting): generated_document = meeting.protocol_document document = generated_document.resolve_document() return '{}/@@update_protocol?document-id={}&meeting-id={}'.format( document.absolute_url(), generated_document.document_id, meeting.meeting_id) def get_generated_document(self): document_id = self.request.get('document-id') if not document_id: raise NotFound document = GeneratedProtocol.get(document_id) if not document: raise NotFound return document def get_meeting(self): meeting_id = self.request.get('meeting-id') if not meeting_id: raise NotFound meeting = Meeting.get(meeting_id) if not meeting: raise NotFound return meeting def render(self): meeting = self.get_meeting() generated_doc = self.get_generated_document() command = UpdateGeneratedDocumentCommand(generated_doc, meeting, self.operations) command.execute() command.show_message() return self.request.RESPONSE.redirect(meeting.get_url())
def update_protocol_document(self): """Update or create meeting's protocol.""" from opengever.meeting.command import CreateGeneratedDocumentCommand from opengever.meeting.command import ProtocolOperations from opengever.meeting.command import UpdateGeneratedDocumentCommand operations = ProtocolOperations() if self.has_protocol_document(): command = UpdateGeneratedDocumentCommand(self.protocol_document, self, operations) else: command = CreateGeneratedDocumentCommand( self.get_dossier(), self, operations, lock_document_after_creation=True) command.execute()
class UpdateProtocol(BrowserView): operations = ProtocolOperations() @classmethod def url_for(cls, meeting): generated_document = meeting.protocol_document document = generated_document.resolve_document() return '{}/@@update_protocol?document-id={}&meeting-id={}'.format( document.absolute_url(), generated_document.document_id, meeting.meeting_id) def get_generated_document(self): document_id = self.request.get('document-id') if not document_id: raise NotFound document = GeneratedProtocol.get(document_id) if not document: raise NotFound return document def get_meeting(self): meeting_id = self.request.get('meeting-id') if not meeting_id: raise NotFound meeting = Meeting.get(meeting_id) if not meeting: raise NotFound return meeting def __call__(self): meeting = self.get_meeting() generated_doc = self.get_generated_document() command = UpdateGeneratedDocumentCommand(generated_doc, meeting, self.operations) command.execute() command.show_message() return self.request.RESPONSE.redirect(meeting.get_url())
class GenerateProtocol(grok.View): grok.context(IMeetingDossier) grok.name('generate_protocol') grok.require('cmf.AddPortalContent') operations = ProtocolOperations() @classmethod def url_for(cls, meeting): dossier = meeting.get_dossier() url = '{}/@@generate_protocol?meeting-id={}'.format( dossier.absolute_url(), meeting.meeting_id) return addTokenToUrl(url) def get_meeting(self): meeting_id = self.request.get('meeting-id') if not meeting_id: raise NotFound meeting = Meeting.query.with_for_update().get(meeting_id) if not meeting: raise NotFound return meeting def render(self): meeting = self.get_meeting() command = CreateGeneratedDocumentCommand( self.context, meeting, self.operations, lock_document_after_creation=True) try: command.execute() command.show_message() except ProtocolAlreadyGenerated: msg = _(u'msg_error_protocol_already_generated', default=u'The protocol for meeting ${title} has already ' u'been generated.', mapping=dict(title=meeting.get_title())) api.portal.show_message(msg, self.request, type='error') return self.request.RESPONSE.redirect(meeting.get_url())
class DownloadProtocolJson(BrowserView): operations = ProtocolOperations() def __init__(self, context, request): super(DownloadProtocolJson, self).__init__(context, request) self.model = context.model def __call__(self): response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', 'application/json') response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format('protocol.json')) return self.get_protocol_json(pretty=True) def get_protocol_json(self, pretty=False): return self.operations.get_meeting_data( self.model).as_json(pretty=pretty)
class MergeDocxProtocol(BrowserView): """Create a protocol merged from several partial protocols.""" operations = ProtocolOperations() @classmethod def url_for(cls, meeting): dossier = meeting.get_dossier() url = '{}/@@merge_docx_protocol?meeting-id={}'.format( dossier.absolute_url(), meeting.meeting_id) return addTokenToUrl(url) def __call__(self): meeting = self.context.get_meeting() command = MergeDocxProtocolCommand(self.context, meeting, self.operations, lock_document_after_creation=True) command.execute() command.show_message() return self.request.RESPONSE.redirect(meeting.get_url())
class DownloadGeneratedProtocol(BrowserView): operations = ProtocolOperations() def __init__(self, context, request): super(DownloadGeneratedProtocol, self).__init__(context, request) self.model = context.model def get_protocol_json(self, pretty=False): return self.operations.get_meeting_data( self.model).as_json(pretty=pretty) def __call__(self): sablon = Sablon(self.operations.get_sablon_template(self.model)) sablon.process(self.get_protocol_json()) assert sablon.is_processed_successfully(), sablon.stderr filename = self.operations.get_filename(self.model).encode('utf-8') response = self.request.response response.setHeader('X-Theme-Disabled', 'True') response.setHeader('Content-Type', MIME_DOCX) response.setHeader("Content-Disposition", 'attachment; filename="{}"'.format(filename)) return sablon.file_data
class DebugDocxCompose(BrowserView): """Return a zip containing all raw files for docxcompose for debugging purposes. """ operations = ProtocolOperations() def __init__(self, context, request): super(DebugDocxCompose, self).__init__(context, request) self.meeting = context.model def __call__(self): with ZipGenerator() as generator: self.add_header_sablon(generator) for index, agenda_item in enumerate(self.meeting.agenda_items, 1): self.add_agenda_item(index, agenda_item, generator) self.add_suffix_sablon(index, generator) # Return zip response = self.request.response zip_file = generator.generate() filename = '{}.zip'.format(normalize_path(self.meeting.title)) response.setHeader( "Content-Disposition", 'inline; filename="{0}"'.format( safe_unicode(filename).encode('utf-8'))) response.setHeader("Content-type", "application/zip") response.setHeader("Content-Length", os.stat(zip_file.name).st_size) return filestream_iterator(zip_file.name, 'rb') def add_header_sablon(self, generator): template = self.meeting.get_protocol_header_template() sablon = Sablon(template).process( self.operations.get_meeting_data(self.meeting).as_json()) generator.add_file(u'000_protocol_header_template.docx', StringIO(sablon.file_data)) def add_suffix_sablon(self, index, generator): template = self.meeting.get_protocol_suffix_template() if template is None: return sablon = Sablon(template).process( self.operations.get_meeting_data(self.meeting).as_json()) generator.add_file( u'{:03d}_protocol_suffix_template.docx'.format(index + 1), StringIO(sablon.file_data)) def add_agenda_item(self, index, agenda_item, generator): if agenda_item.is_paragraph: self.add_sablon_for_paragraph(index, agenda_item, generator) elif agenda_item.has_document: self.add_agenda_item_document(index, agenda_item, generator) def add_sablon_for_paragraph(self, index, agenda_item, generator): committee = self.meeting.committee.resolve_committee() template = committee.get_paragraph_template() sablon = Sablon(template).process( ProtocolData(self.meeting, [agenda_item]).as_json()) filename = u'{:03d}_paragraph_{}.docx'.format( index, safe_unicode(agenda_item.title)) generator.add_file(filename, StringIO(sablon.file_data)) def add_agenda_item_document(self, index, agenda_item, generator): document = agenda_item.resolve_document() filename = u'{:03d}_agenda_item_{}.docx'.format( index, safe_unicode(document.Title())) generator.add_file(filename, document.file.open())