Ejemplo n.º 1
0
Archivo: api.py Proyecto: emosyne/crate
def pendulum_to_nlprp_datetime(p: Pendulum, to_utc: bool = True) -> str:
    """
    Converts a :class:`pendulum.Pendulum` to the ISO string format (with
    timezone) used by the NLPRP.
    """
    if to_utc:
        p = convert_datetime_to_utc(p)
    return p.isoformat()
Ejemplo n.º 2
0
    def set_era(self, iso_datetime: str) -> None:
        from cardinal_pythonlib.datetimefunc import (
            convert_datetime_to_utc,
            format_datetime,
        )
        from camcops_server.cc_modules.cc_constants import DateFormat

        self.era_time = pendulum.parse(iso_datetime)
        self.era_time_utc = convert_datetime_to_utc(self.era_time)
        self.era = format_datetime(self.era_time, DateFormat.ISO8601)
Ejemplo n.º 3
0
    def __init__(self, cfg: "CamcopsConfig") -> None:
        super().__init__()
        engine = cfg.get_sqla_engine()
        self.dbsession = sessionmaker()(bind=engine)  # type: SqlASession

        self.era_time = pendulum.now()
        self.era_time_utc = convert_datetime_to_utc(self.era_time)
        self.era = format_datetime(self.era_time, DateFormat.ISO8601)

        self.group = None  # type: Optional[Group]
        self.user = None  # type: Optional[User]
        self.device = None  # type: Optional[Device]
        self.nhs_iddef = None  # type: Optional[IdNumDefinition]
Ejemplo n.º 4
0
    def __init__(self,
                 from_addr: str,
                 date: str = None,
                 sender: str = "",
                 reply_to: str = "",
                 to: str = "",
                 cc: str = "",
                 bcc: str = "",
                 subject: str = "",
                 body: str = "",
                 content_type: str = "text/plain",
                 charset: str = "utf8",
                 attachment_filenames: Sequence[str] = None,
                 attachments_binary: Sequence[Tuple[str, bytes]] = None,
                 save_msg_string: bool = False) -> None:
        """
        Args:
            from_addr: name of the sender for the "From:" field
            date: e-mail date in RFC 2822 format, or ``None`` for "now"
            sender: name of the sender for the "Sender:" field
            reply_to: name of the sender for the "Reply-To:" field

            to: e-mail address(es) of the recipients for "To:" field, as a
                CSV list
            cc: e-mail address(es) of the recipients for "Cc:" field, as a
                CSV list
            bcc: e-mail address(es) of the recipients for "Bcc:" field, as a
                CSV list

            subject: e-mail subject
            body: e-mail body
            content_type: MIME type for body content, default ``text/plain``
            charset: character set for body; default ``utf8``
            charset:

            attachment_filenames: filenames of attachments to add
            attachments_binary: binary attachments to add, as a list of
                ``filename, bytes`` tuples

            save_msg_string: save the encoded message string? (May take
                significant space in the database).
        """

        # ---------------------------------------------------------------------
        # Timestamp
        # ---------------------------------------------------------------------
        now_local = get_now_localtz_pendulum()
        self.created_at_utc = convert_datetime_to_utc(now_local)

        # -------------------------------------------------------------------------
        # Arguments
        # -------------------------------------------------------------------------
        if not date:
            date = email.utils.format_datetime(now_local)
        attachment_filenames = attachment_filenames or []  # type: List[str]
        attachments_binary = attachments_binary or [
        ]  # type: List[Tuple[str, bytes]]  # noqa
        attachment_binary_filenames, attachment_binaries = zip(
            *attachments_binary)  # noqa
        # ... https://stackoverflow.com/questions/13635032/what-is-the-inverse-function-of-zip-in-python  # noqa
        # Other checks performed by our e-mail function below

        # ---------------------------------------------------------------------
        # Transient fields
        # ---------------------------------------------------------------------
        self.password = None
        self.msg = make_email(
            from_addr=from_addr,
            date=date,
            sender=sender,
            reply_to=reply_to,
            to=to,
            cc=cc,
            bcc=bcc,
            subject=subject,
            body=body,
            content_type=content_type,
            attachment_filenames=attachment_filenames,
            attachment_binaries=attachment_binaries,
            attachment_binary_filenames=attachment_binary_filenames,
        )

        # ---------------------------------------------------------------------
        # Database fields
        # ---------------------------------------------------------------------
        self.date = date
        self.from_addr = from_addr
        self.sender = sender
        self.reply_to = reply_to
        self.to = to
        self.cc = cc
        self.bcc = bcc
        self.subject = subject
        self.body = body
        self.content_type = content_type
        self.charset = charset
        if save_msg_string:
            self.msg_string = self.msg.as_string()
Ejemplo n.º 5
0
 def end_datetime_utc(self) -> Optional[Pendulum]:
     if not self.end_datetime:
         return None
     return convert_datetime_to_utc(self.end_datetime)
