confmixin.py 4.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
"""confmixin.py

"""
from datetime import datetime
from plugin_dbui import CLEAN_SPACES


class ConfMixin(object):
    """Mixin to handle conference data.

    The parent class must have the attribute ``conference``.
    It is a dictionary with at least the following keys:

        * addresses: [{cities: [], country: str, ...}, ...]
        * cnum: str
        * control_number: int
        * closing_date: str
        * opening_date: str
        * titles: [{value: str}, ...]
        * urls: [{value: str}, ...]

    """

    def conference_country(self):
        """The country where the conference took place.

        Returns:
            str:
                * the filter *CLEAN_SPACES* is applied.
                * empty when the country is not defined.

        """
        conference = self.conference
        if conference is None:
            return ""

        val = (conference
               .get("addresses", [{}])[0]
               .get("country", ""))

        return CLEAN_SPACES(val)

    def conference_dates(self):
        """The dates of the conference.

        Returns:
            str:
                * usual pattern are ``6-5 March 2012``
                  or ``30 March - 5 April 2012``
                * empty string when not defined

        """
        conference = self.conference
        if conference is None:
            return ""

        opening = conference.get("opening_date", None)
        closing = conference.get("closing_date", None)

        if opening is None or closing is None:
            return ""

        ds = datetime.strptime(opening, "%Y-%m-%d")
        de = datetime.strptime(closing, "%Y-%m-%d")

        val = de.strftime("%d %b %Y")
        if ds.month == de.month:
            val = f"{ds.day}-{val}"
        else:
            val = f"{ds.strftime('%d %b')} - {val}"

        return val

    def conference_id(self):
        """The conference identifier used in the store.

        Returns:
            int or None

        """
        conference = self.conference
        if conference is None:
            return ""

        return conference.get("control_number", None)

    def conference_key(self):
        """The conference key used in the store.

        Returns:
            str:
                * empty string when it is not defined

        """
95 96 97
        conference = self.conference
        if conference is None:
            return ""
98

99
        return conference.get("cnum", "")
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    def conference_location(self):
        """The conference location.

        Returns:
            str:
                - the pattern is ``town, country``
                - empty string when town or country is missing
                - empty string when it is not defined

        """
        conference = self.conference
        if conference is None:
            return ""

        addresses = conference.get("addresses", [{}])[0]
        city = addresses.get("cities", [None])[0]
        country = addresses.get("country", None)

        if city is None or country is None:
            return ""

        return f"{city}, {country}"

    def conference_title(self):
        """The title of the conference.

        Returns:
            str:
                * empty string when it is not defined
                * first title is selected is there is more than one

        """
        conference = self.conference
        if conference is None:
            return ""

        val = (conference
               .get("titles", [{}])[0]
               .get("title", ""))

        return val

    def conference_town(self):
        """The town where the conference took place.

        Returns:
            str:
                * empty string when it is not defined.

        """
        conference = self.conference
        if conference is None:
            return ""

        val = (conference
               .get("addresses", [{}])[0]
               .get("cities", [""])[0])

        return val

    def conference_url(self):
        """The URL of the conference home page.

        Returns:
            str:
                * select the first URL when there is more than one
                * empty string when it is not defined

        """
        conference = self.conference
        if conference is None:
            return ""

        val = (conference
               .get("urls", [{}])[0]
               .get("value", ""))

        return val

    def conference_year(self):
        """The year of the conference.

        Returns:
            str:
                * empty string when it is not defined.

        """
        conference = self.conference
        if conference is None:
            return ""

        opening = conference.get("opening_date", None)

        return ("" if opening is None else opening[:4])