|
13 | 13 |
|
14 | 14 | import typing
|
15 | 15 |
|
| 16 | +from dataclasses import dataclass |
| 17 | + |
16 | 18 | from sqlalchemy import Column, DateTime, ForeignKey, Index, String, orm, sql
|
17 | 19 | from sqlalchemy.dialects.postgresql import JSONB, UUID
|
18 | 20 | from sqlalchemy.exc import NoResultFound
|
|
27 | 29 | from pyramid.request import Request
|
28 | 30 |
|
29 | 31 |
|
| 32 | +@dataclass |
| 33 | +class GeoIPInfo: |
| 34 | + """ |
| 35 | + Optional values passed in from upstream CDN |
| 36 | + https://github.com/pypi/infra/blob/3e57592ba91205cb5d10c588d529767b101753cd/terraform/warehouse/vcl/main.vcl#L181-L191 |
| 37 | + """ |
| 38 | + |
| 39 | + city: str | None = None |
| 40 | + continent_code: str | None = None |
| 41 | + country_code3: str | None = None |
| 42 | + country_code: str | None = None |
| 43 | + country_name: str | None = None |
| 44 | + region: str | None = None |
| 45 | + |
| 46 | + @property |
| 47 | + def _city(self) -> str: |
| 48 | + return self.city.title() if self.city else "" |
| 49 | + |
| 50 | + @property |
| 51 | + def _region(self) -> str: |
| 52 | + return self.region.upper() if self.region else "" |
| 53 | + |
| 54 | + @property |
| 55 | + def _country_code(self) -> str: |
| 56 | + return self.country_code.upper() if self.country_code else "" |
| 57 | + |
| 58 | + def display(self) -> str: |
| 59 | + """ |
| 60 | + Construct a reasonable location, depending on optional values |
| 61 | + """ |
| 62 | + if self.city and self.region and self.country_code: |
| 63 | + return f"{self._city}, {self._region}, {self._country_code}" |
| 64 | + elif self.city and self.region: |
| 65 | + return f"{self._city}, {self._region}" |
| 66 | + elif self.city and self.country_code: |
| 67 | + return f"{self._city}, {self._country_code}" |
| 68 | + elif self.region: |
| 69 | + return f"{self._region}, {self._country_code}" |
| 70 | + elif self.country_name: |
| 71 | + return self.country_name |
| 72 | + return "" |
| 73 | + |
| 74 | + |
30 | 75 | class Event(AbstractConcreteBase):
|
31 | 76 | tag = Column(String, nullable=False)
|
32 | 77 | time = Column(DateTime, nullable=False, server_default=sql.func.now())
|
@@ -99,6 +144,23 @@ def ip_address(cls, value): # noqa: N805
|
99 | 144 | session.add(_ip_address)
|
100 | 145 | cls.ip_address_obj = _ip_address
|
101 | 146 |
|
| 147 | + @property |
| 148 | + def location_info(cls) -> str: # noqa: N805 |
| 149 | + """ |
| 150 | + Determine "best" location info to display. |
| 151 | +
|
| 152 | + Dig into `.additional` for `geoip_info` and return that if it exists. |
| 153 | + It was stored at the time opf the event, and may change in the related |
| 154 | + `IpAddress` object over time. |
| 155 | + Otherwise, return the `ip_address_obj` and let its repr decide. |
| 156 | + """ |
| 157 | + if cls.additional is not None and "geoip_info" in cls.additional: |
| 158 | + g = GeoIPInfo(**cls.additional["geoip_info"]) |
| 159 | + if g.display(): |
| 160 | + return g.display() |
| 161 | + |
| 162 | + return cls.ip_address_obj |
| 163 | + |
102 | 164 | def __init_subclass__(cls, /, parent_class, **kwargs):
|
103 | 165 | cls._parent_class = parent_class
|
104 | 166 | return cls
|
|
0 commit comments