Ejemplo n.º 6
0
    def setUp(self) -> None:
        super().setUp()
        from cardinal_pythonlib.datetimefunc import (
            convert_datetime_to_utc,
            format_datetime,
        )
        from camcops_server.cc_modules.cc_blob import Blob
        from camcops_server.cc_modules.cc_constants import DateFormat
        from camcops_server.cc_modules.cc_device import Device
        from camcops_server.cc_modules.cc_group import Group
        from camcops_server.cc_modules.cc_patient import Patient
        from camcops_server.cc_modules.cc_patientidnum import PatientIdNum
        from camcops_server.cc_modules.cc_task import Task
        from camcops_server.cc_modules.cc_user import User
        from camcops_server.tasks.photo import Photo

        Base.metadata.create_all(self.engine)

        self.era_time = pendulum.parse("2010-07-07T13:40+0100")
        self.era_time_utc = convert_datetime_to_utc(self.era_time)
        self.era = format_datetime(self.era_time, DateFormat.ISO8601)

        # Set up groups, users, etc.
        # ... ID number definitions
        iddef1 = IdNumDefinition(which_idnum=1,
                                 description="NHS number",
                                 short_description="NHS#",
                                 hl7_assigning_authority="NHS",
                                 hl7_id_type="NHSN")
        self.dbsession.add(iddef1)
        iddef2 = IdNumDefinition(which_idnum=2,
                                 description="RiO number",
                                 short_description="RiO",
                                 hl7_assigning_authority="CPFT",
                                 hl7_id_type="CPFT_RiO")
        self.dbsession.add(iddef2)
        # ... group
        self.group = Group()
        self.group.name = "testgroup"
        self.group.description = "Test group"
        self.group.upload_policy = "sex AND anyidnum"
        self.group.finalize_policy = "sex AND idnum1"
        self.dbsession.add(self.group)
        self.dbsession.flush()  # sets PK fields

        # ... users

        self.user = User.get_system_user(self.dbsession)
        self.user.upload_group_id = self.group.id
        self.req._debugging_user = self.user  # improve our debugging user

        # ... devices
        self.server_device = Device.get_server_device(self.dbsession)
        self.other_device = Device()
        self.other_device.name = "other_device"
        self.other_device.friendly_name = "Test device that may upload"
        self.other_device.registered_by_user = self.user
        self.other_device.when_registered_utc = self.era_time_utc
        self.other_device.camcops_version = CAMCOPS_SERVER_VERSION
        self.dbsession.add(self.other_device)

        self.dbsession.flush()  # sets PK fields

        # Populate database with two of everything
        p1 = Patient()
        p1.id = 1
        self._apply_standard_db_fields(p1)
        p1.forename = "Forename1"
        p1.surname = "Surname1"
        p1.dob = pendulum.parse("1950-01-01")
        self.dbsession.add(p1)
        p1_idnum1 = PatientIdNum()
        p1_idnum1.id = 1
        self._apply_standard_db_fields(p1_idnum1)
        p1_idnum1.patient_id = p1.id
        p1_idnum1.which_idnum = iddef1.which_idnum
        p1_idnum1.idnum_value = 333
        self.dbsession.add(p1_idnum1)
        p1_idnum2 = PatientIdNum()
        p1_idnum2.id = 2
        self._apply_standard_db_fields(p1_idnum2)
        p1_idnum2.patient_id = p1.id
        p1_idnum2.which_idnum = iddef2.which_idnum
        p1_idnum2.idnum_value = 444
        self.dbsession.add(p1_idnum2)

        p2 = Patient()
        p2.id = 2
        self._apply_standard_db_fields(p2)
        p2.forename = "Forename2"
        p2.surname = "Surname2"
        p2.dob = pendulum.parse("1975-12-12")
        self.dbsession.add(p2)
        p2_idnum1 = PatientIdNum()
        p2_idnum1.id = 3
        self._apply_standard_db_fields(p2_idnum1)
        p2_idnum1.patient_id = p2.id
        p2_idnum1.which_idnum = iddef1.which_idnum
        p2_idnum1.idnum_value = 555
        self.dbsession.add(p2_idnum1)

        self.dbsession.flush()

        for cls in Task.all_subclasses_by_tablename():
            t1 = cls()
            t1.id = 1
            self._apply_standard_task_fields(t1)
            if t1.has_patient:
                t1.patient_id = p1.id

            if isinstance(t1, Photo):
                b = Blob()
                b.id = 1
                self._apply_standard_db_fields(b)
                b.tablename = t1.tablename
                b.tablepk = t1.id
                b.fieldname = 'photo_blobid'
                b.filename = "some_picture.png"
                b.mimetype = MimeType.PNG
                b.image_rotation_deg_cw = 0
                b.theblob = DEMO_PNG_BYTES
                self.dbsession.add(b)

                t1.photo_blobid = b.id

            self.dbsession.add(t1)

            t2 = cls()
            t2.id = 2
            self._apply_standard_task_fields(t2)
            if t2.has_patient:
                t2.patient_id = p2.id
            self.dbsession.add(t2)

        self.dbsession.commit()