107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
import logging
|
|
|
|
from app_be.views.twitter_api import twitter_api
|
|
from django.http import JsonResponse, HttpRequest
|
|
|
|
from py_jwt_validator import PyJwtValidator, PyJwtException
|
|
|
|
from rest_framework.decorators import api_view
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
from app_be.models import Feed
|
|
from app_be.serializers import FeedSerializer
|
|
from twitter import Status
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def authorize(request):
|
|
logger.debug('Validating request: {}'.format(request))
|
|
|
|
if 'Authorization' not in request.headers:
|
|
print(f"Authorization header missing")
|
|
logger.error(f"Authorization header missing")
|
|
return None
|
|
|
|
bearer = request.headers['Authorization']
|
|
|
|
if len(bearer.split()) < 2:
|
|
return None
|
|
|
|
jwt = bearer.split()[1]
|
|
|
|
try:
|
|
validator = PyJwtValidator(jwt, auto_verify=False)
|
|
token = validator.verify(True)
|
|
if 'payload' in token:
|
|
payload = token['payload']
|
|
if 'sub' in payload:
|
|
return payload['sub']
|
|
except PyJwtException as e:
|
|
print(f"Exception caught. Error: {e}")
|
|
logger.error(f"Exception caught. Error: {e}")
|
|
return None
|
|
except UnicodeDecodeError as e2:
|
|
print(f"Exception caught. Error: {e2}")
|
|
logger.error(f"Exception caught. Error: {e2}")
|
|
return None
|
|
return None
|
|
|
|
|
|
class LoginClass:
|
|
@staticmethod
|
|
@api_view(['GET'])
|
|
def login(request):
|
|
user_sub = authorize(request)
|
|
if not user_sub:
|
|
return JsonResponse({}, status=401)
|
|
|
|
return JsonResponse({'user': user_sub}, safe=False, status=200)
|
|
|
|
|
|
class TwitterClass:
|
|
@staticmethod
|
|
@api_view(['GET'])
|
|
def getLastSixTweets(request):
|
|
|
|
#TODO: Bearer Token im FE einfügen
|
|
'''user_sub = authorize(request)
|
|
if not user_sub:
|
|
return JsonResponse({}, status=401)'''
|
|
|
|
return JsonResponse(status=200,data=twitter_api.get_last_six_tweets(),safe=False)
|
|
|
|
@staticmethod
|
|
@api_view(['GET'])
|
|
def getMoreTweets(request,twitter_id):
|
|
|
|
#TODO: Bearer Token im FE einfügen
|
|
'''user_sub = authorize(request)
|
|
if not user_sub:
|
|
return JsonResponse({}, status=401)'''
|
|
|
|
return JsonResponse(status=200,data=twitter_api.get_more_tweets(twitter_id),safe=False)
|
|
|
|
|
|
class FeedViewSet(ModelViewSet):
|
|
queryset = Feed.objects.all()
|
|
serializer_class = FeedSerializer
|
|
|
|
@staticmethod
|
|
@api_view(['PUT'])
|
|
def update_feed(request: HttpRequest, feed_id):
|
|
logger.debug('update_feed called for feed %d', feed_id)
|
|
feed_instance = Feed.objects.get(pk=feed_id)
|
|
if 'url' in request.data:
|
|
feed_instance.url = request.data['url']
|
|
if 'active' in request.data:
|
|
feed_instance.active = request.data['active']
|
|
if 'icon' in request.data:
|
|
feed_instance.icon = request.FILES['icon']
|
|
if 'keywords' in request.data:
|
|
feed_instance.keywords = request.data['keywords']
|
|
if 'match_all_keywords' in request.data:
|
|
feed_instance.match_all_keywords = request.data['match_all_keywords']
|
|
feed_instance.save()
|
|
return JsonResponse(status=200, data={})
|