Example #1
0
File: mail.py Project: MatiasSM/fcb
 def _get_last_checked_date(self):
     mail_dst = self._mail_dst
     if not self._last_checked_date:
         value = 0.0
         with self._session_resource as session:
             try:
                 result = session \
                     .query(CheckerState.last_checked_time) \
                     .join(Destination) \
                     .filter(Destination.destination == mail_dst) \
                     .one()
                 value = float(result.last_checked_time)
             except NoResultFound:  # no last checked entry
                 destination = Destination.get_or_add(session, mail_dst)
                 session.flush()  # gen id if necessary
                 session.add(CheckerState(destinations_id=destination.id, last_checked_time=0))
             self._last_checked_date = value
     return self._last_checked_date
Example #2
0
    def _log_in_db(self, block):
        if not self._session_resource:
            self._session_resource = get_session()
        with self._session_resource as session:
            session.autoflush = False  # to avoid IntegrityError raised during testing

            sent_file_info = block.latest_file_info

            # a new container has been saved
            file_container = FilesContainer(
                sha1=sent_file_info.sha1,
                file_name=sent_file_info.basename,
                encryption_key=block.cipher_key if hasattr(block, 'cipher_key') else '',
                container_size=sent_file_info.size
            )
            session.add(file_container)
            ''' FIXME we need the container id because file_destination is not getting it
                (not working example of SQLAlchemy) '''
            session.flush()  # get container id

            # associate destinations to the container
            for destination in block.send_destinations if hasattr(block, 'send_destinations') else []:
                file_destination = FilesDestinations()
                file_destination.destination = Destination.get_or_add(session, destination)
                # FIXME according to the example in SQLAlchemy, this shouldn't be needed
                file_destination.file_containers_id = file_container.id
                if hasattr(block, 'destinations_verif_data') and destination in block.destinations_verif_data:
                    file_destination.verification_info = block.destinations_verif_data[destination]
                file_container.files_destinations.append(file_destination)

            # save/update each file in the container
            for file_info in block.content_file_infos:
                uploaded_file_fragment_number = 0
                if hasattr(file_info, 'fragment_info'):  # check if it is a fragment
                    uploaded_file_fragment_number = file_info.fragment_info.fragment_num
                    uploaded_file = \
                        self._get_uploaded_file(
                            session=session,
                            file_info=file_info.fragment_info.file_info,
                            fragment_count=file_info.fragment_info.fragments_count)

                    # save a new fragment for the file
                    file_fragment = FileFragment(
                        fragment_sha1=file_info.sha1,
                        fragment_name=file_info.upath,
                        fragment_number=file_info.fragment_info.fragment_num
                    )
                    uploaded_file.fragments.append(file_fragment)
                else:  # not fragmented file
                    uploaded_file = self._get_uploaded_file(session=session, file_info=file_info)

                session.flush()  # if uploaded_file has no id, we need one

                file_in_container_assoc = FilesInContainers(
                    uploaded_file_fragment_number=uploaded_file_fragment_number,
                    uploaded_files_id=uploaded_file.id
                )
                file_in_container_assoc.container_file = file_container
                file_container.fragments.append(file_in_container_assoc)

            session.commit()