Exemplo n.º 1
0
 def __init__(self, context, request, view, manager):
     super(VersionFileListingViewlet, self).__init__(context, request, view, manager)
     # !+AttachmentableVersion viewlet should not trigger for versions
     # whose head item is not Attachmentable! We only want to display the
     # list attachments on versions whose head item is Attachmentable...
     self.for_display = IFeatureAttachment.providedBy(
         removeSecurityProxy(self.context).head)
Exemplo n.º 2
0
 def __init__(self, context, request, view, manager):
     super(VersionFileListingViewlet,
           self).__init__(context, request, view, manager)
     # !+AttachmentableVersion viewlet should not trigger for versions
     # whose head item is not Attachmentable! We only want to display the
     # list attachments on versions whose head item is Attachmentable...
     self.for_display = IFeatureAttachment.providedBy(
         removeSecurityProxy(self.context).head)
Exemplo n.º 3
0
 def create_body_structure(self, ob):
     """ Creates bill specific akomantoso xml 
     """
     bill_element = self.create_element(self.document_type)
     country = uri.get_country(ob)
     meta_element = self.create_element("meta")
     bill_element.appendChild(meta_element)
     
     # Identification
     identification_element = self.create_element("identification", source="bungeni")
     meta_element.appendChild(identification_element)
     # FRBRWork
     identification_element.appendChild(self.create_frbr_work_element(ob))
     # FRBRExpression
     identification_element.appendChild(self.create_frbr_expression_element(ob))
     # FRBRManifestation
     identification_element.appendChild(self.create_frbr_manifestation_element(ob))
     
     # Publication
     meta_element.appendChild(self.create_publication_element(ob))
     
     # Lifecycle
     if hasattr(ob, "events"):
         if len(ob.events) > 0:
             lifecycle_element = self.create_element("lifecycle",
                 source="#bungeni")
             for item in ob.events.values():
                 event_element = self.create_element("event",
                     id="evn%s" % item.doc_id,
                     type="generation",
                     date=item.doc_date and item.doc_date.strftime("%Y-%m-%d") or "",
                     source="orig")
                 lifecycle_element.appendChild(event_element)
             
             meta_element.appendChild(lifecycle_element)
     
     # References
     references_element = self.create_element("references", source="#bungeni")
     meta_element.appendChild(references_element)
     # Original
     original_element = self.create_element("original", 
         id="orig", 
         href=uri.get_frbr_expression_url(ob), 
         showAs=self.get_title(ob))
     references_element.appendChild(original_element)
     # Parliament TLCOrganization
     tlc_parliament_element = self.create_element("TLCOrganization", 
         id="parliament",
         href="/ontology/organization/%s/%s.bungeni" % (country, country),
         showAs="Parliament")
     references_element.appendChild(tlc_parliament_element)
     # Bungeni TLCOrganization
     tlc_bungeni_element = self.create_element("TLCOrganization",
         id="bungeni",
         href="/ontology/organization/%s/%s.parliament" % (country, country),
         showAs="Bungeni")
     references_element.appendChild(tlc_bungeni_element)
     
     # Body
     body_element = self.create_element("body")
     bill_element.appendChild(body_element)
     
     # Title
     title_element = self.create_element("title")
     title_element.appendChild(self.response.createTextNode(self.get_title(ob)))
     body_element.appendChild(title_element)
     
     # Article
     article_element = self.create_element("article")
     article_element.appendChild(self.response.createTextNode(self.get_body(ob)))
     body_element.appendChild(article_element)
     
     # Files
     if IFeatureAttachment.providedBy(ob):
         files = [f for f in ob.files.values()]
         if len(files) > 0:
             attachments_element = self.create_element("attachments")
             bill_element.appendChild(attachments_element)
             for file in files:
                 attachment_element = self.create_element("attachment",
                     id="att%s" % file.attachment_id,
                     href="%s/%s" % (uri.get_frbr_expression_url(ob), file.name),
                     showAs=file.title
                 )
                 attachments_element.appendChild(attachment_element)
     
     return bill_element
