<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from decimal import Decimal
import json, logging

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render, get_object_or_404
from django.forms import ValidationError
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.conf import settings
from django.contrib import messages
from django.db.models import Q, Count

from valuenetwork.valueaccounting.models import EconomicResource, EconomicEvent, EconomicResourceType, EconomicAgent, Help
from valuenetwork.valueaccounting.forms import EconomicResourceForm
from valuenetwork.valueaccounting.service import ExchangeService
from faircoin import utils as faircoin_utils
from faircoin.forms import SendFairCoinsForm
from faircoin.decorators import confirm_password

FAIRCOIN_DIVISOR = faircoin_utils.FAIRCOIN_DIVISOR #Decimal("100000000.00")

WALLET = faircoin_utils.is_connected()

logger = logging.getLogger('ocp')

def get_agent(request):
    agent = None
    try:
        au = request.user.agent
        agent = au.agent
    except:
        pass
    return agent

def get_help(page_name):
    try:
        return Help.objects.get(page=page_name)
    except Help.DoesNotExist:
        return None

@login_required
def manage_faircoin_account(request, resource_id):
    resource = get_object_or_404(EconomicResource, id=resource_id)
    user_agent = get_agent(request)
    if not user_agent or not (resource.owner() == user_agent or resource.owner() in user_agent.managed_projects()):
        raise Http404

    send_coins_form = None
    is_wallet_address = None
    limit = 0
    confirmed_balance = None
    unconfirmed_balance = None
    faircoin_account = False
    payment_due = False
    share_price = False
    number_of_shares = False
    can_pay = False
    candidate_membership = None
    wallet = faircoin_utils.is_connected()
    if wallet:
        is_wallet_address = faircoin_utils.is_mine(resource.faircoin_address.address)
        if is_wallet_address:
            send_coins_form = SendFairCoinsForm(agent=resource.owner())
            try:
                balances = faircoin_utils.get_address_balance(resource.faircoin_address.address)
                unconfirmed_balance =  Decimal(balances[1]) / FAIRCOIN_DIVISOR
                unconfirmed_balance += resource.balance_in_tx_state_new()
                confirmed_balance = Decimal(balances[0]) / FAIRCOIN_DIVISOR
                if unconfirmed_balance < 0:
                    confirmed_balance += unconfirmed_balance
                elif unconfirmed_balance == 0:
                    unconfirmed_balance = confirmed_balance
            except:
                confirmed_balance = "Not accessible now"
                unconfirmed_balance = "Not accessible now"
        else:
            wallet = False
            if resource.is_address_requested(): is_wallet_address = True

    netfee = faircoin_utils.network_fee_fairs()
    project = None
    jn_req = None
    for req in resource.owner().project_join_requests.all():
      #candidate_membership = resource.owner().candidate_membership(req.project.agent)
      if req.pending_shares(): #candidate_membership:
        obj = req.payment_option()
        faircoin_account = resource.owner().faircoin_resource()
        shacct = req.project.shares_account_type()
        if faircoin_account and wallet and obj and obj['key'] == 'faircoin' and shacct:
            share = req.project.shares_type() #EconomicResourceType.objects.membership_share()
            #share_price = faircoin_utils.share_price_in_fairs(req)
            number_of_shares = req.pending_shares() #resource.owner().number_of_shares()
            share_price = req.payment_pending_amount() + netfee #share_price * number_of_shares
            project = req.project
            jn_req = req
            payment_due = True
            if resource.owner().owns_resource_of_type(shacct) and share_price == 0:
                payment_due = False
            if confirmed_balance and confirmed_balance != "Not accessible now":
                can_pay = confirmed_balance >= share_price
            break
        elif request.user.is_superuser:
            logger.warning("(debug) pro:"+str(req.project.agent)+" fair_account:"+str(faircoin_account)+" wallet:"+str(wallet)+" obj:"+str(obj)+" shares_account_type:"+str(shacct))
            messages.warning(request, "(debug) pro:"+str(req.project.agent)+" fair_account:"+str(faircoin_account)+" wallet:"+str(wallet)+" obj:"+str(obj)+" shares_account_type:"+str(shacct))

    return render(request, "faircoin/faircoin_account.html", {
        "resource": resource,
        "photo_size": (128, 128),
        "agent": resource.owner(),
        "wallet": wallet,
        "send_coins_form": send_coins_form,
        "is_wallet_address": is_wallet_address,
        "confirmed_balance": confirmed_balance,
        "unconfirmed_balance": unconfirmed_balance,
        "faircoin_account": faircoin_account,
        "candidate_membership": candidate_membership,
        "payment_due": payment_due,
        "share_price": share_price,
        "number_of_shares": number_of_shares,
        "can_pay": can_pay,
        "project": project,
        "jn_req": jn_req,<commit_msg>fix decimal err<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from decimal import Decimal
import json, logging

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render, get_object_or_404
from django.forms import ValidationError
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.conf import settings
from django.contrib import messages
from django.db.models import Q, Count

from valuenetwork.valueaccounting.models import EconomicResource, EconomicEvent, EconomicResourceType, EconomicAgent, Help
from valuenetwork.valueaccounting.forms import EconomicResourceForm
from valuenetwork.valueaccounting.service import ExchangeService
from faircoin import utils as faircoin_utils
from faircoin.forms import SendFairCoinsForm
from faircoin.decorators import confirm_password

FAIRCOIN_DIVISOR = faircoin_utils.FAIRCOIN_DIVISOR #Decimal("100000000.00")

WALLET = faircoin_utils.is_connected()

logger = logging.getLogger('ocp')

def get_agent(request):
    agent = None
    try:
        au = request.user.agent
        agent = au.agent
    except:
        pass
    return agent

def get_help(page_name):
    try:
        return Help.objects.get(page=page_name)
    except Help.DoesNotExist:
        return None

@login_required
def manage_faircoin_account(request, resource_id):
    resource = get_object_or_404(EconomicResource, id=resource_id)
    user_agent = get_agent(request)
    if not user_agent or not (resource.owner() == user_agent or resource.owner() in user_agent.managed_projects()):
        raise Http404

    send_coins_form = None
    is_wallet_address = None
    limit = 0
    confirmed_balance = None
    unconfirmed_balance = None
    faircoin_account = False
    payment_due = False
    share_price = False
    number_of_shares = False
    can_pay = False
    candidate_membership = None
    wallet = faircoin_utils.is_connected()
    if wallet:
        is_wallet_address = faircoin_utils.is_mine(resource.faircoin_address.address)
        if is_wallet_address:
            send_coins_form = SendFairCoinsForm(agent=resource.owner())
            try:
                balances = faircoin_utils.get_address_balance(resource.faircoin_address.address)
                unconfirmed_balance =  Decimal(balances[1]) / FAIRCOIN_DIVISOR
                unconfirmed_balance += resource.balance_in_tx_state_new()
                confirmed_balance = Decimal(balances[0]) / FAIRCOIN_DIVISOR
                if unconfirmed_balance < 0:
                    confirmed_balance += unconfirmed_balance
                elif unconfirmed_balance == 0:
                    unconfirmed_balance = confirmed_balance
            except:
                confirmed_balance = "Not accessible now"
                unconfirmed_balance = "Not accessible now"
        else:
            wallet = False
            if resource.is_address_requested(): is_wallet_address = True

    netfee = faircoin_utils.network_fee_fairs()
    project = None
    jn_req = None
    for req in resource.owner().project_join_requests.all():
      #candidate_membership = resource.owner().candidate_membership(req.project.agent)
      if req.pending_shares(): #candidate_membership:
        obj = req.payment_option()
        faircoin_account = resource.owner().faircoin_resource()
        shacct = req.project.shares_account_type()
        if faircoin_account and wallet and obj and obj['key'] == 'faircoin' and shacct:
            share = req.project.shares_type() #EconomicResourceType.objects.membership_share()
            #share_price = faircoin_utils.share_price_in_fairs(req)
            number_of_shares = req.pending_shares() #resource.owner().number_of_shares()
            share_price = Decimal(req.payment_pending_amount()) + netfee #share_price * number_of_shares
            project = req.project
            jn_req = req
            payment_due = True
            if resource.owner().owns_resource_of_type(shacct) and share_price == 0:
                payment_due = False
            if confirmed_balance and confirmed_balance != "Not accessible now":
                can_pay = confirmed_balance >= share_price
            break
        elif request.user.is_superuser:
            logger.warning("(debug) pro:"+str(req.project.agent)+" fair_account:"+str(faircoin_account)+" wallet:"+str(wallet)+" obj:"+str(obj)+" shares_account_type:"+str(shacct))
            messages.warning(request, "(debug) pro:"+str(req.project.agent)+" fair_account:"+str(faircoin_account)+" wallet:"+str(wallet)+" obj:"+str(obj)+" shares_account_type:"+str(shacct))

    return render(request, "faircoin/faircoin_account.html", {
        "resource": resource,
        "photo_size": (128, 128),
        "agent": resource.owner(),
        "wallet": wallet,
        "send_coins_form": send_coins_form,
        "is_wallet_address": is_wallet_address,
        "confirmed_balance": confirmed_balance,
        "unconfirmed_balance": unconfirmed_balance,
        "faircoin_account": faircoin_account,
        "candidate_membership": candidate_membership,
        "payment_due": payment_due,
        "share_price": share_price,
        "number_of_shares": number_of_shares,
        "can_pay": can_pay,
        "project": project,
        "jn_req": jn_req,
{'fraction_non_alphanumeric': 0.08596736596736597, 'fraction_numerical': 0.006713286713286713, 'mean_word_length': 3.298997995991984, 'pattern_counts': {'":': 34, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2593674', 'n_tokens_mistral': 3182, 'n_tokens_neox': 3051, 'n_words': 645}