def update(self, request, pk=None): """ Method, responding to a PUT request, updates status of a specific token, which in turn determines if a token is disallowed or allowed for use. :param request: request data :param pk: an unique identifier of a token :return: rest_framework.response.Response containing serialized data """ token_enabled = request.DATA.get('token_enabled', None) if not pk: raise core_exceptions.InvalidRequestException( 'Unique identifier (id) is missing in a request URI.') elif not token_enabled: raise core_exceptions.InvalidRequestException( 'Token status not found in a request.') try: auth_token = SimpleTokenAuthModel.objects.get(pk=pk) except SimpleTokenAuthModel.DoesNotExist: raise core_exceptions.DoesNotExistException() if not auth_token.change_token_status(token_enabled): raise core_exceptions.InvalidRequestException() auth_token.save() return Response({'id': auth_token.id})
def get_us_cpm_avg_by_date(): if request.method == "GET": date = utils.get_date_from_args() window = utils.get_window_from_args() res = db.get_us_cpm_avg_by_date(date, window) return res raise exceptions.InvalidRequestException(request.method)
def get_world_hashtags(countryCode): if request.method == "GET": if utils.validate_country_code(countryCode): date = utils.get_date_from_args() res = db.get_hashtags_by_country(countryCode, date) return res raise exceptions.InvalidCountryException(countryCode) raise exceptions.InvalidRequestException(request.method)
def get_by_country(countryCode): """ on GET request, if countryCode is valid, return document matching countryCode at the date supplied in the arguments If countryCode is invalid, respond with code 404 and present error page On any other request respond with code 405 """ if request.method == "GET": date = utils.get_date_from_args() if (utils.validate_country_code(countryCode)): res = db.get_by_country_and_date(countryCode, date) return res raise exceptions.InvalidCountryException(countryCode) raise exceptions.InvalidRequestException(request.method)
def get_dpm_by_state(stateCode, methods=["GET"]): """ on GET request, if stateCode is valid, return document matching stateCode at the date supplied in the arguments If stateCode is invalid, respond with code 404 and present error page On any other request respond with code 405 """ if request.method == "GET": date = utils.get_date_from_args() if (utils.validate_state_code(stateCode)): res = db.get_dpm_by_state_and_date(stateCode, date) return res raise exceptions.InvalidStateException(stateCode) raise exceptions.InvalidRequestException(request.method)
def delete(self, request, pk=None): """ Method, responding to a DELETE request, deletes a specific token. :param request: request data :param pk: an unique identifier of a token :return: rest_framework.response.Response containing serialized data """ if not pk: raise core_exceptions.InvalidRequestException( 'Unique identifier (id) is missing in a request URI.') try: auth_token = SimpleTokenAuthModel.objects.get(pk=pk) auth_token.delete() except SimpleTokenAuthModel.DoesNotExist: raise core_exceptions.DoesNotExistException() return Response({'deleted': True})
def get_world_cpm_by_date(): if request.method == "GET": date = utils.get_date_from_args() res = db.get_world_cpm_by_date(date) return res raise exceptions.InvalidRequestException(request.method)