Exemplo n.º 4
0
    def create_body_structure(self, ob):
        """ Creates bill specific akomantoso xml 
        """
        bill_element = self.create_element(self.document_type)
        country = uri.get_country(ob)
        meta_element = self.create_element("meta")
        bill_element.appendChild(meta_element)

        # Identification
        identification_element = self.create_element("identification",
                                                     source="bungeni")
        meta_element.appendChild(identification_element)
        # FRBRWork
        identification_element.appendChild(self.create_frbr_work_element(ob))
        # FRBRExpression
        identification_element.appendChild(
            self.create_frbr_expression_element(ob))
        # FRBRManifestation
        identification_element.appendChild(
            self.create_frbr_manifestation_element(ob))

        # Publication
        meta_element.appendChild(self.create_publication_element(ob))

        # Lifecycle
        if hasattr(ob, "events"):
            if len(ob.events) > 0:
                lifecycle_element = self.create_element("lifecycle",
                                                        source="#bungeni")
                for item in ob.events.values():
                    event_element = self.create_element(
                        "event",
                        id="evn%s" % item.doc_id,
                        type="generation",
                        date=item.doc_date
                        and item.doc_date.strftime("%Y-%m-%d") or "",
                        source="orig")
                    lifecycle_element.appendChild(event_element)

                meta_element.appendChild(lifecycle_element)

        # References
        references_element = self.create_element("references",
                                                 source="#bungeni")
        meta_element.appendChild(references_element)
        # Original
        original_element = self.create_element(
            "original",
            id="orig",
            href=uri.get_frbr_expression_url(ob),
            showAs=self.get_title(ob))
        references_element.appendChild(original_element)
        # Parliament TLCOrganization
        tlc_parliament_element = self.create_element(
            "TLCOrganization",
            id="parliament",
            href="/ontology/organization/%s/%s.bungeni" % (country, country),
            showAs="Parliament")
        references_element.appendChild(tlc_parliament_element)
        # Bungeni TLCOrganization
        tlc_bungeni_element = self.create_element(
            "TLCOrganization",
            id="bungeni",
            href="/ontology/organization/%s/%s.parliament" %
            (country, country),
            showAs="Bungeni")
        references_element.appendChild(tlc_bungeni_element)

        # Body
        body_element = self.create_element("body")
        bill_element.appendChild(body_element)

        # Title
        title_element = self.create_element("title")
        title_element.appendChild(
            self.response.createTextNode(self.get_title(ob)))
        body_element.appendChild(title_element)

        # Article
        article_element = self.create_element("article")
        article_element.appendChild(
            self.response.createTextNode(self.get_body(ob)))
        body_element.appendChild(article_element)

        # Files
        if IFeatureAttachment.providedBy(ob):
            files = [f for f in ob.files.values()]
            if len(files) > 0:
                attachments_element = self.create_element("attachments")
                bill_element.appendChild(attachments_element)
                for file in files:
                    attachment_element = self.create_element(
                        "attachment",
                        id="att%s" % file.attachment_id,
                        href="%s/%s" %
                        (uri.get_frbr_expression_url(ob), file.name),
                        showAs=file.title)
                    attachments_element.appendChild(attachment_element)

        return bill_element
