Skip to content

Commit

Permalink
<REFACTOR> 최종 main 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dayeonkimm committed Aug 12, 2024
1 parent 4b59ebc commit ae196e1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 86 deletions.
9 changes: 2 additions & 7 deletions potato_project/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@

DEFAULT_CHARSET = "utf-8"

DEFAULT_CHARSET = "utf-8"

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
Expand All @@ -236,8 +234,8 @@
# CORS_ORIGIN_WHITELIST = ['http://localhost:5173', 'http://127.0.0.1:5173', 'https://www.gitpotatoes.com',] # 특정 Origin만 허용
CORS_ALLOWED_ORIGINS = [
"https://www.gitpotatoes.com", # 실제 배포 프론트엔드 URL
"http://localhost:5173", # 프론트엔드 로컬 서버 URL
"http://127.0.0.1:5173", # 프론트엔드 로컬 서버 URL
# 'http://localhost:5173', # 프론트엔드 로컬 서버 URL
# 'http://127.0.0.1:5173', # 프론트엔드 로컬 서버 URL
]
CORS_ALLOW_CREDENTIALS = True # 쿠키 등 credential 정보 허용
CORS_ALLOW_METHODS = [
Expand All @@ -260,6 +258,3 @@
"x-requested-with",
]
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
2 changes: 1 addition & 1 deletion potato_project/githubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Github(TimeStampedModel):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="User_id")
commit_num = models.BigIntegerField(verbose_name="Commit Number")
date = models.DateField(default=timezone.now)
date = models.DateField()

def __str__(self):
return f"{self.date}-{self.commit_num}"
170 changes: 99 additions & 71 deletions potato_project/githubs/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@


@receiver(post_save, sender=Github)
def get_winter_potato(sender, instance, created, **kwargs):
# 새 Github 데이터가 생성 or 업데이트, 날짜가 크리스마스이며, commit_num이 1 이상인 경우에만 실행
def get_winter_potato(sender, instance, **kwargs):
# 새 Github 데이터가 생성 or 업데이트, 날짜가 크리스마스이며, commit_num이 >
if (
created
and instance.commit_num >= 1
instance.commit_num >= 1
and instance.date.month == 12
and instance.date.day == 25
): # 월과 일만 비교
Expand All @@ -27,10 +26,9 @@ def get_winter_potato(sender, instance, created, **kwargs):


@receiver(post_save, sender=Github)
def get_ghost_potato(sender, instance, created, **kwargs):
def get_ghost_potato(sender, instance, **kwargs):
if (
created
and instance.commit_num >= 1
instance.commit_num >= 1
and instance.date.month == 10
and instance.date.day == 31
):
Expand All @@ -44,79 +42,109 @@ def get_ghost_potato(sender, instance, created, **kwargs):


@receiver(post_save, sender=Github)
def get_crystal_potato(sender, instance, created, **kwargs):
if created and instance.commit_num >= 1:
def get_crystal_potato(sender, instance, **kwargs):
if instance.commit_num >= 1:
# 30일 전 날짜 계산
thirty_days_ago = instance.date.date() - timedelta(days=30)
thirty_days_ago = instance.date - timedelta(days=30)

# 30일 연속 커밋 여부 확인
commits_in_30_days = (
Github.objects.filter(
user=instance.user,
date__gte=thirty_days_ago,
date__lte=instance.date.date(),
commit_num__gte=1,
)
.values("date")
.distinct()
.count()
# 30일치 데이터가 있는지 확인
oldest_record = (
Github.objects.filter(user=instance.user).order_by("date").first()
)
if oldest_record and (instance.date - oldest_record.date).days >= 30:
# 30일 연속 커밋 여부 확인
commits_in_30_days = (
Github.objects.filter(
user=instance.user,
date__gte=thirty_days_ago,
date__lte=instance.date,
commit_num__gte=1,
)
.values("date")
.distinct()
.count()
)

if commits_in_30_days == 30:
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=8)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass
if commits_in_30_days == 30:
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=8)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass
else:
# 30일치 데이터가 없는 경우 로그 남기기 또는 다른 처리
print(
f"Not enough data for user {instance.user.id}. Oldest record date: {oldest_record.date if oldest_record else 'No records'}"
)


@receiver(post_save, sender=Github)
def get_dirty_potato(sender, instance, created, **kwargs):
if created:
def get_dirty_potato(sender, instance, **kwargs):
if instance.commit_num == 0:
# 30일 전 날짜 계산
thirty_days_ago = instance.date.date() - timedelta(days=30)

# 30일 동안 커밋이 있었는지 확인
any_commits_in_30_days = Github.objects.filter(
user=instance.user,
date__gte=thirty_days_ago,
date__lte=instance.date.date(),
commit_num__gte=1,
).exists()

if not any_commits_in_30_days:
# 30일 연속 커밋이 없는 경우 감자 아이디 9 획득 로직 실행
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=9)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass # 필요에 따라 에러 처리 추가
thirty_days_ago = instance.date - timedelta(days=30)

# 30일치 데이터가 있는지 확인
oldest_record = (
Github.objects.filter(user=instance.user).order_by("date").first()
)
if oldest_record and (instance.date - oldest_record.date).days >= 30:
# 30일 동안 커밋이 있었는지 확인
any_commits_in_30_days = Github.objects.filter(
user=instance.user,
date__gte=thirty_days_ago,
date__lte=instance.date,
commit_num__gte=1,
).exists()

