Beispiel #1
0
def send_message_to(request, employee_username):
    """
    Send message to employee
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            employee = get_object_or_404(Employee, username=employee_username)
            message = Message(
                text=request.data['message'],
                from_user=request.user,
                to_user=employee.username
            )
            message.save()
            send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
def send_message_event(request, event_id):
    """
    Send message to all employees in event
    ---
    response_serializer: employees.serializers.EmployeeDeviceSerializer
    parameters:
    - name: message
      type: string
      required: true
    responseMessages:
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden.
    - code: 404
      message: Not found
    - code: 500
      message: Internal Server Error
    """
    if request.method == 'POST':
        if 'message' in request.data:
            event = get_object_or_404(Event, pk=event_id)
            EventActivity.objects.create(event=event, text=request.data['message'])
            event_participants = EventParticipant.objects.filter(event=event)
            for record in event_participants:
                employee = Employee.objects.get(pk=record.participant.id)
                message = Message.objects.create(text=request.data['message'],
                                                 from_user=request.user, to_user=employee.username)
                send_push_notification(employee, message.text)
            serializer = MessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            content = {'detail': config.NO_MESSAGE}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
def send_message_to_all(request):
    """
    Creates a message to all devices by city
    """
    devices = Device.objects.all()
    message_text = (request.data['message']
                    if 'message' in request.data.keys() else None)
    if message_text is not None:
        message = Message.objects.create(text=message_text, city=0)
        if devices.count() > 0:
            send_messages.send_push_notification(devices, message.text)
        serializer = MessageSerializer(message)
        return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)
    def evaluate_block_users(self):
        employees = get_list_or_404(Employee)
        for employee in employees:
            if employee.yesterday_given > config.MAX_STARS_GIVEN_DAY:
                employee.is_blocked = True
            if employee.yesterday_received > config.MAX_STARS_RECEIVED_DAY:
                employee.is_blocked = True
            if employee.current_month_given > config.MAX_STARS_GIVEN_MONTHLY:
                employee.is_blocked = True
            if employee.current_month_score > config.MAX_STARS_RECEIVED_MONTHLY:
                employee.is_blocked = True
            employee.save()

            try:
                if employee.is_blocked:
                    self.send_blocked_notification_email(employee)
                    send_push_notification(employee, config.USER_BLOCKED_NOTIFICATION_MESSAGE % employee.username)
            except Exception as e:
                print e
Beispiel #5
0
def give_star_to(request, from_employee_id, to_employee_id):
    """
    This endpoint saves stars on both employees (from and to).
    This endpoint saves stars on both employees (from and to).
    ---
    response_serializer: stars.serializers.StarSerializer
    responseMessages:
    - code: 400
      message: Bad request
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden, authentication credentials were not provided
    - code: 404
      message: Not found (from_employee_id or to_employee_id or category or subcategory)
    - code: 406
      message: User is unable to give stars to itself.
    parameters:
    - name: category
      description: category id
      required: true
      paramType: string
    - name: subcategory
      description: subcategory id
      required: true
      paramType: string
    - name: keyword
      description: keyword id
      required: true
      paramType: string
    """
    if from_employee_id == to_employee_id:
        content = {'detail': config.USER_UNABLE_TO_GIVE_STARS_ITSELF}
        return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
    elif request.method == 'POST':
        # Set values from request.data from POST
        text = (request.data['text'] if 'text' in request.data.keys() else None)
        from_user = get_object_or_404(Employee, pk=from_employee_id)
        to_user = get_object_or_404(Employee, pk=to_employee_id)
        category = get_object_or_404(Category, pk=request.data['category'])
        subcategory = get_object_or_404(Subcategory, pk=request.data['subcategory'])
        keyword = get_object_or_404(Keyword, pk=request.data['keyword'])

        if from_user.is_blocked:
            content = {'detail': config.USER_BLOCKED_TO_GIVE_STARS}
            return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
        elif to_user.is_blocked:
            content = {'detail': config.USER_BLOCKED_TO_RECEIVED_STARS}
            return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)

        # Create data object to save
        data = {"category": category.id,
                "subcategory": subcategory.id,
                "keyword": keyword.id,
                "text": text,
                "from_user": from_user.id,
                "to_user": to_user.id}

        # Validate serializer with data provided.
        serializer = StarSerializer(data=data)
        if serializer.is_valid():
            # Save recommendation
            serializer.save()

            # Add 1 to employee given points
            from_user.add_stars_given(1)
            from_user.save()

            current_level = to_user.level

            # Add points to to_user according category weight
            to_user.add_stars(category.weight)
            message = config.RECOMMENDATION_MESSAGE % (category.weight, from_user.first_name, from_user.last_name)
            send_push_notification(to_user, message)
            to_user.evaluate_level()
            to_user.save()

            # Add activity log if user level up
            if to_user.level != current_level:
                message = config.LEVEL_UP_TEXT % (to_user.first_name, to_user.last_name, to_user.level)
                activity = Activity.objects.create(text=message, to_user=to_user)
                send_push_notification(to_user, message)
                activity.save()

            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #6
0
def give_star_to_many(request, from_employee_id):
    """
    This endpoint saves stars on many employees.
    ---
    response_serializer: stars.serializers.StarSerializer
    responseMessages:
    - code: 400
      message: Bad request
    - code: 401
      message: Unauthorized. Authentication credentials were not provided. Invalid token.
    - code: 403
      message: Forbidden, authentication credentials were not provided
    - code: 404
      message: Not found (from_employee_id or to_users or category or subcategory)
    - code: 406
      message: User is unable to give stars to itself.
    parameters:
    - name: body
      required: true
      paramType: body
      pytype: stars.serializers.StarBulkSerializer
    """
    if request.method == 'POST':
        serializer_bulk = StarBulkSerializer(data=request.data)
        errors = []
        stars_added = 0
        if serializer_bulk.is_valid():
            text = (request.data['text'] if 'text' in request.data.keys() else None)
            from_user = get_object_or_404(Employee, pk=from_employee_id)
            category = get_object_or_404(Category, pk=request.data['category'])
            keyword = get_object_or_404(Keyword, pk=request.data['keyword'])

            # Create data object to save
            data = {"category": category.id,
                    "keyword": keyword.id,
                    "text": text,
                    "from_user": from_user.id}

            for user_pk in request.data['to_users']:
                data.update({"to_user": int(user_pk)})
                serializer = StarSerializer(data=data)
                if serializer.is_valid():
                    serializer.save()
                    stars_added += 1

                    # Add points
                    to_user = get_object_or_404(Employee, pk=user_pk)
                    from_user.add_stars_given(1)
                    from_user.save()

                    current_level = to_user.level

                    # Add points to to_user according category weight
                    if from_user.position:
                        weight = from_user.position.weight
                    else:
                        weight = 1

                    to_user.add_stars(weight)
                    message = config.RECOMMENDATION_MESSAGE % (weight, from_user.first_name, from_user.last_name)
                    send_push_notification(to_user, message)
                    to_user.evaluate_level()
                    to_user.save()

                    # Add activity log if user level up
                    if to_user.level != current_level:
                        message = config.LEVEL_UP_TEXT % (to_user.first_name, to_user.last_name, to_user.level)
                        activity = Activity.objects.create(text=message, to_user=to_user)
                        activity.save()

                else:
                    errors.append(serializer.errors)
        else:
            errors.append(serializer_bulk.errors)

        if len(errors) == 0:
            content = {'detail': config.SUCCESSFULLY_STARS_ADDED}
            return Response(content, status=status.HTTP_201_CREATED)
        else:
            stars_results = {"stars_added": stars_added}
            detail = {'detail': errors}
            content = stars_results.copy()
            content.update(detail)
            return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
 def save_model(self, request, obj, form, change):
     devices = Device.objects.filter(city=obj.city) | Device.objects.filter(
         city=0)
     send_push_notification(devices, obj.text)
     obj.save()