Exemple #1
0
    def get(self, request):
        currency = request.session.get('currency', BASE_CURRENCIES[0][0])
        stockdetail = request.session.get('stockdetail', STOCK_DETAILS[0][0])
        selected_portfolio = request.session.get('selected_portfolio', '')
        user = request.user
        # add Person class to user
        if user.is_authenticated:
            user.__class__ = Person

        portfolio_name = ''
        stocks = []

        if selected_portfolio:
            try:
                self.portfolio = Portfolio.objects.get(
                    user=user, portfolio_name=selected_portfolio)
                portfolio_name = self.portfolio.portfolio_name
                stocks = self.get_stock_info(GetStock.YES, currency)
                request.session['stock_info'] = json.dumps(
                    stocks, cls=DjangoJSONEncoder)

            except Portfolio.DoesNotExist:
                selected_portfolio = ''

        else:
            pass

        form = self.portfolio_form(user=user,
                                   initial={
                                       'symbol': '',
                                       'portfolio_name': portfolio_name,
                                       'portfolios': selected_portfolio,
                                       'currencies': currency,
                                       'stockdetails': stockdetail,
                                   })

        totals_values = format_totals_values(
            *self.td.calculate_stocks_value(stocks))
        stocks = add_display_tokens(stocks)
        stocks = format_and_sort_stocks(stocks)
        context = {
            'form': form,
            'stocks': stocks,
            'totals': totals_values,
            'source': source,
            'exchangerate': self.td.get_usd_euro_exchangerate(currency),
            'data_provider_url': self.data_provider_url,
        }
        return render(request, self.template_name, context)
Exemple #2
0
    def get(self, request):
        currency = request.session.get('currency', 'EUR')
        portfolios = request.session.get('selected_portfolio', '')
        user = request.user

        portfolio_name = ''
        stocks = []

        if portfolios:
            try:
                self.portfolio = Portfolio.objects.get(
                    user=user, portfolio_name=portfolios)
                portfolio_name = self.portfolio.portfolio_name
                stocks = self.get_stock_info(GetStock.YES, currency)
                request.session['stock_info'] = json.dumps(
                    stocks, cls=DjangoJSONEncoder)

            except Portfolio.DoesNotExist:
                portfolios = ''

        else:
            pass

        form = self.form_class(user=request.user,
                               initial={
                                   'symbol': '',
                                   'portfolio_name': portfolio_name,
                                   'portfolios': portfolios,
                                   'currencies': currency
                               })

        totals_values = format_totals_values(
            *self.wtd.calculate_stocks_value(stocks))
        stocks = add_display_tokens(stocks)
        stocks = format_and_sort_stocks(stocks)
        context = {
            'form': form,
            'stocks': stocks,
            'totals': totals_values,
            'source': source,
            'data_provider_url': self.data_provider_url,
        }
        return render(request, self.template_name, context)
Exemple #3
0
    def get(self, request):
        user = request.user
        if user.is_authenticated:
            # add Person class to user
            user.__class__ = Person

        try:
            default_user = Person.objects.get(username='******')

        except Person.DoesNotExist:
            default_user = None

        quote_string = request.session.get('quote_string', '')
        selected_portfolio = request.session.get('selected_portfolio', '')
        markets = request.session.get('markets', self.markets)
        stockdetail = request.session.get('stockdetail', STOCK_DETAILS[0][0])
        form = self.stockquote_form(
            initial={
                'quote_string': quote_string,
                'selected_portfolio': selected_portfolio,
                'markets': markets,
                'stockdetails': stockdetail,
            })
        portfolios = default_user.get_portfolio_names()

        if user.is_authenticated:
            portfolios += user.get_portfolio_names()

        if selected_portfolio:
            try:
                # try if user has selected a portfolio if authenticated
                if user.is_authenticated:
                    symbols = Portfolio.objects.get(
                        user=user,
                        portfolio_name=selected_portfolio).get_stock()
                    stock_info = self.td.get_stock_trade_info(symbols[0:20])
                    stock_info += self.td.get_stock_trade_info(symbols[20:40])

                else:
                    raise Portfolio.DoesNotExist

            except Portfolio.DoesNotExist:
                # try if it is a default portfolio
                try:
                    symbols = Portfolio.objects.get(
                        user=default_user, portfolio_name=selected_portfolio).get_stock()  #pylint: disable=line-too-long
                    stock_info = self.td.get_stock_trade_info(symbols[0:20])
                    stock_info += self.td.get_stock_trade_info(symbols[20:40])

                except Portfolio.DoesNotExist:
                    pass

        else:
            symbols = self.td.parse_stock_quote(quote_string, markets=markets)
            stock_info = self.td.get_stock_trade_info(symbols[0:20])

        stock_info = add_display_tokens(stock_info)
        stock_info = format_and_sort_stocks(stock_info)

        context = {
            'source': source,
            'stock_info': stock_info,
            'form': form,
            'portfolios': sorted(portfolios),
            'data_provider_url': self.data_provider_url,
        }

        return render(request, self.template_name, context)
