Ejemplo n.º 1
0
def update_source(request, source_id):
    source = get_object_or_404(Source, pk=source_id)
    form = AddHistoryForm(request.POST or None)
    if request.method == 'POST' and form.is_valid():
        new_history = History(
            source_id=source,
            author=IzumiUser.objects.get(user=request.user),
            date_created=timezone.now(),
            latitude=source.latitude,
            longitude=source.longitude,
            d_pathogen_pollution=form.cleaned_data.get('pathogen_pollution') -
            source.pathogen_pollution,
            d_inorganic_pollution=form.cleaned_data.get('inorganic_pollution')
            - source.inorganic_pollution,
            d_organic_pollution=form.cleaned_data.get('organic_pollution') -
            source.organic_pollution,
            d_macroscopic_pollution=form.cleaned_data.get(
                'macroscopic_pollution') - source.macroscopic_pollution,
            d_thermal_pollution=form.cleaned_data.get('thermal_pollution') -
            source.thermal_pollution,
            d_climate_condition=form.cleaned_data.get('climate_condition') -
            source.climate_condition,
            d_depletion_risk=form.cleaned_data.get('depletion_risk') -
            source.depletion_risk,
            d_stress=form.cleaned_data.get('stress') - source.stress,
        )
        new_history.save()
        source.last_updated = timezone.now()
        source.pathogen_pollution = form.cleaned_data.get('pathogen_pollution')
        source.inorganic_pollution = form.cleaned_data.get(
            'inorganic_pollution')
        source.organic_pollution = form.cleaned_data.get('organic_pollution')
        source.macroscopic_pollution = form.cleaned_data.get(
            'macroscopic_pollution')
        source.thermal_pollution = form.cleaned_data.get('thermal_pollution')
        source.climate_condition = form.cleaned_data.get('climate_condition')
        source.depletion_risk = form.cleaned_data.get('depletion_risk')
        source.stress = form.cleaned_data.get('stress')
        source.save()
        return HttpResponseRedirect(
            reverse('source_detail', kwargs={"source_id": source.id}))
    context = {'source': source, 'form': form}
    return render(request, "update_source.html", context)
Ejemplo n.º 2
0
def new_scrape():
		#When called will generate a new scrape, and write to csv file, and add to history
		#TODO Define these in another location
		RunScraper()
		#linkadd = jsonify(links)
		history = History()
		db.session.add(history)
		db.session.commit()
		#Writing file
		csv_file = open('scrape.csv', 'w')
		csv_writer = csv.writer(csv_file)
		csv_writer.writerow(['Links'])
		csv_writer.writerow([links])
		csv_file.close()
		return render_template('newscrape.html', links=links)
 def add_history(self, msg):
     h = History(log=msg, user=User.objects.first())
     h.save()
     res = self.get_object()
     res.history.add(h)
Ejemplo n.º 4
0
def submit_shortCover_fun(user_id, quantity, company, pending_price=None):

    user_portfolio = Portfolio.objects.get(user_id=user_id)
    no_trans = user_portfolio.no_trans

    #Checking if the Company exists
    stock_data_response = companyCheck(company)
    if stock_data_response['error']:
        return stock_data_response
    stock_data = stock_data_response['stock_data']
    current_price = Decimal(stock_data.current_price)

    #Checking if the user has any share of the company
    if (not TransactionShortSell.objects.filter(user_id=user_id,
                                                symbol=company).exists()):
        msg = "No quantity to Short cover"
        return {'msg': msg, 'error': True}

    transaction = TransactionShortSell.objects.get(user_id=user_id,
                                                   symbol=company)

    #Checking if the posted quantity is greater than the quantity user owns
    if (quantity == 0 or transaction.quantity - quantity < 0):
        msg = "Quantity error"
        return {'msg': msg, 'error': True}

    if (pending_price != None):
        if pending_price == current_price:
            msg = 'Pending price error'
            return {'msg': msg, 'error': True}
        pending_price = Decimal(pending_price)
        percentager = Decimal(0.05 * float(current_price))

        if (pending_price > current_price):
            msg = 'Pending Price for short cover should be less than current price'
            return {'msg': msg, 'error': True}
        else:
            p = Pending(user_id=user_id,
                        symbol=company,
                        buy_ss="SHORT COVER",
                        quantity=quantity,
                        value=pending_price,
                        time=datetime.datetime.now().time())
            p.save()
            msg = "You have made a Pending Order to " + "short cover" + " " + str(
                quantity
            ) + " shares of '" + company + "' at a Desired Price of'" + 'RS. ' + str(
                pending_price)
            return {'msg': msg, 'error': False}

    #SHORT COVER

    brokerage = calculateBrokerage(user_portfolio.no_trans, quantity,
                                   current_price)

    margin = (quantity * transaction.value) / 2
    user_portfolio.margin = user_portfolio.margin - margin
    user_portfolio.cash_bal = user_portfolio.cash_bal + (
        transaction.value - stock_data.current_price) * quantity + margin

    if (transaction.quantity == quantity):
        transaction.delete()
    else:
        transaction.quantity -= quantity
        transaction.save()

    user_portfolio.cash_bal -= brokerage
    user_portfolio.no_trans += 1
    user_portfolio.save()

    msg = "Success"

    history = History(user_id=user_id,
                      time=datetime.datetime.now().time(),
                      symbol=company,
                      buy_ss="SHORT COVER",
                      quantity=quantity,
                      price=stock_data.current_price)
    history.save()

    return {'msg': msg, 'error': False}
