def count(self, request): """ 알림함 뱃지 카운트 --- """ # 14일 지난 메세지 삭제 처리 last_created_at = utc_now() - timedelta(days=14) user_id = self.request.META.get('HTTP_IDREGISTER') if user_id is None or user_id == '0': return Response({'count': 0}, status=status.HTTP_200_OK) MessageBox.objects.filter( Q(user_id=user_id) | Q(user_id__isnull=True, group_id__isnull=True)).filter( updated_at__lt=last_created_at, is_active=True).update(is_active=False) user = get_object_or_404(User, id=user_id) joined_at = user.date_joined - timedelta(hours=9) query_set = MessageBox.objects.filter( Q(user_id=user_id) | Q(user_id__isnull=True, group_id__isnull=True)).filter( created_at__range=(joined_at, utc_now()), is_active=True).distinct() count = query_set.exclude(messagecheck__user_id=user_id).count() return Response({'count': count}, status=status.HTTP_200_OK)
def perform_destroy(self, instance): user_id = self.request.META.get('HTTP_IDREGISTER') deleted_at = utc_now() MessageCheck.objects.filter( user_id=user_id, message=instance).update(deleted_at=deleted_at) if instance.user: instance.is_active = False instance.save()
def make_like_message(self, review_id, register_id, created_at=None): """ 좋아요 알림함 메세지 생성 """ review = Review.objects.select_related( 'user', 'product', 'product__brand').get(id=review_id) if not created_at: created_at = utc_now() two_week_ago = created_at.astimezone( tz=timezone.get_current_timezone()) - timedelta(days=14) obj, created = MessageBox.objects.get_or_create( user_id=review.user_id, category=MessageCategory.objects.get(name='좋아요'), reference_id=review_id, is_active=True) if created: register = User.objects.get(id=register_id) obj.message = "{}님이 내 리뷰를 좋아합니다.\n{} - {}".format( register.nickname, review.product.brand.name, review.product.name) obj.created_at = created_at obj.updated_at = created_at obj.save() else: MessageCheck.objects.filter(user_id=review.user_id, message=obj.id).delete() likes = Reviewlike.objects.filter( writer=review.user, product=review.product, create_date__gte=two_week_ago.strftime('%Y%m%d%H%M%S'), ) likes = likes.filter(create_date__gte=obj.created_at.astimezone( tz=timezone.get_current_timezone()).strftime( '%Y%m%d%H%M%S')).order_by('create_date') likes = likes.values('register__nickname', ) count = likes.count() if count == 2: nickname1 = likes[1].get('register__nickname') nickname2 = likes[0].get('register__nickname') obj.message = "{}, {}님이 내 리뷰를 좋아합니다.\n{} - {}".format( nickname1, nickname2, review.product.brand.name, review.product.name) elif count > 2: likes = list(likes) nickname = likes[-1].get('register__nickname') obj.message = "{}님 외 {}명이 내 리뷰를 좋아합니다.\n{} - {}".format( nickname, count - 1, review.product.brand.name, review.product.name) obj.updated_at = created_at obj.save()
def get_queryset(self): user_id = self.request.META.get('HTTP_IDREGISTER') user = get_object_or_404(User, id=user_id) joined_at = user.date_joined - timedelta(hours=9) query_set = super(MessageView, self).get_queryset() query_set = query_set.select_related('category').filter( created_at__range=(joined_at, utc_now())).filter( Q(user_id=user_id) | Q(user_id__isnull=True, group_id__isnull=True)).exclude( messagecheck__in=MessageCheck.objects.filter( user=user, deleted_at__isnull=False)).distinct() return query_set
def check(self, request, pk=None): """ 알림함 메세지 체크 --- """ user_id = self.request.META.get('HTTP_IDREGISTER') user = get_object_or_404(User, id=user_id) message = get_object_or_404(MessageBox, id=pk) obj, created = MessageCheck.objects.get_or_create(user=user, message=message) obj.read_at = utc_now() obj.save() return Response(MessageReadSerializer(obj).data, status=status.HTTP_200_OK)
def all(self, request): """ 알림함 메세지 전체 삭제 --- """ user_id = self.request.META.get('HTTP_IDREGISTER') query_set = self.get_queryset() deleted_at = utc_now() MessageCheck.objects.filter(user_id=user_id, message__in=[ message for message in query_set ]).update(deleted_at=deleted_at) query_set.filter(user__isnull=False).update(is_active=False) return Response(status=status.HTTP_204_NO_CONTENT)
def get_review_points(self): result = dict() points = Point.objects.filter(name__in=('review_point', 'first_bonus_point', 'multiple_bonus_point')).all() for r in points: _point = r.point if r.event_start_date and r.event_end_date and r.event_point and \ r.event_start_date < utc_now() < r.event_end_date: _point = r.event_point if r.name == 'review_point': result['review_point'] = _point if r.name == 'multiple_bonus_point': result['multiple_bonus_point'] = _point if r.name == 'first_bonus_point': result['first_bonus_point'] = _point return result
def list(self, request, *args, **kwargs): """ 알림 메세지 리스트 --- <b>헤더</b> - IDREGISTER: (필수) 회원 항번 <br> """ user_id = self.request.META.get('HTTP_IDREGISTER') user = get_object_or_404(User, id=user_id) response = super(MessageView, self).list(request, *args, **kwargs) query_set = self.get_queryset() latest_checked_at = utc_now() - timedelta(days=14) query_set = query_set.exclude(id__in=[ checked.message_id for checked in MessageCheck.objects.filter( user=user, checked_at__gte=latest_checked_at) ]) MessageCheck.objects.bulk_create([ MessageCheck(user=user, message=message) for message in query_set ]) return response
def like(self, request, pk=None): """ 리뷰 좋아요 --- 헤더 값 <br> IDREGISTER (필수) """ from django.utils import timezone from libs.utils import utc_now cuid = int(request.META.get('HTTP_IDREGISTER') or 0) try: register = User.objects.get(id=cuid, is_active=True) except: raise InvalidParameterException( _("로그인이 필요합니다.") ) review = get_object_or_404(Review, id=pk) product = review.product writer = review.user response = dict() if register.id == writer.id: raise ConflictException( _("나의 리뷰에는 좋아요 하실 수 없습니다.") ) if Reviewlike.objects.filter(writer=writer, product=product, register=cuid).exists(): raise ConflictException( _("이미 좋아요 하셨습니다.") ) created_at = utc_now() create_date = format_round_datetime(created_at.astimezone(tz=timezone.get_current_timezone())) with transaction.atomic(): # add one Reviewlike( writer=writer, product=product, register=register, create_date=create_date ).save() # writer info update user_updated_info = users_service.get_user_score_info(writer.id) writer.review_count = user_updated_info.get('review_count') writer.like_count = user_updated_info.get('like_count') writer.score = user_updated_info.get('score') writer.save() # review like count update review.like_count = Reviewlike.objects.using('default').filter(writer=writer, product=product).count() review.save() # elastic update body = { "doc": {'likeCount': review.like_count} } elasticsearch_reviews.update(_id=review.id, body=body) # 알림함 try: review_service.make_like_message(review.id, register.id, created_at) except: pass # send push message push_text = "{} 님이 내 리뷰를 좋아합니다.\n{} - {}".format( register.nickname, product.brand.name, product.name ) try: send_push_message( push_text, link_type=17, link_code=product.id, target_id=writer.id, ) except: pass response['is_success'] = True response['message'] = _("좋아요 되었습니다.") return Response(SuccessMessageResponse(response).data, status=status.HTTP_201_CREATED)