Exemple #4
0
    def post(self, request):
        # TODO add refresh button

        self.request = request
        self.user = self.request.user
        # add Person class to user
        if self.user.is_authenticated:
            self.user.__class__ = Person

        currency = request.session.get('currency', 'EUR')

        form = self.form_class(self.request.POST, user=self.user)
        if form.is_valid():
            form_data = form.cleaned_data
            currency = form_data.get('currencies')
            self.selected_portfolio = form_data.get('portfolios')
            self.portfolio_name = form_data.get('portfolio_name')
            self.new_portfolio = form_data.get('new_portfolio')
            self.symbol = form_data.get('symbol').upper()
            self.btn1_pressed = form_data.get('btn1_pressed')
            self.change_qty_btn_pressed = form_data.get(
                'change_qty_btn_pressed')
            self.delete_symbol_btn_pressed = form_data.get(
                'delete_symbol_btn_pressed')
            self.previous_selected = request.session.get('selected_portfolio')
            previous_currency = request.session.get('currency')

            if (self.previous_selected != self.selected_portfolio
                    or previous_currency != currency):
                get_stock = GetStock.YES

            else:
                get_stock = GetStock.NO

            try:
                self.portfolio = Portfolio.objects.get(
                    user=self.user, portfolio_name=self.selected_portfolio)

            except Portfolio.DoesNotExist:
                self.portfolio = None
                self.selected_portfolio = ''
                self.btn1_pressed = ''
                self.btn2_pressed = ''
                get_stock = GetStock.EMPTY

            if self.new_portfolio:
                get_stock = self.create_new_portfolio()

            elif self.btn1_pressed:
                get_stock = self.rename_or_delete_portfolio_or_add_stock()

            elif self.change_qty_btn_pressed:
                get_stock = self.change_quantity_symbol()

            elif self.delete_symbol_btn_pressed:
                get_stock = self.delete_symbol()

            else:
                pass

            # set portfolio name except if the portfolio has been deleted or
            # does not exist
            try:
                self.portfolio_name = self.portfolio.portfolio_name

            except AttributeError:
                self.portfolio_name = ''

            stocks = self.get_stock_info(get_stock, currency)
            request.session['stock_info'] = json.dumps(stocks,
                                                       cls=DjangoJSONEncoder)
            request.session['selected_portfolio'] = self.selected_portfolio
            request.session['currency'] = currency

            form = self.form_class(user=self.user,
                                   initial={
                                       'portfolio_name': self.portfolio_name,
                                       'portfolios': self.selected_portfolio,
                                       'symbol': self.symbol,
                                       'currencies': currency
                                   })

            logger.info(
                f'user {self.user} [ip: {get_client_ip(self.request)}] '
                f'views {self.selected_portfolio}')

        else:
            form = self.form_class(user=self.user,
                                   initial={
                                       'portfolios': '',
                                       'portfolio_name': '',
                                       'symbol': '',
                                       'currencies': currency
                                   })

            stocks = []

        totals_values = format_totals_values(
            *self.td.calculate_stocks_value(stocks))
        stocks = add_display_tokens(stocks)
        stocks = format_and_sort_stocks(stocks)
        context = {
            'form': form,
            'stocks': stocks,
            'totals': totals_values,
            'source': source,
            'data_provider_url': self.data_provider_url,
        }

        return render(self.request, self.template_name, context)