Ejemplo n.º 5
0
def submit_buy_fun(user_id, quantity, company, pending_price=None):

    #Checking if the Company exists
    stock_data_response = companyCheck(company)
    if stock_data_response['error']:
        return stock_data_response
    stock_data = stock_data_response['stock_data']
    current_price = stock_data.current_price
    user_portfolio = Portfolio.objects.get(user_id=user_id)
    no_trans = user_portfolio.no_trans
    margin = (user_portfolio.margin)

    if (quantity == 0):
        return {'msg': 'Quantity cannot be 0', 'error': True}

    if (pending_price != None):
        if pending_price == current_price:
            return {'msg': 'Pending price error', 'error': True}
        pending_price = Decimal(pending_price)
        percentager = Decimal(0.05 * float(current_price))
        t = current_price - percentager
        l = current_price + percentager
        q = False
        if (pending_price > current_price or pending_price <= t):
            q = True

        if (q):
            msg = 'Pending Price for Buying should be less than and maximum of 5% below Current Price'
            return {'msg': msg, 'error': True}
        # elif r:
        # 	return JsonResponse({'msg':'Pending Price for Short Selling should be greater than and maximum of 5% above Current Price'})
        else:
            p = Pending(user_id=user_id,
                        symbol=company,
                        buy_ss="BUY",
                        quantity=quantity,
                        value=pending_price,
                        time=datetime.datetime.now().time())
            p.save()
            msg = "You have made a Pending Order to " + "buy" + " " + str(
                quantity
            ) + " shares of '" + company + "' at a Desired Price of'" + 'RS. ' + str(
                pending_price)
            return {'msg': msg, 'error': False}

    #Brokerage
    brokerage = calculateBrokerage(no_trans, quantity, current_price)

    #Checking if the user has enough cash balance
    user_cash_balance = user_portfolio.cash_bal
    if (user_cash_balance - (quantity * current_price) - brokerage < 0):
        msg = "Not enough balance"
        return {'msg': msg, 'error': True}

    #===Executed only if the user has enough cash balance===#
    if TransactionBuy.objects.filter(user_id=user_id, symbol=company).exists():
        transaction = TransactionBuy.objects.get(user_id=user_id,
                                                 symbol=company)
        transaction.value = (current_price * quantity +
                             transaction.value * transaction.quantity) / (
                                 quantity + transaction.quantity)
        transaction.quantity += int(quantity)
        transaction.time = now = datetime.datetime.now()
        transaction.save()
    else:
        TransactionBuy.objects.create(
            user_id=user_id,
            symbol=company,
            quantity=quantity,
            value=current_price,
        )
    user_portfolio.cash_bal = user_cash_balance - (quantity * current_price)
    msg = "You have successfully bought {1} quantities of {2}".format(
        user_id, quantity, company)

    user_portfolio.cash_bal -= brokerage
    user_portfolio.no_trans += 1
    user_portfolio.save()

    history = History(user_id=user_id,
                      time=datetime.datetime.now().time(),
                      symbol=company,
                      buy_ss="BUY",
                      quantity=quantity,
                      price=stock_data.current_price)
    history.save()

    return {'msg': msg, 'error': False}