Пример #1
0
def add_to_group(request):
    """
    Adds a user to a smoke group
    """
    resp_data = {}
    valid_data, data = get_request_content("add_to_group", request, None, ["group_name", "group_password", "token"])

    if valid_data:
        # get smoker group
        try:
            group = SmokeGroup.objects.get(name=data["group_name"])
        except:
            resp_data["resp"] = "Group name does not exist!"
            return Response(status=status.HTTP_404_NOT_FOUND, data=resp_data)
        else:
            # check password
            if (data["group_password"]==group.password):
                user, smoker = get_user_smoker_from_token(data["token"])
                if smoker:
                    group.smokers.add(smoker)
                    group.save()
                    logger.debug("Smoker %s added to group %s." %(smoker, group))
                    return Response(status=status.HTTP_200_OK)

                else:
                    logger.error("Smoker does not exist for token: %s" %data["token"])
                    return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
            # wrong group password
            else:
                resp_data["resp"] = "Wrong password, try again!"
                return Response(data=resp_data, status=status.HTTP_403_FORBIDDEN)

    else:
        return Response(status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
def increment_smoke(request):

    """
    Creates smoke object: logs a smoke for smoker(s) and/or smoker_object
    """

    valid_data, data = get_request_content("create_smoke", request, None, ["token_list", "count"])

    if valid_data:
        try:
            data["token_list"] = ast.literal_eval(data["token_list"])
        except:
            data["token_list"] = data["token_list"]
        
        # individual smokers
        for token in data["token_list"]:
            try:
                (user, smoker) = get_user_smoker_from_token(token) 
                logger.debug("smoker: %s\tuser: %s" %(user, smoker))
            except:
                logger.exception("Smoker not found for token: %s" %token)

            else:
                try:
                    count = int(data["count"])
                except:
                    count = 1

                while count:
                    # create smoke object
                    smoke_obj = Smoke.objects.create()
                    smoke_obj.smokers.add(smoker)                
                    logger.debug("smoke obj created: %s" %smoke_obj)
                    
                    # update analytics obj
                    try:
                        analytics_obj = smoker.smoke_analytic_individual
                    except:
                        logger.exception("Smoker analytic object not found for smoker: %s" %smoker)
                    else:
                        logger.debug("Smoke analytic: %s" %analytics_obj)
                        analytics_obj.smoke_count += 1
                        analytics_obj.daily_count += 1
                        analytics_obj.weekly_count += 1
                        analytics_obj.monthly_count += 1
                        analytics_obj.save()

                    count = count - 1
        
        return Response(status=status.HTTP_200_OK)
    
    else:

        return Response(status=status.HTTP_400_BAD_REQUEST)
Пример #3
0
def get_stats(request):
    """
    Returns smoke analytics for individual smoker
    """
    resp_data = {}
    valid_data, data = get_request_content("get_stats", request, None, ["token"])

    if valid_data:
        try:
            token = data["token"]
            (user, smoker) = get_user_smoker_from_token(token)
        except:
            logger.exception("Smoker not found: %s" %token)
        else:
            resp_data = fetch_smoker_analytics(user=user)
            logger.debug("stats: %s" %resp_data)
            return Response(status=status.HTTP_200_OK, data=resp_data)
    else:

        return Response(status=status.HTTP_400_BAD_REQUEST)