Exemple #5
0
    def post(self, request):
        user = request.user
        try:
            default_user = User.objects.get(username='******')
        except User.DoesNotExist:
            default_user = None

        quote_string = request.session.get('quote_string', '')
        markets = request.session.get('markets', self.markets)

        form = self.form_class(request.POST)
        if form.is_valid():
            quote_string = form.cleaned_data.get('quote_string')
            selected_portfolio = form.cleaned_data.get('selected_portfolio')
            markets = form.cleaned_data.get('markets')
            symbols = []
            stock_info = []

            if selected_portfolio:
                try:
                    # try if user has selected a portfolio
                    portfolio = Portfolio.objects.filter(
                        user=user, portfolio_name=selected_portfolio)

                    for stock in portfolio.first().stocks.all():
                        symbols.append(stock.stock.symbol)

                    stock_info = self.wtd.get_stock_trade_info(symbols[0:20])
                    stock_info += self.wtd.get_stock_trade_info(symbols[20:40])

                except (TypeError, AttributeError):
                    # try if it is a default portfolio
                    try:
                        portfolio = Portfolio.objects.filter(
                            user=default_user,
                            portfolio_name=selected_portfolio)

                        for stock in portfolio.first().stocks.all():
                            symbols.append(stock.stock.symbol)

                        stock_info = self.wtd.get_stock_trade_info(
                            symbols[0:20])
                        stock_info += self.wtd.get_stock_trade_info(
                            symbols[20:40])

                    except AttributeError:
                        pass

            else:
                symbols = self.wtd.parse_stock_name(quote_string,
                                                    markets=markets)
                stock_info = self.wtd.get_stock_trade_info(symbols[0:20])

            if stock_info:
                stock_info = add_display_tokens(stock_info)
                stock_info = sorted(stock_info,
                                    key=lambda i: i['name'].lower())

            request.session['quote_string'] = quote_string
            request.session['markets'] = markets
            logger.info(
                f'user {request.user} [ip: {get_client_ip(request)}] looking '
                f'up: {quote_string}  / {selected_portfolio}')

        else:
            stock_info = []

        portfolios = [
            item.portfolio_name
            for item in Portfolio.objects.filter(user=default_user)
        ]

        if user.is_authenticated:
            portfolios += [
                item.portfolio_name
                for item in Portfolio.objects.filter(user=user)
            ]

        stock_info = add_display_tokens(stock_info)
        stock_info = format_and_sort_stocks(stock_info)
        context = {
            'source': source,
            'stock_info': stock_info,
            'form': form,
            'portfolios': sorted(portfolios),
            'data_provider_url': self.data_provider_url,
        }

        return render(request, self.template_name, context)
Exemple #6
0
    def post(self, request):
        user = request.user
        # add Person class to user
        if user.is_authenticated:
            user.__class__ = Person

        try:
            default_user = Person.objects.get(username='******')

        except Person.DoesNotExist:
            default_user = None

        quote_string = request.session.get('quote_string', '')
        markets = request.session.get('markets', self.markets)

        form = self.form_class(request.POST)
        if form.is_valid():
            quote_string = form.cleaned_data.get('quote_string')
            selected_portfolio = form.cleaned_data.get('selected_portfolio')
            markets = form.cleaned_data.get('markets')
            symbols = []
            stock_info = []

            if selected_portfolio:
                try:
                    # try if user has selected a portfolio if authenticated
                    if user.is_authenticated:
                        symbols = Portfolio.objects.get(
                            user=user, portfolio_name=selected_portfolio).get_stock()
                        stock_info = self.td.get_stock_trade_info(symbols[0:20])
                        stock_info += self.td.get_stock_trade_info(symbols[20:40])

                    else:
                        raise Portfolio.DoesNotExist

                except Portfolio.DoesNotExist:
                    # try if it is a default portfolio
                    try:
                        symbols = Portfolio.objects.get(
                            user=default_user, portfolio_name=selected_portfolio).get_stock()  #pylint: disable=line-too-long
                        stock_info = self.td.get_stock_trade_info(symbols[0:20])
                        stock_info += self.td.get_stock_trade_info(symbols[20:40])

                    except Portfolio.DoesNotExist:
                        pass

            else:
                symbols = self.td.parse_stock_quote(quote_string, markets=markets)
                stock_info = self.td.get_stock_trade_info(symbols[0:20])

            request.session['quote_string'] = quote_string
            request.session['markets'] = markets
            logger.info(f'user {user} [ip: {get_client_ip(request)}] looking '
                        f'up: {quote_string} / {selected_portfolio}')

        else:
            stock_info = []

        portfolios = default_user.get_portfolio_names()

        if user.is_authenticated:
            portfolios += user.get_portfolio_names()

        stock_info = add_display_tokens(stock_info)
        stock_info = format_and_sort_stocks(stock_info)
        context = {'source': source,
                   'stock_info': stock_info,
                   'form': form,
                   'portfolios': sorted(portfolios),
                   'data_provider_url': self.data_provider_url, }

        return render(request, self.template_name, context)