def mime_type(request): url = request.params.get('url', None) if not url: raise HTTPBadRequest("Missing 'url' parameter") parsed = urlparse(url) if not parsed or parsed.scheme not in ('http', 'https'): raise HTTPBadRequest("Wrong scheme") if parsed.netloc.split(":")[0] == config.get('public_hostname'): # is it one of our own documents? # If so, detect it and shortcut to avoid the pyramid handler calling # another pyramid handler, as this exhausts pyramid threads rapidly # and can deadlock the whole application r = re.match( r'^https?://[\w\.]+(?:\:\d+)?/data/.*/documents/(\d+)/data(?:\?.*)?$', url) if r: document_id = r.groups(0)[0] from sqlalchemy.sql.functions import func mimetype, create_date, size = File.default_db.query( File.mime_type, File.creation_date, func.length(File.data) ).filter_by(id=int(document_id)).first() return Response( body=None, content_type=str(mimetype), content_length=size, last_modified=create_date) try: result = requests.head(url, timeout=15) except requests.ConnectionError: return Response( status=503, location=url) return Response( content_type=result.headers.get('Content-Type', None), status=result.status_code, location=result.url)
def get_latest_versions(branch: str = "Stable") -> result: """ SELECT latest.* FROM devices, ( SELECT all_latest.* FROM (SELECT codename, version, android from updates WHERE updates.branch like "Stable%" AND updates.type = "Full" ORDER BY updates.date DESC LIMIT 99999) as all_latest GROUP BY all_latest.codename) as latest WHERE latest.codename = devices.codename AND devices.eol = 0 AND devices.miui_code != "" AND LENGTH(devices.miui_code) = 4 """ all_latest = session.query( Update.codename, Update.version, Update.android).filter(Update.branch.startswith(branch)).filter( Update.type == "Full").order_by( Update.date.desc()).limit(99999).subquery() latest = session.query(all_latest).group_by( all_latest.c.codename).subquery() updates = session.query(latest).filter( latest.c.codename == Device.codename).filter( Device.eol == '0').filter(Device.miui_code != "").filter( func.length(Device.miui_code) == 4).all() return updates
def get_device_roms(codename) -> result: """ SELECT CONCAT(devices.name, ' ', devices.region) as name, all_updates.* FROM devices, ( SELECT codename, version, android, branch, method, size, md5, link, changelog, date from updates WHERE codename like 'whyred%' AND (updates.branch like "Stable%" OR updates.branch = "Weekly") AND updates.type = "Full" ORDER BY updates.date DESC LIMIT 99999) as all_updates WHERE devices.codename = all_updates.codename AND LENGTH(devices.miui_code) = 4 """ all_updates = session.query( Update.codename, Update.version, Update.android, Update.branch, Update.method, Update.size, Update.md5, Update.link, Update.changelog, Update.date).filter(Update.codename.startswith(codename)).filter( or_(Update.branch.startswith("Stable"), Update.branch == "Weekly")).filter( Update.type == "Full").order_by( Update.date.desc()).limit(99999).subquery() updates = session.query( concat(Device.name, ' ', Device.region).label('name'), all_updates).filter(Device.codename == all_updates.c.codename).filter( func.length(Device.miui_code) == 4).all() return updates
def get_latest_updates(branch: str = "Stable") -> result: """ SELECT CONCAT(devices.name, ' ', devices.region) as name, latest.* FROM devices, ( SELECT all_latest.* FROM (SELECT codename, version, branch, method, size, md5, link, changelog, date from updates WHERE updates.branch = "Stable" AND updates.type = "Full" ORDER BY updates.date DESC LIMIT 99999) as all_latest GROUP BY all_latest.codename, all_latest.method) as latest WHERE latest.codename = devices.codename AND devices.miui_code != "" AND LENGTH(devices.miui_code) = 4 ORDER BY date desc """ all_latest = session.query( Update.codename, Update.version, Update.android, Update.branch, Update.method, Update.size, Update.md5, Update.link, Update.changelog, Update.date).filter(Update.branch == branch).filter( Update.type == "Full").order_by( Update.date.desc()).limit(99999).subquery() latest = session.query(all_latest).group_by( all_latest.c.codename).group_by(all_latest.c.method).subquery() updates = session.query( Device.name, concat(Device.name, ' ', Device.region).label('fullname'), latest).filter(latest.c.codename == Device.codename).filter( Device.miui_code != "").filter( func.length(Device.miui_code) == 4).order_by( latest.c.date.desc()).all() return updates
def get_current_devices() -> result: """ SELECT codename from devices WHERE eol = 0 AND miui_code != "" AND LENGTH(miui_code) = 4 :return: list of codenames """ return session.query(Device.codename).filter( Device.eol == "0").filter(Device.miui_code != "").filter( func.length(Device.miui_code) == 4).all()
def get_devices() -> result: """ SELECT codename, CONCAT(name, ' ', region) as name, miui_name from devices WHERE miui_code != "" AND LENGTH(miui_code) = 4 GROUP BY codename ORDER BY codename """ return session.query( Device.codename, concat(Device.name, ' ', Device.region).label('name'), Device.miui_name).filter(Device.miui_code != "").filter( func.length(Device.miui_code) == 4).order_by( Device.codename).all()
def test_iso_datetime_field(self) -> None: id_colname = "id" dt_local_colname = "dt_local" dt_utc_colname = "dt_utc" iso_colname = "iso" id_col = Column(id_colname, Integer, primary_key=True) dt_local_col = Column(dt_local_colname, DateTime) dt_utc_col = Column(dt_utc_colname, DateTime) iso_col = Column(iso_colname, PendulumDateTimeAsIsoTextColType) table = Table("testtable", self.meta, id_col, dt_local_col, dt_utc_col, iso_col) table.create() now = Pendulum.now() now_utc = now.in_tz(pendulum.UTC) yesterday = now.subtract(days=1) yesterday_utc = yesterday.in_tz(pendulum.UTC) table.insert().values([ { id_colname: 1, dt_local_colname: now, dt_utc_colname: now_utc, iso_colname: now, }, { id_colname: 2, dt_local_colname: yesterday, dt_utc_colname: yesterday_utc, iso_colname: yesterday, }, ]).execute() select_fields = [ id_col, dt_local_col, dt_utc_col, iso_col, func.length(dt_local_col).label("len_dt_local_col"), func.length(dt_utc_col).label("len_dt_utc_col"), func.length(iso_col).label("len_iso_col"), isotzdatetime_to_utcdatetime(iso_col).label("iso_to_utcdt"), unknown_field_to_utcdatetime(dt_utc_col).label( "uk_utcdt_to_utcdt"), unknown_field_to_utcdatetime(iso_col).label("uk_iso_to_utc_dt"), ] rows = list( select(select_fields).select_from(table).order_by( id_col).execute()) self._assert_dt_equal(rows[0][dt_local_col], now) self._assert_dt_equal(rows[0][dt_utc_col], now_utc) self._assert_dt_equal(rows[0][iso_colname], now) self._assert_dt_equal(rows[0]["iso_to_utcdt"], now_utc) self._assert_dt_equal(rows[0]["uk_utcdt_to_utcdt"], now_utc) self._assert_dt_equal(rows[0]["uk_iso_to_utc_dt"], now_utc) self._assert_dt_equal(rows[1][dt_local_col], yesterday) self._assert_dt_equal(rows[1][dt_utc_col], yesterday_utc) self._assert_dt_equal(rows[1][iso_colname], yesterday) self._assert_dt_equal(rows[1]["iso_to_utcdt"], yesterday_utc) self._assert_dt_equal(rows[1]["uk_utcdt_to_utcdt"], yesterday_utc) self._assert_dt_equal(rows[1]["uk_iso_to_utc_dt"], yesterday_utc)