Exemplo n.º 5
0
def publish_to_xml(context):
    """Generates XML for object and saves it to the file. If object contains
    attachments - XML is saved in zip archive with all attached files. 
    """
    context = zope.security.proxy.removeSecurityProxy(context)
    obj_type = IWorkflow(context).name
    #locking
    random_name_sfx = generate_random_filename()
    context_file_name = "%s-%s" % (stringKey(context), random_name_sfx)
    #lock_name = "%s-%s" %(obj_type, context_file_name)
    #!+LOCKING(AH, 25-01-2014) disabling file locking
    #! locking was reqiured when the serializer used ta constant file name
    #! for an object. Now serialized file names are unique, and non repeated
    #with LockStore.get_lock(lock_name):
    #    
    #root key (used to cache files to zip)
    root_key = make_key()
    # create a fake interaction to ensure items requiring a participation
    # are serialized 
    #!+SERIALIZATION(mb, Jan-2013) review this approach
    try:
        zope.security.management.getInteraction()
    except zope.security.interfaces.NoInteraction:
        principal = zope.security.testing.Principal("user", "manager", ())
        zope.security.management.newInteraction(create_participation(principal))
    include = []
    # data dict to be published
    data = {}
    if IFeatureVersion.providedBy(context):
        include.append("versions")
    if IFeatureEvent.providedBy(context):
        include.append("event")
    
    exclude = ["data", "event", "attachments"]
    updated_dict = obj2dict(context, 1, 
	    parent=None,
	    include=include,
	    exclude=exclude,
	    root_key=root_key
        )
    data.update(
        updated_dict
    )

    tags = IStateController(context).get_state().tags
    if tags:
        data["tags"] = tags
    permissions = get_object_state_rpm(context).permissions
    data["permissions"] = get_permissions_dict(permissions)

    # setup path to save serialized data
    path = os.path.join(setupStorageDirectory(), obj_type)
    log.info("Setting up path to write to : %s", path)
    if not os.path.exists(path):
        #
        # !+THREADSAFE(AH, 2014-09-24) making makedirs threadsafe, 
        # sometimes between checking for existence and execution 
        # of makedirs() the folder has already been created by 
        # another thread
        try:
            os.makedirs(path)
        except OSError as exc:
            if exc.errno == errno.EEXIST and os.path.isdir(path):
                log.info("Error Folder : %s already exists, ignoring exception ", path)
            else:
                raise

    # xml file path
    file_path = os.path.join(path, context_file_name) 
    # files to zip
    files = []

    if IFeatureAttachment.providedBy(context):
        attachments = getattr(context, "attachments", None)
        if attachments:
	    data["attachments"] = []
	    for attachment in attachments:
	        # serializing attachment
	        attachment_dict = obj2dict(attachment, 1,
	            parent=context,
	            exclude=["data", "event", "versions"])
	        # saving attachment to tmp
	        attached_file = tmp(delete=False)
	        attached_file.write(attachment.data)
	        attached_file.flush()
	        attached_file.close()
	        files.append(attached_file.name)
	        attachment_dict["saved_file"] = os.path.basename(
	            attached_file.name
	        )
	        data["attachments"].append(attachment_dict)

    # add explicit origin chamber for this object (used to partition data
    # in if more than one chamber exists)
    
    if obj_type == "Legislature":
        data["origin_chamber"] = None
    else:
        data["origin_chamber"] = get_origin_chamber(context)

    # add any additional files to file list
    files = files + PersistFiles.get_files(root_key)
    # zipping xml, attached files plus any binary fields
    # also remove the temporary files
    if files:
        # generate temporary xml file
        temp_xml = tmp(delete=False)
        temp_xml.write(serialize(data, name=obj_type))
        temp_xml.close()
        # write attachments/binary fields to zip
        with  ZipFile("%s.zip" % (file_path), "w") as zip_file:
	    for f in files:
	        zip_file.write(f, os.path.basename(f))
	    # write the xml
	    zip_file.write(temp_xml.name, "%s.xml" % os.path.basename(file_path))
        files.append(temp_xml.name)
    else:
        # save serialized xml to file
        with open("%s.xml" % (file_path), "w") as xml_file:
	    xml_file.write(serialize(data, name=obj_type))
	    xml_file.close()
    # publish to rabbitmq outputs queue
    connection = bungeni.core.notifications.get_mq_connection()
    if not connection:
        return
    channel = connection.channel()
    #channel.confirm_delivery()
    publish_file_path = "%s.%s" %(file_path, ("zip" if files else "xml"))
    #channel_delivery = 
    channel.basic_publish(
        exchange=SERIALIZE_OUTPUT_EXCHANGE,
        routing_key=SERIALIZE_OUTPUT_ROUTING_KEY,
        body=simplejson.dumps({"type": "file", "location": publish_file_path }),
        properties=pika.BasicProperties(content_type="text/plain",
            delivery_mode=2
        )
    )
    #if channel_delivery:
    #    log.info("Message published to exchange %s with key %s for %s" % 
    #        (SERIALIZE_OUTPUT_EXCHANGE, SERIALIZE_OUTPUT_ROUTING_KEY, publish_file_path))
    #else:
    #    log.error("Message publication failed for %r", publish_file_path)
        

    #clean up - remove any files if zip was/was not created
    if files:
        files.append("%s.%s" %(file_path, "xml"))
    else:
        files.append("%s.%s" %(file_path, "zip"))
    remove_files(files)

    # clear the cache
    PersistFiles.clear_files(root_key)