if not any_commits_in_30_days:
# 30일 연속 커밋이 없는 경우 감자 아이디 9 획득 로직 실행
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=9)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass # 필요에 따라 에러 처리 추가
else:
# 30일치 데이터가 없는 경우 로그 남기기 또는 다른 처리
print(
f"Not enough data for user {instance.user.id}. Oldest record date: {oldest_record.date if oldest_record else 'No records'}"
)


@receiver(post_save, sender=Github)
def get_green_potato(sender, instance, created, **kwargs):
if created:
def get_green_potato(sender, instance, **kwargs):
if instance.commit_num == 0:
# 90일 전 날짜 계산
ninety_days_ago = instance.date.date() - timedelta(days=90)

# 90일 동안 커밋이 있었는지 확인
any_commits_in_90_days = Github.objects.filter(
user=instance.user,
date__gte=ninety_days_ago,
date__lte=instance.date.date(),
commit_num__gte=1,
).exists()

if not any_commits_in_90_days:
# 90일 연속 커밋이 없는 경우 감자 아이디 10 획득 로직 실행
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=10)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass # 필요에 따라 에러 처리 추가
ninety_days_ago = instance.date - timedelta(days=90)

# 90일치 데이터가 있는지 확인
oldest_record = (
Github.objects.filter(user=instance.user).order_by("date").first()
)
if oldest_record and (instance.date - oldest_record.date).days >= 90:
# 90일 동안 커밋이 있었는지 확인
any_commits_in_90_days = Github.objects.filter(
user=instance.user,
date__gte=ninety_days_ago,
date__lte=instance.date,
commit_num__gte=1,
).exists()

if not any_commits_in_90_days:
# 90일 연속 커밋이 없는 경우 감자 아이디 10 획득 로직 실행
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=10)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass # 필요에 따라 에러 처리 추가
else:
# 90일치 데이터가 없는 경우 로그 남기기 또는 다른 처리
print(
f"Not enough data for user {instance.user.id}. Oldest record date: {oldest_record.date if oldest_record else 'No records'}"
)
16 changes: 13 additions & 3 deletions potato_project/githubs/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, timedelta

import pytz
import requests
from django.db.models import Avg, Sum
from django.http import JsonResponse
Expand Down Expand Up @@ -92,9 +93,17 @@ def get(self, request):
)
if commits is not None:
for commit in commits:
commit_date = datetime.strptime(
commit["commit"]["author"]["date"], "%Y-%m-%dT%H:%M:%SZ"
).date()
commit_date = (
timezone.make_aware(
datetime.strptime(
commit["commit"]["author"]["date"],
"%Y-%m-%dT%H:%M:%SZ",
),
timezone=pytz.UTC,
)
.astimezone(timezone.get_current_timezone())
.date()
)
if commit_date == today:
today_commits += 1

Expand All @@ -121,6 +130,7 @@ def get(self, request):

# 경험치 및 레벨 업데이트
user.potato_exp = total_commits
user.potato_level = 1
level_up_threshold = int(50 * 1.5 ** (user.potato_level - 1))
while user.potato_exp >= level_up_threshold:
user.potato_level += 1
Expand Down
1 change: 0 additions & 1 deletion potato_project/potatoes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .serializers import PotatoSerializer


# 유저의 포테이토가 다가지고있지, 획득을 못햇던 햇던, 유저가감자를 획득했는지, 대표이미지 설정했는지.
# 유저의 감자 조회
class MyPotatoDetail(APIView):
permission_classes = [IsAuthenticated]
Expand Down
13 changes: 10 additions & 3 deletions potato_project/users/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def get_level_two_potato(sender, instance, **kwargs):
# 레벨이 2로 변경되었고, 이전 레벨이 2가 아닐 때만 실행
if (
instance.potato_level == 2
and instance.tracker.has_changed('potato_level')
and instance.tracker.previous("potato_level") != 2
and not kwargs.get("created", False)
):
try:
try:
# potato_type_id=2인 감자 조회
potato = Potato.objects.get(user=instance.user, potato_type_id=2)
if not potato.is_acquired:
Expand All @@ -23,10 +24,12 @@ def get_level_two_potato(sender, instance, **kwargs):
pass



@receiver(post_save, sender=User)
def get_level_three_potato(sender, instance, **kwargs):
if (
instance.potato_level == 3
and instance.tracker.has_changed('potato_level')
and instance.tracker.previous("potato_level") != 3
and not kwargs.get("created", False)
):
Expand All @@ -44,9 +47,10 @@ def get_level_four_potato(sender, instance, **kwargs):
if (
instance.potato_level == 4
and instance.tracker.previous("potato_level") != 4
and instance.tracker.has_changed('potato_level')
and not kwargs.get("created", False)
):
try:
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=4)
if not potato.is_acquired:
potato.is_acquired = True
Expand All @@ -55,17 +59,20 @@ def get_level_four_potato(sender, instance, **kwargs):
pass



@receiver(post_save, sender=User)
def get_level_five_potato(sender, instance, **kwargs):
if (
instance.potato_level == 5
and instance.tracker.has_changed('potato_level')
and instance.tracker.previous("potato_level") != 5
and not kwargs.get("created", False)
):
try:
try:
potato = Potato.objects.get(user=instance.user, potato_type_id=5)
if not potato.is_acquired:
potato.is_acquired = True
potato.save()
except Potato.DoesNotExist:
pass

0 comments on commit ae196e1

Please sign in to comment.