def __create_event_from_booking(user_email): from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from v2.core.eventqueue.models import EventQueue from core.booking.models import BookedTeeTime from core.golfcourse.models import GolfCourseEvent from api.userMana.tasks import log_activity eventqueue = EventQueue.objects.get_eventqueue(user_email=user_email) print(eventqueue) user = User.objects.filter( Q(**({ 'email': user_email })) | Q(**({ 'username': user_email }))).first() print(user) event_ctype = ContentType.objects.get_for_model(GolfCourseEvent) if eventqueue.exists() and user: user_displayname = user.user_profile.display_name or user.username for eq in eventqueue: ge = GolfCourseEvent.objects.create( time=eq.booking.teetime.time, user=user, golfcourse=eq.booking.golfcourse, description='{} want to play'.format(user_displayname), date_start=eq.booking.teetime.date, date_end=eq.booking.teetime.date, is_publish=True, pod='M' if eq.booking.teetime.time > datetime.datetime.strptime('12:00', '%H:%M').time() else 'A') print(user.id, 'create_event', ge.id, event_ctype.id) log_activity(user.id, 'create_event', ge.id, event_ctype.id) eq.is_created = True eq.save()
def create(self, request, *args, **kwargs): serializer = CommentActivitySerializer(data=request.DATA) if not serializer.is_valid(): return Response( { 'status': '400', 'code': 'E_INVALID_PARAMETER_VALUES', 'detail': serializer.errors }, status=400) ctype = None if serializer.data['content_type'] == 'Event': ctype = ContentType.objects.get_for_model(GolfCourseEvent) if not ctype: return Response({}, status=404) log_activity(request.user.id, 'comment', serializer.data['object_id'], ctype.id) return Response({'detail': 'OK'}, status=200)
def log_review_activity(instance, **kwargs): ctype = ContentType.objects.get_for_model(GolfCourseReview) log_activity(instance.user.id, 'review_golfcourse', instance.id, ctype.id)
def create(self, request, *args, **kwargs): if 'data' in request.DATA: request.DATA['data']['user'] = request.user.id data = request.DATA['data'] players = request.DATA['data']['invite_people'] else: request.DATA['user'] = request.user.id data = request.DATA players = request.DATA['invite_people'] golfcourse_names = list(GolfCourse.objects.all().values('id', 'name')) try: int(data['golfcourse']) except ValueError: golfcourse_id = None for g in golfcourse_names: if fuzz.token_set_ratio(data['golfcourse'], g['name']) > 90: golfcourse_id = g['id'] data['golfcourse'] = golfcourse_id break if not golfcourse_id: return Response({'status': '400', 'code': 'E_INVALID_PARAMETER_VALUES', 'detail': 'Invalid GolfCourse'}, status=400) for player in players: email = player.get('email') phone = player.get('phone') user_id = player.get('user_id') if not any([email, phone, user_id]): return Response({'status': '400', 'code': 'E_INVALID_PARAMETER_VALUES', 'detail': 'You must fill at least email or phone'}, status=400) if email == request.user.username or user_id == request.user.id: return Response({'status': '400', 'code': 'E_INVALID_PARAMETER_VALUES', 'detail': 'Cannot invite myself'}, status=400) data.update({'date_start': data['date'], 'date_end': data['date']}) serializer = self.serializer_class(data=data) if not serializer.is_valid(): return Response({'status': '400', 'code': 'E_INVALID_PARAMETER_VALUES', 'detail': serializer.errors}, status=400) invitation = serializer.save() event_ctype = ContentType.objects.get_for_model(GolfCourseEvent) log_activity(request.user.id, 'create_event', invitation.id, event_ctype.id) if players: # ----- Message for gc24 notification ------ detail = '<a href=/#/profile/' + str(request.user.id) + '/></a> mời bạn tham gia sự kiện tại <b>' + str( invitation.golfcourse.name) + '</b>' if invitation.time: detail += ' lúc <b>' + str(invitation.time.strftime('%H:%M')) + '</b>' detail += ' vào <b>' + DOW[str(invitation.date_start.strftime('%A'))] + ', ' + str( invitation.date_start.strftime('%d-%m-%Y')) + '</b>' detail_en = '<a href=/#/profile/' + str(request.user.id) + '/></a> invited you to join the event at <b>' + str( invitation.golfcourse.name) + '</b>' if invitation.time: detail_en += ' at <b>' + str(invitation.time.strftime('%H:%M')) + '</b>' detail_en += ' on <b>' + str(invitation.date_start.strftime('%A')) + ', ' + str( invitation.date_start.strftime('%d-%m-%Y')) + '</b>' # ----- Message for push notification ------ detail_notify_en = str(request.user.user_profile.display_name) + ' invited you to join the event at ' + str( invitation.golfcourse.name) if invitation.time: detail_notify_en += ' at ' + str(invitation.time.strftime('%H:%M')) detail_notify_en += ' on ' + str(invitation.date_start.strftime('%A')) + ', ' + str( invitation.date_start.strftime('%d-%m-%Y')) detail_notify_vi = str(request.user.user_profile.display_name) + ' mời bạn tham gia sự kiện tại ' + str( invitation.golfcourse.name) if invitation.time: detail_notify_vi += ' lúc ' + str(invitation.time.strftime('%H:%M')) detail_notify_vi += ' vào ' + str(invitation.date_start.strftime('%A')) + ', ' + str( invitation.date_start.strftime('%d-%m-%Y')) translate_message = { 'alert_vi': detail_notify_vi, 'alert_en': detail_notify_en } # --- End message --- for player in players: name = player.get('name') email = player['email'].lower() if player.get('email') else None phone = player.get('phone') player = User.objects.filter(Q(id=player.get('user_id')) | Q(username=email) | Q(email=email)).first() if player: instance, created = EventMember.objects.get_or_create(user=player, # status='P', event=invitation) if not created: instance.status = 'P' instance.save() else: created = False player = Customer.objects.create(name=name, email=email, phone_number=phone) instance = EventMember.objects.create(customer=player, status='P', event=invitation) if player and isinstance(player, User) and created: Notice.objects.get_or_create(content_type=event_ctype, object_id=invitation.id, to_user=player, detail=detail, detail_en=detail_en, notice_type='I', from_user=request.user, send_email=False) send_notification.delay([player.id], detail_notify_en, translate_message) return Response({'status': '200', 'code': 'OK', 'detail': 'OK'}, status=200)
def log_scoring_activity(sender, instance, created, **kwargs): if instance.event_member.user: ctype = ContentType.objects.get_for_model(Game) public = instance.is_finish & instance.active log_activity(instance.event_member.user_id, 'create_game', instance.id, ctype.id, public)