def post(self, request, image_id, format=None): user = request.user try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) # 이미지를 찾고, 이미지가 존재하면 이미 존재하는 좋아요를 찾는다. try: preexisiting_like = models.Like.objects.get(creator=user, image=found_image) return Response(status=status.HTTP_304_NOT_MODIFIED) # 이미 존재하는 좋아요가 없으면, 좋아요를 생성. except models.Like.DoesNotExist: new_like = models.Like.objects.create(creator=user, image=found_image) new_like.save() # create notification for user follow notification_views.create_notification(user, found_image.creator, 'like', found_image) return Response(status=status.HTTP_201_CREATED)
def post(self, request, image_id, format=None): # user는 요청한 유저ID를 가져가게 되어 있다. user = request.user try: # 해당 이미지는 이미지 ID와 같은 ID를 갖고 있다. 그 이미지 ID는 URL에서 왔다. found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=user, image=found_image) # create notification for user follow notification_views.create_notification(user, found_image.creator, 'comment', found_image, serializer.data['message']) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, image_id, format=None): user = request.user # if문 형식으로 try catch 사용하기 try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = serializers.CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=user, image=found_image) notifications_views.create_notification(user, found_image.creator, 'comment', found_image, serializer.data['message']) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, image_id, format=None): user = request.user # if문 형식 대신 try catch 형식으로 대신함 try: found_image = models.Image.objects.get(id=image_id) except models.Image.DoesNotExist: return Response( status=status.HTTP_404_NOT_FOUND) # 해당 이미지가 없으면 404에러 try: preexisting_like = models.Like.objects.get( # 이미 좋아요가 눌려있을때 creator=user, image=found_image) return Response(status=status.HTTP_304_NOT_MODIFIED) except models.Like.DoesNotExist: # 좋아요가 없다면 new_like = models.Like.objects.create( # 좋아요 추가 creator=user, image=found_image) notifications_views.create_notification(user, found_image.creator, 'like', found_image) new_like.save() # 값 저장 return Response(status=status.HTTP_201_CREATED)
def post(self, request, item_id, format=None): user = request.user try: found_production = Production.objects.get(id=item_id) except Production.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) try: preexisiting_like = Like.objects.get(creator=user, post=found_production) return Response(status=status.HTTP_304_NOT_MODIFIED) except Like.DoesNotExist: new_like = Like.objects.create(creator=user, post=found_production) new_like.save() notifications_views.create_notification(user, found_production.creator, "like", found_production) return Response(status=status.HTTP_201_CREATED)
def request_membership(request, organization_id=None): user = request.user organization = Organization.objects.filter(id=organization_id).first() if organization: # role = request.DATA.get("role") if not OrganizationMember.user_is_member_of(user, organization): if not OrganizationMember.user_is_member_of(user): role = "COO" membership = OrganizationMember() membership.organization = organization membership.user = user membership.role = role membership.initiator = user membership.save() plain_member = membership.plain() serializer = PlainMemberSerializer(plain_member) message = "%s %s has requested membership of %s" % (user.first_name, user.last_name, organization.name, ) for admin in organization.administrators(): create_notification("OMR", membership.id, membership.user, admin.user, message=message) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "You are already a member/awaiting membership of some other organization" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "You are already a member/awaiting membership of this organization" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Organization not found" }, status=HTTP_404_NOT_FOUND)
def post(self, request, item_id, format=None): user = request.user try: found_production = Production.objects.get(id=item_id) except Production.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=user, post=found_production) notifications_views.create_notification( user, found_production.creator, "comment", found_production, serializer.data["message"], ) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def process_membership_request(request, organization_id=None, request_id=None, accept=True): """ Accepts/Rejects a membership request. :param request: an HTTP request :param organization_id: organization of the request :param request_id: id of membership request :param accept: accept/reject :return: Response with success/failure """ user = request.user organization = Organization.objects.filter(id=organization_id).first() membership = OrganizationMember.objects.filter(id=request_id).first() membership_role = request.DATA.get("role") if organization: if membership: if OrganizationMember.user_is_admin(organization, user): if membership.is_new(): message = "" notification_type = "" if accept: membership.is_accepted = True message = "Your membership request has been accepted" notification_type = "ORA" else: membership.is_rejected = True message = "Your membership request has been rejected" notification_type = "ORR" if membership_role: membership.role = membership_role else: membership.role = "COO" membership.save() plain_invitation = membership.plain() serializer = PlainMemberSerializer(plain_invitation) create_notification("", membership.id, membership.initiator, membership.user, message=message) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "This membership request had already been accepted/rejected" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You are not authorized to accept/reject this membership request" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Membership request not found" }, status=HTTP_404_NOT_FOUND) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Organization not found" }, status=HTTP_404_NOT_FOUND)
def approve_leave(request): if not manager.has_lms_access(request.user)[1]: messages.error( request, "Sorry. You do not have permission for LMS. Please contact your authority." ) return HttpResponseRedirect(reverse('crm_index')) if not check_leave_admin.is_leave_issuer(request.user): messages.error(request, "Sorry. You do not have permission.") return HttpResponseRedirect(reverse('lms_index')) id = '' if request.method == "POST": try: id = request.POST['lms_id'] except (Exception, ValueError) as e: print(e) messages.error(request, "Something went wrong. Please try again.") return HttpResponseRedirect(reverse('lms_index')) if leave_manager.approve_leave_request(request, int(id)): leave = leave_models.Leave.objects.get(id=int(id)) notification_data = { "type": "leave-approved", "data": leave, "text": "Your {} has been approved by {}".format( leave.type.type, request.user.get_full_name()) } notification_views.create_notification( request, data=notification_data, for_employee=leave.user.employee) total_noti = notification_views.notification_count(request) pusher_client.trigger('leave-channel', 'leave-approved', { "count": total_noti, "applied_by_id": leave.user.employee.user.id }) messages.success(request, "Leave approved successfully.") else: messages.error(request, "Something went wrong. Please try again.") return HttpResponseRedirect(reverse('get_leave_requests')) else: messages.error(request, 'Method not allowed. Please try again. Thank You') return HttpResponseRedirect(reverse('lms_index'))
def send_broadcast(request, event_id=None): me = request.user to = request.DATA.get("to") title = request.DATA.get("title") msg = request.DATA.get("message") event = Event.objects.filter(id=event_id).first() if not event: return Response({ "status": HTTP_404_NOT_FOUND, "message": "Event not found" }, status=HTTP_404_NOT_FOUND) event_organization = event.owner user_organization = OrganizationMember.user_organization(me) if event_organization != user_organization: return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You are not an authorized member of the event's organization" }, status=HTTP_401_UNAUTHORIZED) if not OrganizationMember.user_is_admin(user_organization, me): return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You must be an administrator to broadcast messages" }, status=HTTP_401_UNAUTHORIZED) if to == "ALL": recipients = User.objects.all() elif to == "CASTING": recipients = User.objects.filter(my_user__type='C') else: recipients = User.objects.filter(my_user__type='T') for recipient in recipients: message = Message() message.from_user = me message.to_user = recipient message.title = title message.message = msg message.save() create_notification("MSG", message.id, me, recipient, message=msg) serializer = PlainMessageSerializer({ "status": HTTP_200_OK, "message": "Broadcast sent." }) return Response(serializer.data)
def post(self, request, user_id, format=None): user = request.user try: user_to_follow = User.objects.get(id=user_id) except User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) user.following.add(user_to_follow) user_to_follow.followers.add(user) create_notification(user, user_to_follow, Constant.TYPE_FOLLOW) return Response(status=status.HTTP_200_OK)
def post(self, request, user_id, forma=None): user = request.user try: user_to_follow = User.objects.get(id=user_id) except User.DoexNotExist: return Response(status=status.HTTP_404_NOT_FOUND) user.following.add(user_to_follow) user.save() notifications_views.create_notification(user, user_to_follow, 'follow') return Response(status=status.HTTP_200_OK)
def process_invitation(request, organization_id=None, invitation_id=None, accept=True): """ Accepts/Rejects an invitation. :param request: an HTTP request :param organization_id: organization of the invitation :param invitation_id: id of inivitation :param accept: accept/reject :return: Response with success/failure """ user = request.user organization = Organization.objects.filter(id=organization_id).first() invitation = OrganizationMember.objects.filter(id=invitation_id).first() if organization: if invitation: if invitation.user == user: if invitation.is_new(): message = "" notification_type = "" if accept: invitation.is_accepted = True message = "Your membership invitation has been accepted" notification_type = "OIA" else: invitation.is_rejected = True message = "Your membership invitation has been rejected" notification_type = "OIR" invitation.save() plain_invitation = invitation.plain() serializer = PlainMemberSerializer(plain_invitation) create_notification(notification_type, invitation.id, invitation.user, invitation.initiator, message=message) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "This invitation had already been accepted/rejected" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You are not authorized to accept/reject this invitation" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Invitation not found" }, status=HTTP_404_NOT_FOUND) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Organization not found" }, status=HTTP_404_NOT_FOUND)
def process_attendance_request(request, event_id=None, request_id=None, accept=True): """ Accepts/Rejects a membership request. :param request: an HTTP request :param event_id: organization of the request :param request_id: id of membership request :param accept: accept/reject :return: Response with success/failure """ user = request.user event = Event.objects.filter(id=event_id).first() attend_request = EventAttendee.objects.filter(id=request_id).first() if event: organization = event.owner if attend_request: if OrganizationMember.user_is_admin(organization, user): if attend_request.is_new(): message = "" n_type = "ERA" if accept: attend_request.is_accepted = True message = "Your attendance request has been accepted" else: attend_request.is_rejected = True n_type = "ERR" message = "Your attendance request has been rejected" attend_request.save() plain_request = attend_request.plain() serializer = PlainAttendeeSerializer(plain_request) create_notification(n_type, plain_request.attendance_id, user, attend_request.attendee, message=message) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "The request to attend by this user has already been accepted/rejected" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You are not authorized to accept/reject this membership request" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_404_NOT_FOUND, "message": "User request not found" }, status=HTTP_404_NOT_FOUND) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Event not found" }, status=HTTP_404_NOT_FOUND)
def send_message(request): """ Sends a message to a linked user. Allowed HTTP methods are:\n 1. POST to send\n Accepts following hash:\n . {\n "to": [user_id],\n "title": [message_title],\n "message": [the_message]\n }\n Returns:\n . Newly created message Status:\n 1. 200 on success 2. 400 if some error occurs 3. 401 if un-authorized 4. 404 if user not found Notes:\n 1. Require user's token to be sent in the header as:\n Authorization: Token [token]\n """ me = request.user to_id = request.DATA.get("to") title = request.DATA.get("title") msg = request.DATA.get("message") to = User.objects.filter(id=to_id).first() if to: if Link.is_already_link(me, to): message = Message() message.from_user = me message.to_user = to message.title = title message.message = msg message.save() create_notification("MSG", message.id, me, to, message=msg) serializer = PlainMessageSerializer(message.plain()) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "Cannot send message to a non-link" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_404_NOT_FOUND, "message": "User not found" }, status=HTTP_404_NOT_FOUND)
def post(self, request, id, format=None): user = request.user try: user_to_follow = models.User.objects.get(id=id) except models.User.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) user.following.add(user_to_follow) user.save() # create notification for user follow notification_views.create_notification(user, user_to_follow, 'follow') return Response(status=status.HTTP_200_OK)
def send_callbacks_to_talent(request, event_id=None): """ NOT for mobiles """ user = request.user event = Event.objects.filter(id=event_id).first() str_talent_callbacks = request.DATA.get("talent_callbacks") if event: user_organization = OrganizationMember.user_organization(user) if user_organization: is_admin = OrganizationMember.user_is_admin(user_organization, user) if is_admin and event.owner.id == user_organization.id: talent_callback_ids = str_talent_callbacks.split(",") for talent_callback_id in talent_callback_ids: talent_callback = CallbackTalent.objects.filter(id=talent_callback_id).first() if talent_callback: if talent_callback.callback.callback_organization == user_organization: talent_callback.sent_to_event_organization = True talent_callback.save() create_notification("CB", talent_callback.id, event_id, talent_callback.talent.id, message="Callback Request") else: return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You are not authorized to perform this action" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Talent callback not found" }, status=HTTP_404_NOT_FOUND) return Response({ "status": HTTP_200_OK, "message": "OK" }, status=HTTP_200_OK) return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You must be an admin of the event's organization to be able to send callbacks to talents on behalf of the casting organization" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You need to be an approved member of some organization to perform this action" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Event not found" }, status=HTTP_404_NOT_FOUND)
def request_attendance(request, event_id=None): user = request.user event = Event.objects.filter(id=event_id).first() event_organization = event.owner if event: if user.my_user.type == 'C': user_organization = OrganizationMember.user_organization(user) if user_organization is None: return Response({ "status": HTTP_400_BAD_REQUEST, "message": "You must be associated with your organization to attend this event" }, HTTP_400_BAD_REQUEST) elif user_organization.id == event_organization.id: return Response({ "status": HTTP_400_BAD_REQUEST, "message": "You are the organizer of this event and don't need to link to the event" }, HTTP_400_BAD_REQUEST) if not EventAttendee.user_is_already_attending(user, event): attendance = EventAttendee() attendance.event = event if user.my_user.type == 'C': attendance.organization = user_organization else: attendance.organization = None attendance.attendee = user attendance.save() plain_attendance = attendance.plain() serializer = PlainAttendeeSerializer(plain_attendance) message = "%s %s has requested to attend %s" % (user.first_name, user.last_name, event.name, ) for admin in event_organization.administrators(): logger.debug(admin.user.first_name) create_notification("ER", attendance.id, attendance.attendee, admin.user, message=message) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "You are already attending/awaiting approval of this event" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Event not found" }, status=HTTP_404_NOT_FOUND)
def post(self, request, image_id, format=None): user = request.user try: image = Image.objects.get(id=image_id) except Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = CommentSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=user, image=image) create_notification(user, image.creator, Constant.TYPE_COMMENT, image, request.data['message']) return Response(data=serializer.data, status=status.HTTP_201_CREATED) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, image_id, format=None): user = request.user try: image = Image.objects.get(id=image_id) except Image.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) try: Like.objects.get( creator=user, image=image, ) return Response(status=status.HTTP_304_NOT_MODIFIED) except Like.DoesNotExist: Like.objects.create( creator=user, image=image, ) create_notification(user, image.creator, Constant.TYPE_LIKE, image) return Response(status=status.HTTP_201_CREATED)
def invite_user(request, organization_id=None): initiator = request.user organization = Organization.objects.filter(id=organization_id).first() if organization: if OrganizationMember.user_is_admin(organization, initiator): user_id = request.DATA.get("to") user = User.objects.filter(id=user_id).first() role = request.DATA.get("role") if not OrganizationMember.user_is_member_of(user, organization): if not OrganizationMember.user_is_member_of(user): invitation = OrganizationMember() invitation.organization = organization invitation.user = user invitation.role = role invitation.initiator = initiator invitation.save() plain_member = invitation.plain() serializer = PlainMemberSerializer(plain_member) message = "You have been invited to join %s as %s" % (organization.name, "as an administrator" if invitation.role == "ADM" else "as a coordinator", ) create_notification("OMI", invitation.id, invitation.initiator, invitation.user, message=message) return Response(serializer.data) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "The user is already a member of some other organization" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_400_BAD_REQUEST, "message": "The user is already a member/awaiting membership of this organization" }, status=HTTP_400_BAD_REQUEST) return Response({ "status": HTTP_401_UNAUTHORIZED, "message": "You are not authorized to send membership invitation" }, status=HTTP_401_UNAUTHORIZED) return Response({ "status": HTTP_404_NOT_FOUND, "message": "Organization not found" }, status=HTTP_404_NOT_FOUND)
def apply_leave(request): if 'crm_branch' not in cache: redis.set_crm_branch(request) current_branch = cache.get('crm_branch') context = {} context.update({"current_branch": current_branch}) leave_type = leave_models.LeaveType.objects.all() context.update({"leave_type": leave_type}) if request.method == "POST": if validation.leave_validation(request): return HttpResponseRedirect(reverse("crm_index")) valid = validation.able_to_apply_leave(request) try: from_date = datetime.strptime(request.POST['from_date'], "%Y-%m-%d") context.update({"old_from_date": from_date}) except (Exception, ValueError) as e: pass try: to_date = datetime.strptime(request.POST['to_date'], "%Y-%m-%d") context.update({"old_to_date": to_date}) except (Exception, ValueError) as e: pass try: context.update({"old_reason": request.POST['leave_reason']}) except (Exception, ValueError) as e: pass try: context.update({"old_leave_type": int(request.POST["leave_type"])}) except (Exception, ValueError) as e: pass try: context.update({"old_half": request.POST['half_leave']}) except (Exception, ValueError) as e: pass if not valid[1]: context.update({"data_failed": valid[0]}) return render(request, "leave_manager/" + template_version + "/apply_leave.html", context=context) return HttpResponseRedirect(reverse("lms_apply_leave")) has_left = validation.has_leave_left(request) if not has_left[1]: if has_left[0]['status'] == "0": messages.error( request, "Sorry. You cannot apply for leave. You have {} days of <b>{}</b> leave left." .format(has_left[0]['diff'], has_left[0]['type'])) elif has_left[0]['status'] == "1": messages.error( request, "Sorry. You cannot apply for leave. You have only {} days of <b>{}</b> leave left." .format(has_left[0]['diff'], has_left[0]['type'])) elif has_left[0]['status'] == "2": messages.error( request, "Sorry. Something went wrong. Please try again later.") return render(request, "leave_manager/" + template_version + "/apply_leave.html", context=context) leave_issuer = check_leave_admin.get_employee_leave_issuer( request.user) if leave_issuer[1]: issuer = leave_issuer[0] current_lms_user = leave_manager.get_lms_user(request.user) if not current_lms_user[1]: messages.error(request, "Something went wrong. Please try again.") return HttpResponseRedirect(reverse("crm_index")) leave_details = { "current_lms_user": current_lms_user[0], "issuer": issuer, "from_date": datetime.strptime(request.POST["from_date"], "%Y-%m-%d"), "to_date": datetime.strptime(request.POST["to_date"], "%Y-%m-%d"), "leave_type": leave_models.LeaveType.objects.get( id=request.POST["leave_type"]), "leave_reason": request.POST["leave_reason"], "half_day": False, "leave_multiplier": 1, } full_ = request.user.get_full_name() if leave_manager.half_leave_applied(request): leave_details.update({ "half_day": True, "leave_multiplier": 0.5 }) apply_ = leave_manager.apply_leave(leave_details, request) if not apply_[1]: messages.error(request, "Leave applied failed. Please try again.") # notification_models.Notifications.objects.create(employee=issuer, leave=apply_[0], text="{} has applied for {}.".format(full_, leave_models.LeaveType.objects.get(id=request.POST["leave_type"]))) notification_data = { "type": "apply-leave", "data": apply_[0], "text": "{} has applied for {}.".format( full_, leave_models.LeaveType.objects.get( id=request.POST["leave_type"])), "url": '/lms/leave-requests' } notification_views.create_notification(request, data=notification_data, for_employee=issuer) return HttpResponseRedirect(reverse('lms_apply_leave')) else: messages.error( request, "Leave not applied. Probably server error or you dont have any leave issuer." ) return HttpResponseRedirect(reverse('crm_index')) else: return render(request, "leave_manager/" + template_version + "/apply_leave.html", context=context)