Skip to content

Commit

Permalink
Merge pull request #51 from shimakaze-git/master
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
shimakaze-git authored Feb 9, 2022
2 parents 94869a5 + f7bcbc2 commit 7e6e38f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 41 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
echo ${{ env.REPOSITORY }}
echo ${{ env.ACTOR }}
rm HISTORY.md
github_changelog_generator -u ${{ env.ACTOR }} \
-p ${{ env.REPOSITORY }} \
--header-label '# HISTORY' \
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Authored by [shimakaze_soft](https://github.com/shimakaze-git) and some great
- Get all user profiles which have their birthday today
- order the user profiles according to their birthday

===
-----

- Converting Birthdays to Japanese Style
- Calculate age based on birthday
Expand Down Expand Up @@ -59,22 +59,35 @@ class ModelsTest(BirthdayModel):
ordering = ('pk',)
```

Get all user profiles within the next 30 days:
## Get all user profiles within the next 30 days

```Python
ModelsTest.objects.get_upcoming_birthdays()
# ["2001-01-01", "2000-01-02", "2002-12-31"]

jan1 = date(year=2010, month=1, day=1)
birthdays = ModelsTest.objects.get_upcoming_birthdays(after=jan1)
# ["2001-01-01", "2000-01-02"]
```

Get all user profiles which have their birthday today:
## Get all user profiles which have their birthday today

```Python
ModelsTest.objects.get_birthdays()
# ["2001-01-01", "2000-01-02", "2002-12-31", "1990-03-01"]

jan1 = date(year=2010, month=1, day=1)
birthdays = ModelsTest.objects.get_birthdays(jan1)

# ["2001-01-01", "1990-01-01"]
```

Or order the user profiles according to their birthday:
## Order the user profiles according to their birthday

```Python
ModelsTest.objects.order_by_birthday()
# ["2001-01-01", "2000-01-02", "2002-12-31", "1990-03-01"]

jan1 = date(year=2010, month=1, day=1)
birthdays = ModelsTest.objects.order_by_birthday()
# ["2001-01-01", "2000-01-02", "1990-03-01", "2002-12-31"]
```

# Docs
Expand Down
2 changes: 1 addition & 1 deletion jp_birthday/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """shimakaze-git"""
__email__ = "shimakaze.soft+github@googlemail.com"
__version__ = "0.4.0"
__version__ = "0.6.0"

__copyright__ = "Copyright (C) 2020 shimakaze-git"
__license__ = "MIT"
Expand Down
58 changes: 40 additions & 18 deletions jp_birthday/eras.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import datetime
import json
import unicodedata
Expand Down Expand Up @@ -47,6 +48,25 @@ def get_date_range_from_jp_era(self, jp_era: str) -> dict:
"""
return self.jtw._get_date_range_from_jp_era(jp_era)

def get_jp_era_range(self, birthday: datetime.date) -> int:
"""
get_jp_era_range
Args:
birthday (datetime.date): [description]
Returns:
int: [description]
"""
jp_era_birthday = self.convert_to_jp_era(birthday)

era = jp_era_birthday["era_kanji"]
data = self.get_date_range_from_jp_era(era)

max = data["max"]

return max

# def convert(
# self,
# year=datetime.date.today().year,
Expand All @@ -56,29 +76,29 @@ def get_date_range_from_jp_era(self, jp_era: str) -> dict:
# ):
# pass

def __pre_process(self, str_arg):
"""[summary]
# def __pre_process(self, str_arg):
# """[summary]

Args:
str_arg ([type]): [description]
"""
pass
# Args:
# str_arg ([type]): [description]
# """
# pass

def __is_correct_format(self, str_arg):
"""[summary]
# def __is_correct_format(self, str_arg):
# """[summary]

Args:
str_arg ([type]): [description]
"""
pass
# Args:
# str_arg ([type]): [description]
# """
# pass

def __is_correct_era(self, str_arg):
"""[summary]
# def __is_correct_era(self, str_arg):
# """[summary]

Args:
str_arg ([type]): [description]
"""
pass
# Args:
# str_arg ([type]): [description]
# """
# pass


class WestToJapanEra:
Expand Down Expand Up @@ -117,10 +137,12 @@ def _convert_to_jp_era(self, year: int, month: int, day: int) -> dict:

era_en = reading["en"]
era_en_short = era_en[0]
era_kanji = era

return {
"era": era_en,
"era_short": era_en_short,
"era_kanji": era_kanji,
"year": int(era_year),
"month": int(month),
"day": int(day),
Expand Down
41 changes: 26 additions & 15 deletions jp_birthday/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class BaseBirthdayModel(models.Model):
objects = JpBirthdayManager()
birthday = BirthdayField()

_era = JapanEra()

class Meta:
abstract = True

Expand Down Expand Up @@ -56,10 +58,8 @@ def _get_jp_era_birthday(self, birthday: datetime.date) -> dict:
dict: [description]
"""

# print("get_jp_era", BaseBirthdayModel.objects)

era = JapanEra()
return era.convert_to_jp_era(birthday)
# era = JapanEra()
return self._era.convert_to_jp_era(birthday)

def get_wareki_birthday(self, dict_type=False) -> object:
"""get wareki birthday
Expand All @@ -81,6 +81,28 @@ def get_wareki_birthday(self, dict_type=False) -> object:
wareki_birthday = wareki + "-" + year + "-" + month + "-" + day
return wareki_birthday

def get_jp_era_range(self) -> int:
"""
元号がどれくらい続いたかの年数を表示
Returns:
int: [description]
"""
birthday = self.birthday
year = self._era.get_jp_era_range(birthday)
return year


class BirthdayModel(BaseBirthdayModel):
"""BirthdayModel"""

class Meta:
abstract = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# super(BirthdayModel, self).__init__(*args, **kwargs)

def get_age(self) -> int:
"""get age from birthday.
Expand Down Expand Up @@ -117,14 +139,3 @@ def get_zodiac(self) -> str:
zodiac = zodiacs[num_zodiac]

return zodiac


class BirthdayModel(BaseBirthdayModel):
"""BirthdayModel"""

class Meta:
abstract = True

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# super(BirthdayModel, self).__init__(*args, **kwargs)
28 changes: 28 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ def get_zodiac(year: int):

self.assertEqual(zodiac, get_zodiac(year))

def test_get_jp_era_range(self):
# 平成
heisei_birthdays = [
datetime.strptime(b, "%Y-%m-%d").date() for b in self.heisei
]

model_tests = ModelTest.objects.filter(birthday__in=heisei_birthdays)
for m in model_tests:
year = m.get_jp_era_range()
self.assertEqual(year, 31)

# 昭和
showa_birthdays = [datetime.strptime(b, "%Y-%m-%d").date() for b in self.showa]

model_tests = ModelTest.objects.filter(birthday__in=showa_birthdays)
for m in model_tests:
year = m.get_jp_era_range()
print("year", year)
self.assertEqual(year, 64)

# 明治
meiji_birthdays = [datetime.strptime(b, "%Y-%m-%d").date() for b in self.meiji]

model_tests = ModelTest.objects.filter(birthday__in=meiji_birthdays)
for m in model_tests:
year = m.get_jp_era_range()
self.assertEqual(year, 45)

@classmethod
def teardown_class(self):
"""テストclass実行の後処理"""
Expand Down

0 comments on commit 7e6e38f

Please sign in to comment.