mirror of
https://github.com/Rushilwiz/reinvest.git
synced 2025-04-04 20:40:19 -04:00
Merge branch 'master' of https://github.com/Rushilwiz/reinvest
This commit is contained in:
commit
e5cb749734
|
@ -39,7 +39,7 @@ class StockSerializer(serializers.ModelSerializer):
|
||||||
class StockCreateSerializer(serializers.ModelSerializer):
|
class StockCreateSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Stock
|
model = models.Stock
|
||||||
fields = ('user', 'ticker', 'buy_price', 'quantity', 'uuid')
|
fields = ('user', 'ticker', 'quantity',)
|
||||||
|
|
||||||
class CharitySerializer(serializers.ModelSerializer):
|
class CharitySerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -12,9 +12,11 @@ urlpatterns = [
|
||||||
path('charity/<int:pk>', views.CharityViewSet.as_view({'get': 'retrieve'})),
|
path('charity/<int:pk>', views.CharityViewSet.as_view({'get': 'retrieve'})),
|
||||||
path('charity/', views.CharityViewSet.as_view({'get': 'list', 'post': 'create'})),
|
path('charity/', views.CharityViewSet.as_view({'get': 'list', 'post': 'create'})),
|
||||||
path('stock/<uuid:pk>', views.StockViewSet.as_view({'get': 'retrieve'})),
|
path('stock/<uuid:pk>', views.StockViewSet.as_view({'get': 'retrieve'})),
|
||||||
path('stock/', views.StockViewSet.as_view({'get': 'list', 'post': 'create'})),
|
path('stock/create', views.StockViewSet.as_view({'post': 'create'})),
|
||||||
|
path('stock/', views.StockViewSet.as_view({'get': 'list'})),
|
||||||
path('profile/create', views.UserProfileCreate.as_view()),
|
path('profile/create', views.UserProfileCreate.as_view()),
|
||||||
path('profile/', views.UserProfileDetail.as_view()),
|
path('profile/', views.UserProfileDetail.as_view()),
|
||||||
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
|
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||||
|
path('robinhood/fetch/', views.FetchRobinhoodStocks.as_view())
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
from django.shortcuts import render, get_object_or_404
|
from django.shortcuts import render, get_object_or_404
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
|
|
||||||
|
from math import ceil
|
||||||
|
|
||||||
|
import robin_stocks as r
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
from rest_framework import status, permissions
|
from rest_framework import status, permissions
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
|
@ -36,7 +42,13 @@ class StockViewSet(ModelViewSet):
|
||||||
data.update({'user': request.user})
|
data.update({'user': request.user})
|
||||||
serializer = StockCreateSerializer(data=request.data)
|
serializer = StockCreateSerializer(data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
serializer.save()
|
login = r.login(os.getenv("ROBINHOOD_USER"), os.getenv("ROBINHOOD_PASS"))
|
||||||
|
order = r.order_buy_market(symbol=serializer.data['ticker'], quantity=serializer.data['quantity'])
|
||||||
|
stonks = r.build_holdings()
|
||||||
|
for key, value in stonks.items():
|
||||||
|
if Stock.objects.filter(uuid=value['id']).count() != 1:
|
||||||
|
stock = Stock.objects.create(user=request.user.profile, quantity=ceil(float(value['quantity'])), ticker=key, buy_price=value['price'], uuid=value['id'])
|
||||||
|
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
class UserProfileDetail(APIView):
|
class UserProfileDetail(APIView):
|
||||||
|
@ -48,4 +60,25 @@ class UserProfileDetail(APIView):
|
||||||
class UserProfileCreate(CreateAPIView):
|
class UserProfileCreate(CreateAPIView):
|
||||||
model = User
|
model = User
|
||||||
permission_classes = [permissions.AllowAny]
|
permission_classes = [permissions.AllowAny]
|
||||||
serializer_class = UserCreateSerializer
|
serializer_class = UserCreateSerializer
|
||||||
|
|
||||||
|
class FetchRobinhoodStocks(APIView):
|
||||||
|
def get(self, request, format=None):
|
||||||
|
login = r.login(os.getenv("ROBINHOOD_USER"), os.getenv("ROBINHOOD_PASS"))
|
||||||
|
stonks = r.build_holdings()
|
||||||
|
for key, value in stonks.items():
|
||||||
|
if Stock.objects.filter(uuid=value['id']).count() != 1:
|
||||||
|
stock = Stock.objects.create(user=request.user.profile, quantity=ceil(float(value['quantity'])), ticker=key, buy_price=value['price'], uuid=value['id'])
|
||||||
|
queryset = request.user.profile.stocks.all()
|
||||||
|
serializer = StockSerializer(queryset, many=True)
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
class BuyRobinhoodStock(APIView):
|
||||||
|
permission_classes = [permissions.AllowAny]
|
||||||
|
def get(self, request, format=None):
|
||||||
|
login = r.login(os.getenv("ROBINHOOD_USER"), os.getenv("ROBINHOOD_PASS"))
|
||||||
|
stonks = r.build_holdings()
|
||||||
|
for key, value in stonks.items():
|
||||||
|
print(key)
|
||||||
|
print(value)
|
||||||
|
return Response(stonks)
|
Loading…
Reference in New Issue
Block a user