Skip to content

Commit

Permalink
Script #20
Browse files Browse the repository at this point in the history
  • Loading branch information
pachCode committed Dec 4, 2018
1 parent 724e50b commit bb22f18
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
80 changes: 46 additions & 34 deletions speid/commands/spei.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
import os

from ast import literal_eval
import stpmex

from speid import app, db
from speid import db
from speid.models import Event
from speid.models import Transaction
from speid.tables.types import State
import stpmex
from stpmex.ordenes import Orden
from speid.daemon.tasks import execute_task


@click.command()
def re_execute_transactions():
"""Insert new random CLABEs into the db"""
@click.option('--speid_id', default=None, help='Specific speid id to execute')
def re_execute_transactions(speid_id):
"""Retry send a transaction to STP, it takes de values
of the event created before
"""
import pdb
pdb.set_trace()

stp_private_location = os.environ['STP_PRIVATE_LOCATION']
wsdl_path = os.environ['STP_WSDL']
Expand All @@ -28,34 +33,41 @@ def re_execute_transactions():
priv_key=private_key,
priv_key_passphrase=priv_key_passphrase,
prefijo=int(stp_prefijo))
if speid_id is None:
transactions = (db.session.query(Transaction)
.filter_by(estado='submitted'
, orden_id=None))
for transaction in transactions:
send_queue(transaction)
else:
transaction = (db.session.query(Transaction)
.filter_by(speid_id=speid_id,
estado='submitted',
orden_id=None)
.first())
send_queue(transaction)


def send_queue(transaction):
try:
event = db.session.query(Event). \
filter_by(
transaction_id=transaction.id,
type=State.created
).order_by(Event.created_at.desc()).first()

event_retry = Event(
transaction_id=transaction.id,
type=State.retry,
meta=event.meta
)
db.session.add(event_retry)
db.session.commit()
order_val = literal_eval(event.meta)
execute_task(order_val)
except Exception as exc:
print(exc)


transactions = db.session.query(Transaction)\
.filter_by(estado='submitted')
for transaction in transactions:
try:
event = db.session.query(Event).\
filter_by(
transaction_id=transaction.id,
type=State.created
).order_by(Event.created_at.desc()).first()

event_retry = Event(
transaction_id=transaction.id,
type=State.retry,
meta=event.meta
)
db.session.add(event_retry)
db.session.commit()
order_val = literal_eval(event.meta)
order = Orden(**order_val)

# Send order to STP
order.monto = order.monto / 100
res = order.registra()
print(res)
except Exception as exc:
print(exc)


if __name__ == '__main__':
if __name__ == "__main__":
re_execute_transactions()
4 changes: 3 additions & 1 deletion speid/daemon/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,22 @@ def execute(order_val):
(db.session
.query(Transaction)
.filter(Transaction.speid_id == order_val['speid_id'])
.one_or_none())
.first())

if previous_transaction is None:
# Save transaction
db.session.add(transaction)
db.session.commit()
else:
event_created.type = State.retry
transaction.id = previous_transaction.id
order.clave_rastreo = previous_transaction.clave_rastreo

event_created.transaction_id = transaction.id

# Send order to STP
order.monto = order.monto / 100

res = order.registra()

finally:
Expand Down
17 changes: 1 addition & 16 deletions test/custom_vcr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import vcr
import random


random_list = [1234567, 1234566, 1234569, 1234568]


def set_orden_id(request):
Expand All @@ -11,19 +7,8 @@ def set_orden_id(request):
return request


def set_orden_random_id(response):

if '<id>5554988</id>' in str(response['body']['string']):
random_int = str(random_list(random.randint(1, 4)))
body_content = str(response['body']['string'])\
.replace('5554988', random_int)
print(body_content)
response['body']['string'] = body_content
return response


my_vcr = vcr.VCR(
before_record_request=set_orden_id,
ignore_hosts={'sentry.io', 'stpmex.com', '127.0.0.1'},
ignore_hosts={'sentry.io', '127.0.0.1'},
ignore_localhost=True
)

0 comments on commit bb22f18

Please sign in to comment.