Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Basic extention of `django_countries`:

```
In [1]: from django_countries.fields import Country

In [2]: Country("BE").subregion
Out[2]: 'QO'

In [3]: Country("BE").region
Out[3]: 'XE'
```

Ability to get countries by region and subregion:

```
In [7]: from django_countries_regions import regions

In [8]: regions.get_countries_by_subregion("QN")
Out[8]:
['AL',
 'AD',
 'BA',
 'HR',
 'GI',
 'GR',
 'VA',
 'IT',
 'MT',
 'ME',
 'MK',
 'PT',
 'SM',
 'RS',
 'SI',
 'ES']
```
  • Loading branch information
garrettc committed Jul 31, 2024
1 parent 9f0701d commit 55d14a8
Show file tree
Hide file tree
Showing 11 changed files with 2,342 additions and 35 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Publish Python Package

on:
release:
types: [created]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: |
pip install '.[test]'
- name: Run tests
run: |
pytest
deploy:
runs-on: ubuntu-latest
needs: [test]
environment: release
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: |
pip install setuptools wheel build
- name: Build
run: |
python -m build
- name: Publish
uses: pypa/gh-action-pypi-publish@release/v1
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: |
pip install '.[test]'
- name: Run tests
run: |
pytest
35 changes: 1 addition & 34 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,6 @@ target/
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

Expand Down Expand Up @@ -155,8 +126,4 @@ dmypy.json
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ repos:
rev: "v0.3.5"
hooks:
- id: ruff

1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# django-countries-regions

[![PyPI](https://img.shields.io/pypi/v/django-countries-regions.svg)](https://pypi.org/project/django-countries-regions/)
[![Tests](https://github.com/InternetSociety/django-countries-regions/actions/workflows/test.yml/badge.svg)](https://github.com/InternetSociety/django-countries-regions/actions/workflows/test.yml)
[![Changelog](https://img.shields.io/github/v/release/InternetSociety/django-countries-regions?include_prereleases&label=changelog)](https://github.com/InternetSociety/django-countries-regions/releases)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/InternetSociety/django-countries-regions/blob/main/LICENSE)

Adds region and subregion data to django-countries

## Installation

Install this library using `pip`:
```bash
pip install django-countries-regions
```
## Usage

Usage instructions go here.

## Development

To contribute to this library, first checkout the code. Then create a new virtual environment:
```bash
cd django-countries-regions
python -m venv venv
source venv/bin/activate
```
Now install the dependencies and test dependencies:
```bash
pip install -e '.[test]'
```
To run the tests:
```bash
pytest
```
41 changes: 41 additions & 0 deletions django_countries_regions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django_countries_regions.country_regions import COUNTRY_REGIONS
from django_countries_regions.regions import REGIONS, SUBREGIONS


# TODO: Would returning `(region_code, region_name)` be more useful?
def get_country_region(country_code, region=True):
code_to_use = "iso_region_alpha2_code" if region else "iso_subregion_alpha2_code"
return COUNTRY_REGIONS[country_code][code_to_use]


def get_country_subregion(country_code):
return get_country_region(country_code, region=False)


class Regions():
"""
An object that can query an ISO list of geographical regions and subregions and return a list of countries in
that region.
"""

def get_countries_by_region(self, region_code, region=True):
if region:
return REGIONS[region_code]["countries"]
else:
return SUBREGIONS[region_code]["countries"]

def get_countries_by_subregion(self, region_code):
return self.get_countries_by_region(region_code, region=False)

def get_region_name(self, region_code):
if region_code:
return REGIONS[region_code]["name"]
return None

def get_subregion_name(self, region_code):
if region_code:
return SUBREGIONS[region_code]["name"]
return None


regions = Regions()
Loading

0 comments on commit 55d14a8

Please sign in to comment.