def post_create(request, topic_pk): topic = get_object_or_404(Topic, pk=topic_pk) perm_manager = permissions.manager_for(request.user) if not perm_manager.can_create_post(topic): return HttpResponseForbidden() if request.method == 'POST': with transaction.autocommit(): post = Post(topic=topic, author=request.user) form = PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) post.ip = request.META.get('REMOTE_ADDR') post.save() posts_count = topic.posts.exclude(is_deleted=True).count() topic.response_count = posts_count - 1 topic.updated = post.created topic.content_updated = post.created topic.save() cache.expire_groups(( 'topic:all', 'topic:{}'.format(topic.pk), )) return redirect(post.get_absolute_url()) else: form = PostForm() ctx = {'topic': topic, 'form': form} return render(request, 'forum/post_create.html', ctx)
def apply_async(cls, *args, **kwargs): # Delay the task unless the client requested otherwise or transactions # aren't being managed (i.e. the signal handlers won't send the task). # A rather roundabout way of allowing control of transaction behaviour from source. I'm sure there's a better way. after_transaction = True if len(args) > 1: if isinstance(args[1], dict): after_transaction = args[1].pop('after_transaction', True) if 'after_transaction' in kwargs: after_transaction = kwargs.pop('after_transaction') if transaction.is_managed() and after_transaction: if not transaction.is_dirty(): # Always mark the transaction as dirty # because we push task in queue that must be fired or discarded if 'using' in kwargs: transaction.set_dirty(using=kwargs['using']) else: transaction.set_dirty() _get_task_queue().append((cls, args, kwargs)) else: apply_async_orig = cls.original_apply_async if current_app.conf.CELERY_ALWAYS_EAGER: apply_async_orig = transaction.autocommit()(apply_async_orig) return apply_async_orig(*args, **kwargs)
def new_bitcoin_address(): while True: with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() bp = BitcoinAddress.objects.filter(Q(active=False) & Q(wallet__isnull=True) & \ Q(least_received__lte=0)) if len(bp) < 1: refill_payment_queue() db_transaction.commit() print "refilling queue...", bp else: bp = bp[0] sql = """ UPDATE "django_bitcoin_bitcoinaddress" SET "active" = true WHERE "django_bitcoin_bitcoinaddress"."id" IN ( SELECT U0."id" FROM "django_bitcoin_bitcoinaddress" U0 WHERE (U0."id" = %s AND U0."active" = false AND U0."wallet_id" IS NULL AND U0."least_received" <= 0 ) FOR UPDATE) """ cursor = connection.cursor() cursor.execute(sql, [bp.id]) updated = cursor.rowcount if updated and updated != -1: return bp else: print "wallet transaction concurrency:", bp.address
def _autocommit(func): @wraps(func) def __autocommit(*args, **kwargs): # TODO: Testing autocommit on views isn't very useful and # significantly slows down the test suite. Instead, write # some better tests for this decorator. if getattr(settings, 'TEST', False): return func(*args, **kwargs) if callable(conditional) and not conditional(*args, **kwargs): return func(*args, **kwargs) if isinstance(using, basestring): using_list = [using] else: using_list = using for alias in using_list: connection = connections[using] connection.set_autocommit() try: return func(*args, **kwargs) except Exception: raise finally: for alias in using_list: connection = connections[using] connection.set_default_commit() return transaction.autocommit(__autocommit)
def send_to_address(self, address, amount, description=''): if settings.BITCOIN_DISABLE_OUTGOING: raise Exception("Outgoing transactions disabled! contact support.") address = address.strip() if type(amount) != Decimal: amount = Decimal(amount) amount = amount.quantize(Decimal('0.00000001')) if not is_valid_btc_address(str(address)): raise Exception(_("Not a valid bitcoin address") + ":" + address) if amount <= 0: raise Exception(_("Can't send zero or negative amounts")) # concurrency check with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() avail = self.total_balance() updated = Wallet.objects.filter(Q(id=self.id)).update(last_balance=avail) if amount > avail: raise Exception(_("Trying to send too much")) new_balance = avail - amount updated = Wallet.objects.filter(Q(id=self.id) & Q(transaction_counter=self.transaction_counter) & Q(last_balance=avail) )\ .update(last_balance=new_balance, transaction_counter=self.transaction_counter+1) if not updated: print "address transaction concurrency:", new_balance, avail, self.transaction_counter, self.last_balance, self.total_balance() raise Exception(_("Concurrency error with transactions. Please try again.")) # concurrency check end bwt = WalletTransaction.objects.create( amount=amount, from_wallet=self, to_bitcoinaddress=address, description=description) try: result = bitcoind.send(address, amount) except jsonrpc.JSONRPCException: bwt.delete() raise self.transaction_counter = self.transaction_counter+1 self.last_balance = new_balance # check if a transaction fee exists, and deduct it from the wallet # TODO: because fee can't be known beforehand, can result in negative wallet balance. # currently isn't much of a issue, but might be in the future, depending of the application transaction = bitcoind.gettransaction(result) fee_transaction = None total_amount = amount if Decimal(transaction['fee']) < Decimal(0): fee_transaction = WalletTransaction.objects.create( amount=Decimal(transaction['fee']) * Decimal(-1), from_wallet=self) total_amount += fee_transaction.amount if settings.BITCOIN_TRANSACTION_SIGNALING: balance_changed.send(sender=self, changed=(Decimal(-1) * total_amount), transaction=bwt) balance_changed_confirmed.send(sender=self, changed=(Decimal(-1) * total_amount), transaction=bwt) return (bwt, fee_transaction)
def test_autocommit_context_manager_with_using(self): """ The autocommit context manager also works with a using argument. """ with self.assertRaises(Exception), transaction.autocommit(using="default"): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1)
def test_autocommit_decorator_with_using(self): """ The autocommit decorator also works with a using argument. """ autocomitted_create_then_fail = transaction.autocommit(using="default")(self.create_a_reporter_then_fail) self.assertRaises(Exception, autocomitted_create_then_fail, "Alice", "Smith") # Again, the object created before the exception still exists self.assertEqual(Reporter.objects.count(), 1)
def test_autocommit_decorator(self): """ The autocommit decorator works exactly the same as the default behavior. """ autocomitted_create_then_fail = transaction.autocommit(self.create_a_reporter_then_fail) self.assertRaises(Exception, autocomitted_create_then_fail, "Alice", "Smith") # Again, the object created before the exception still exists self.assertEqual(Reporter.objects.count(), 1)
def send_to_wallet(self, otherWallet, amount, description=''): if type(amount) != Decimal: amount = Decimal(amount) amount = amount.quantize(Decimal('0.00000001')) with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() if settings.BITCOIN_UNCONFIRMED_TRANSFERS: avail = self.total_balance_unconfirmed() else: avail = self.total_balance() updated = Wallet.objects.filter(Q(id=self.id)).update(last_balance=avail) if self == otherWallet: raise Exception(_("Can't send to self-wallet")) if not otherWallet.id or not self.id: raise Exception(_("Some of the wallets not saved")) if amount <= 0: raise Exception(_("Can't send zero or negative amounts")) if amount > avail: raise Exception(_("Trying to send too much")) # concurrency check new_balance = avail - amount updated = Wallet.objects.filter(Q(id=self.id) & Q(transaction_counter=self.transaction_counter) & Q(last_balance=avail))\ .update(last_balance=new_balance, transaction_counter=self.transaction_counter+1) if not updated: print "wallet transaction concurrency:", new_balance, avail, self.transaction_counter, self.last_balance, self.total_balance() raise Exception(_("Concurrency error with transactions. Please try again.")) # db_transaction.commit() # concurrency check end transaction = WalletTransaction.objects.create( amount=amount, from_wallet=self, to_wallet=otherWallet, description=description) # db_transaction.commit() self.transaction_counter = self.transaction_counter+1 self.last_balance = new_balance # updated = Wallet.objects.filter(Q(id=otherWallet.id))\ # .update(last_balance=otherWallet.total_balance()) if settings.BITCOIN_TRANSACTION_SIGNALING: balance_changed.send(sender=self, changed=(Decimal(-1) * amount), transaction=transaction) balance_changed.send(sender=otherWallet, changed=(amount), transaction=transaction) balance_changed_confirmed.send(sender=self, changed=(Decimal(-1) * amount), transaction=transaction) balance_changed_confirmed.send(sender=otherWallet, changed=(amount), transaction=transaction) updated = Wallet.objects.filter(Q(id=otherWallet.id))\ .update(last_balance=otherWallet.total_balance_sql()) if not updated: print "otherWallet failed updating", new_balance, avail return transaction
def forwards(self, orm): "Write your forwards methods here." if getattr(connection.features, 'autocommits_when_autocommit_is_off', False): # likely sqlite3 with django 1.6 and above with transaction.autocommit(): self.add_account_verified_permission(orm) else: self.add_account_verified_permission(orm)
def test_autocommit_context_manager(self): """ The autocommit context manager works exactly the same as the default behavior. """ with self.assertRaises(Exception), transaction.autocommit(): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1)
def test_autocommit_context_manager_with_using(self): """ The autocommit context manager also works with a using argument. """ with self.assertRaises(Exception): with transaction.autocommit(using="default"): self.create_reporter_and_fail() self.assertEqual(Reporter.objects.count(), 1)
def send_to_address(self, address, amount, description=''): if settings.BITCOIN_DISABLE_OUTGOING: raise Exception("Outgoing transactions disabled! contact support.") address = address.strip() if type(amount) != Decimal: amount = Decimal(amount) amount = amount.quantize(Decimal('0.00000001')) if not is_valid_btc_address(str(address)): raise Exception(_("Not a valid bitcoin address") + ":" + address) if amount <= 0: raise Exception(_("Can't send zero or negative amounts")) # concurrency check with db_transaction.autocommit(): avail = self.total_balance() updated = Wallet.objects.filter(Q(id=self.id)).update(last_balance=avail) if amount > avail: raise Exception(_("Trying to send too much")) new_balance = avail - amount updated = Wallet.objects.filter(Q(id=self.id) & Q(transaction_counter=self.transaction_counter) & Q(last_balance=avail) )\ .update(last_balance=new_balance, transaction_counter=self.transaction_counter+1) if not updated: print "address transaction concurrency:", new_balance, avail, self.transaction_counter, self.last_balance, self.total_balance() raise Exception(_("Concurrency error with transactions. Please try again.")) # concurrency check end bwt = WalletTransaction.objects.create( amount=amount, from_wallet=self, to_bitcoinaddress=address, description=description) try: result = bitcoind.send(address, amount) except jsonrpc.JSONRPCException: bwt.delete() raise self.transaction_counter = self.transaction_counter+1 self.last_balance = new_balance # check if a transaction fee exists, and deduct it from the wallet # TODO: because fee can't be known beforehand, can result in negative wallet balance. # currently isn't much of a issue, but might be in the future, depending of the application transaction = bitcoind.gettransaction(result) fee_transaction = None total_amount = amount if Decimal(transaction['fee']) < Decimal(0): fee_transaction = WalletTransaction.objects.create( amount=Decimal(transaction['fee']) * Decimal(-1), from_wallet=self) total_amount += fee_transaction.amount if settings.BITCOIN_TRANSACTION_SIGNALING: balance_changed.send(sender=self, changed=(Decimal(-1) * total_amount), transaction=bwt) balance_changed_confirmed.send(sender=self, changed=(Decimal(-1) * total_amount), transaction=bwt) return (bwt, fee_transaction)
def send_to_wallet(self, otherWallet, amount, description=''): if type(amount) != Decimal: amount = Decimal(amount) amount = amount.quantize(Decimal('0.00000001')) with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() if settings.BITCOIN_UNCONFIRMED_TRANSFERS: avail = self.total_balance_unconfirmed() else: avail = self.total_balance() updated = Wallet.objects.filter(Q(id=self.id)).update(last_balance=avail) if self == otherWallet: raise Exception(_("Can't send to self-wallet")) if not otherWallet.id or not self.id: raise Exception(_("Some of the wallets not saved")) if amount <= 0: raise Exception(_("Can't send zero or negative amounts")) if amount > avail: raise Exception(_("Trying to send too much")) # concurrency check new_balance = avail - amount updated = Wallet.objects.filter(Q(id=self.id) & Q(transaction_counter=self.transaction_counter) & Q(last_balance=avail))\ .update(last_balance=new_balance, transaction_counter=self.transaction_counter+1) if not updated: print "wallet transaction concurrency:", new_balance, avail, self.transaction_counter, self.last_balance, self.total_balance() raise Exception(_("Concurrency error with transactions. Please try again.")) # db_transaction.commit() # concurrency check end transaction = WalletTransaction.objects.create( amount=amount, from_wallet=self, to_wallet=otherWallet, description=description) # db_transaction.commit() self.transaction_counter = self.transaction_counter+1 self.last_balance = new_balance # updated = Wallet.objects.filter(Q(id=otherWallet.id))\ # .update(last_balance=otherWallet.total_balance_sql()) otherWallet.update_last_balance(amount) if settings.BITCOIN_TRANSACTION_SIGNALING: balance_changed.send(sender=self, changed=(Decimal(-1) * amount), transaction=transaction) balance_changed.send(sender=otherWallet, changed=(amount), transaction=transaction) balance_changed_confirmed.send(sender=self, changed=(Decimal(-1) * amount), transaction=transaction) balance_changed_confirmed.send(sender=otherWallet, changed=(amount), transaction=transaction) db_transaction.commit() return transaction
def test_autocommit_decorator(self): """ The autocommit decorator works exactly the same as the default behavior. """ autocomitted_create_then_fail = transaction.autocommit( self.create_a_reporter_then_fail) self.assertRaises(Exception, autocomitted_create_then_fail, "Alice", "Smith") # Again, the object created before the exception still exists self.assertEqual(Reporter.objects.count(), 1)
def test_autocommit_decorator_with_using(self): """ The autocommit decorator also works with a using argument. """ autocomitted_create_then_fail = transaction.autocommit( using='default')(self.create_a_reporter_then_fail) self.assertRaises(Exception, autocomitted_create_then_fail, "Alice", "Smith") # Again, the object created before the exception still exists self.assertEqual(Reporter.objects.count(), 1)
def handle(self, *args, **options): """ """ if not args: num = settings.INVITATION_BATCH else: num = args[0] invites = Beta.objects\ .filter(Q(is_invited=False) | Q(priority=True))\ .order_by('-priority', 'pk')[:num] # open the SMTP connection once. connection = mail.get_connection() connection.open() with transaction.autocommit(): subject = render_to_string('beta/invitation-subject.txt')\ .strip('\n') url = 'http://{0}'.format(Site.objects.get_current().domain) for invite in invites: hash_exists = False while not hash_exists: hash_ = str(uuid4()) try: Beta.objects.get(invite_hash=hash_) except Beta.DoesNotExist: invite.invite_hash = hash_ hash_exists = True ctx = { 'invite': invite, 'url': '{0}{1}'.format( url, reverse(options['view'], kwargs={'hash': hash_})) } message = render_to_string('beta/invitation-email.txt', ctx)\ .strip('\n') email = mail.EmailMessage( subject=subject, body=message, to=[invite.email], connection=connection ) email.send() invite.date_sent = timezone.now() invite.is_invited = True invite.save() connection.close() self.stdout.write('%s invitations sent!' % invites.count())
def test_commit_on_success_exit(self): with transaction.autocommit(): with transaction.commit_on_success(): Reporter.objects.create(first_name="Bobby", last_name="Tables") # Much more formal r = Reporter.objects.get() r.first_name = "Robert" r.save() r = Reporter.objects.get() self.assertEqual(r.first_name, "Robert")
def _send_tasks(**kwargs): """Sends all delayed Celery tasks. Called after a transaction is committed or we leave a transaction management block in which no changes were made (effectively a commit). """ queue = _get_task_queue() while queue: cls, args, kwargs = queue.pop(0) apply_async_orig = cls.original_apply_async if current_app.conf.CELERY_ALWAYS_EAGER: apply_async_orig = transaction.autocommit()(apply_async_orig) apply_async_orig(*args, **kwargs)
def apply_async(cls, *args, **kwargs): # Delay the task unless the client requested otherwise or transactions # aren't being managed (i.e. the signal handlers won't send the task). after_transaction = kwargs.pop("after_transaction", True) delay_task = after_transaction and transaction.is_managed() if delay_task: _get_task_queue().append((cls, args, kwargs)) else: apply_async_orig = super(PostTransactionTask, cls).apply_async if current_app.conf.CELERY_ALWAYS_EAGER: apply_async_orig = transaction.autocommit()(apply_async_orig) return apply_async_orig(*args, **kwargs)
def forwards(self, orm): feeds = Feed.objects.filter(hash_address_and_link__isnull=True).order_by('-average_stories_per_month') feed_count = feeds.count() i = 0 with transaction.autocommit(): for feed in feeds: i += 1 if i % 1000 == 0: print "%s/%s" % (i, feed_count,) sys.stdout.flush() if not feed.hash_address_and_link: try: feed.save() except Exception, e: print '\n\n!!! %s: %s\n\n' % (i, e) continue
def go_users(): print "Migrating Users" with open('/tmp/users.json', 'r') as user_file: for i, line in enumerate(user_file): print "Migrating user %d" % i try: jdata = json.loads(line) with transaction.autocommit(): user = OAUser.objects.create(name=jdata['name'], email=jdata['email'], profession=jdata['profession'], mailinglist=jdata['mailinglist'], slug=jdata['_id'],) user.save() except Exception, e: print "Can't migrate user. Data: [%s] error: %s" % (jdata, e) transaction.rollback()
def _send_tasks(**kwargs): """Sends all delayed Celery tasks. Called after a transaction is committed or we leave a transaction management block in which no changes were made (effectively a commit). """ queue = _get_task_queue() while queue: cls, args, kwargs = queue.pop(0) apply_async_orig = cls.original_apply_async if current_app.conf.CELERY_ALWAYS_EAGER: apply_async_orig = transaction.autocommit()(apply_async_orig) apply_async_orig(*args, **kwargs) # TODO: sometime fails when transaction.is_managed() == True, added transaction.autocommit() in these cases # I don't known reason but with this hack it seems works if not current_app.conf.CELERY_ALWAYS_EAGER and transaction.is_managed(): transaction.commit.original()
def go_users(): print "Migrating Users" with open('/tmp/users.json', 'r') as user_file: for i, line in enumerate(user_file): print "Migrating user %d" % i try: jdata = json.loads(line) with transaction.autocommit(): user = OAUser.objects.create( name=jdata['name'], email=jdata['email'], profession=jdata['profession'], mailinglist=jdata['mailinglist'], slug=jdata['_id'], ) user.save() except Exception, e: print "Can't migrate user. Data: [%s] error: %s" % (jdata, e) transaction.rollback()
def new_bitcoin_address(): while True: with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() bp = BitcoinAddress.objects.filter(Q(active=False) & Q(wallet__isnull=True) & \ Q(least_received__lte=0)) if len(bp) < 1: refill_payment_queue() db_transaction.commit() print "refilling queue...", bp else: bp = bp[0] updated = BitcoinAddress.objects.select_for_update().filter(Q(id=bp.id) & Q(active=False) & Q(wallet__isnull=True) & \ Q(least_received__lte=0)).update(active=True) db_transaction.commit() if updated: return bp else: print "wallet transaction concurrency:", bp.address
def _send_tasks(**kwargs): """Sends all delayed Celery tasks. Called after a transaction is committed or we leave a transaction management block in which no changes were made (effectively a commit). """ celery_eager = _get_celery_settings('CELERY_ALWAYS_EAGER') queue = _get_task_queue() while queue: tsk, args, kwargs = queue.pop(0) if django.VERSION < (1, 6): apply_async_orig = tsk.original_apply_async if celery_eager: apply_async_orig = transaction.autocommit()(apply_async_orig) apply_async_orig(*args, **kwargs) else: tsk.original_apply_async(*args, **kwargs)
def forwards(self, orm): feeds = Feed.objects.filter(hash_address_and_link__isnull=True ).order_by('-average_stories_per_month') feed_count = feeds.count() i = 0 with transaction.autocommit(): for feed in feeds: i += 1 if i % 1000 == 0: print "%s/%s" % ( i, feed_count, ) sys.stdout.flush() if not feed.hash_address_and_link: try: feed.save() except Exception, e: print '\n\n!!! %s: %s\n\n' % (i, e) continue
def go_events(): with open('/tmp/events.json', 'r') as event_file: for i, line in enumerate(event_file): print "Migrating event %d" % i try: jdata = json.loads(line) with transaction.autocommit(): evt = OAEvent.objects.create(location=jdata['location'], coords=jdata['coords'], accessed=dateutil.parser.parse(jdata['accessed']), doi=jdata['doi'], url=jdata['url'], story=jdata.get('story', ''), description=jdata.get('description', ''), user_slug=jdata['user_id'], user_email='', user_name=jdata['user_name'], user_profession=jdata['user_profession'],) evt.save() except Exception, e: print "Can't migrate event. Data: [%s] error: %s" % (jdata, e) transaction.rollback()
def go_events(): with open('/tmp/events.json', 'r') as event_file: for i, line in enumerate(event_file): print "Migrating event %d" % i try: jdata = json.loads(line) with transaction.autocommit(): evt = OAEvent.objects.create( location=jdata['location'], coords=jdata['coords'], accessed=dateutil.parser.parse(jdata['accessed']), doi=jdata['doi'], url=jdata['url'], story=jdata.get('story', ''), description=jdata.get('description', ''), user_slug=jdata['user_id'], user_email='', user_name=jdata['user_name'], user_profession=jdata['user_profession'], ) evt.save() except Exception, e: print "Can't migrate event. Data: [%s] error: %s" % (jdata, e) transaction.rollback()
def notifica(): from aula.apps.alumnes.models import Alumne from django.db import transaction from django.core.exceptions import ObjectDoesNotExist from django.db.models import Q from datetime import timedelta from django.utils.datetime_safe import datetime from aula.apps.presencia.models import EstatControlAssistencia from aula.apps.presencia.models import ControlAssistencia from django.core.mail import send_mail from aula.apps.usuaris.models import Accio urlDjangoAula = settings.URL_DJANGO_AULA urlVideoTutorial = "- No disponible -" with transaction.autocommit(): #actualitzo notificacions sortides: notifica_sortides() #Notificacions ara = datetime.now() fa_2_setmanes = ara - timedelta(days=14) presencies_notificar = EstatControlAssistencia.objects.filter( codi_estat__in=['F', 'R', 'J']) q_no_es_baixa = Q(data_baixa__gte=ara) | Q(data_baixa__isnull=True) q_no_informat_adreca = Q(correu_relacio_familia_pare='') & Q( correu_relacio_familia_mare='') llista_alumnes = Alumne.objects.filter(q_no_es_baixa).exclude( q_no_informat_adreca).values_list('pk', flat=True) for alumne_id in llista_alumnes: try: alumne = Alumne.objects.get(pk=alumne_id) fa_n_dies = ara - timedelta(days=alumne.periodicitat_faltes) noves_sortides = NotificaSortida.objects.filter( alumne=alumne, relacio_familia_notificada__isnull=True) noves_incidencies = alumne.incidencia_set.filter( relacio_familia_notificada__isnull=True) noves_expulsions = alumne.expulsio_set.exclude( estat='ES').filter(relacio_familia_notificada__isnull=True) noves_sancions = alumne.sancio_set.filter( impres=True, relacio_familia_notificada__isnull=True) noves_faltes_assistencia = ControlAssistencia.objects.filter( alumne=alumne, impartir__dia_impartir__gte=fa_2_setmanes, relacio_familia_notificada__isnull=True, estat__pk__in=presencies_notificar) hiHaNovetatsPresencia = False hiHaNovetats = False #comprovo si hi ha novetats de presencia i incidències fa_dies_que_no_notifiquem = alumne.relacio_familia_darrera_notificacio is None or \ alumne.relacio_familia_darrera_notificacio < fa_n_dies hiHaNovetatsPresencia = alumne.periodicitat_faltes > 0 and \ fa_dies_que_no_notifiquem and \ noves_faltes_assistencia.exists() hiHaNovetats = ( hiHaNovetatsPresencia or noves_sortides.exists() or (alumne.periodicitat_incidencies and (noves_incidencies.exists() or noves_expulsions.exists() or noves_sancions.exists()))) #print u'Avaluant a {0}'.format( alumne ) enviatOK = False if hiHaNovetats: #enviar correu i marcar novetats com a notificades: missatge = [ u"Portal d'informació a les famílies de " + settings.NOM_CENTRE, u"", u"Teniu novetats de {0} al portal de relació famílies {1}" .format(alumne.nom, urlDjangoAula), u"", u"Recordeu que el vostre nom d'usuari és: {0}".format( alumne.get_user_associat().username), u"", u"Esperem que amb aquesta aplicació us poguem ajudar a fer un seguiment més exahustiu del treball dels vostres fills al centre.", u"", u"Cordialment", u"", settings.NOM_CENTRE, u"", u"", u"Video Tutorial d'ajuda a {0}".format( urlVideoTutorial), ] try: fromuser = settings.EMAIL_HOST_USER if settings.DEBUG: print u'Enviant missatge a {0}'.format(alumne) send_mail(u'Novetats al portal de relació famílies', u'\n'.join(missatge), fromuser, alumne.get_correus_relacio_familia(), fail_silently=False) enviatOK = True except: #cal enviar msg a tutor que no s'ha pogut enviar correu a un seu alumne. enviatOK = False if enviatOK: noves_sortides.update(relacio_familia_notificada=ara) noves_incidencies.update(relacio_familia_notificada=ara) noves_expulsions.update(relacio_familia_notificada=ara) noves_sancions.update(relacio_familia_notificada=ara) noves_faltes_assistencia.update( relacio_familia_notificada=ara) #if hiHaNovetatsPresencia: alumne.relacio_familia_darrera_notificacio = ara alumne.save() #LOGGING Accio.objects.create( tipus='NF', usuari=alumne.get_user_associat(), l4=False, impersonated_from=None, text=u"""Notifica Relació Famílies a {0}.""".format( alumne)) except ObjectDoesNotExist: pass
def forwards(self, orm): app = get_app('pybb') with transaction.autocommit(): create_permissions(app, (), 2)
def send_to_address(self, address, amount, description=''): if settings.BITCOIN_DISABLE_OUTGOING: raise Exception("Outgoing transactions disabled! contact support.") address = address.strip() if type(amount) != Decimal: amount = Decimal(amount) amount = amount.quantize(Decimal('0.00000001')) if not is_valid_btc_address(str(address)): raise Exception(_("Not a valid bitcoin address") + ":" + address) if amount <= 0: raise Exception(_("Can't send zero or negative amounts")) # concurrency check with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() avail = self.total_balance() updated = Wallet.objects.filter(Q(id=self.id)).update(last_balance=avail) if amount > avail: raise Exception(_("Trying to send too much")) new_balance = avail - amount updated = Wallet.objects.filter(Q(id=self.id) & Q(transaction_counter=self.transaction_counter) & Q(last_balance=avail) )\ .update(last_balance=new_balance, transaction_counter=self.transaction_counter+1) if not updated: print "address transaction concurrency:", new_balance, avail, self.transaction_counter, self.last_balance, self.total_balance() raise Exception(_("Concurrency error with transactions. Please try again.")) # concurrency check end outgoing_transaction = OutgoingTransaction.objects.create(amount=amount, to_bitcoinaddress=address) bwt = WalletTransaction.objects.create( amount=amount, from_wallet=self, to_bitcoinaddress=address, outgoing_transaction=outgoing_transaction, description=description) if getattr(django_settings, "CELERY_ALWAYS_EAGER", False): # Don't try to do asynchronous transactoin processing process_outgoing_transactions() else: process_outgoing_transactions.delay() # try: # result = bitcoind.send(address, amount) # except jsonrpc.JSONRPCException: # bwt.delete() # updated2 = Wallet.objects.filter(Q(id=self.id) & Q(last_balance=new_balance)).update(last_balance=avail) # raise self.transaction_counter = self.transaction_counter+1 self.last_balance = new_balance # check if a transaction fee exists, and deduct it from the wallet # TODO: because fee can't be known beforehand, can result in negative wallet balance. # currently isn't much of a issue, but might be in the future, depending of the application # transaction = bitcoind.gettransaction(result) # fee_transaction = None # total_amount = amount # if Decimal(transaction['fee']) < Decimal(0): # fee_transaction = WalletTransaction.objects.create( # amount=Decimal(transaction['fee']) * Decimal(-1), # from_wallet=self) # total_amount += fee_transaction.amount # updated = Wallet.objects.filter(Q(id=self.id))\ # .update(last_balance=new_balance-fee_transaction.amount) if settings.BITCOIN_TRANSACTION_SIGNALING: balance_changed.send(sender=self, changed=(Decimal(-1) * amount), transaction=bwt) balance_changed_confirmed.send(sender=self, changed=(Decimal(-1) * amount), transaction=bwt) return (bwt, None)
if e.retry: request.session['infos'].append( '<a href="/payments/%d/edit/">Click here to retry.</a>' % pmt.id) elif e.amountLacking <> pmt.amount: request.session['infos'].append( '<a href="/payments/%d/edit/">Click here to retry with a smaller amount.</a>' % pmt.id) break return HttpResponseRedirect('/payments/%d/' % pmt.id) # set payment view to autocommit -- same behaviour as 0.91 # actual payment transaction uses its own db connection pay = transaction.autocommit(pay) def cancelPayment(request, pmtId): userNode = checkLogin(request) if not userNode: return HttpResponseRedirect('/login/?redirect=%s' % request.path) if not Payment.objects.filter(pk=pmtId).count() > 0: return HttpResponseRedirect('/payments/') pmt = Payment.objects.get(pk=pmtId) if pmt.payer_id != userNode.id: return HttpResponseRedirect('/payments/')
def forwards(self, orm): app = get_app('noticeapp') with transaction.autocommit(): create_permissions(app, (), 2)
'membership_type': MEMBER_TYPES_DICT[membership.type], 'person': membership.person, 'billing_contact': membership.billing_contact, 'tech_contact': membership.tech_contact, 'ip': request.META['REMOTE_ADDR'], 'services': services}), settings.FROM_EMAIL, [membership.email_to()], fail_silently=False) return redirect('new_person_application_success') except Exception, e: transaction.rollback() logger.critical("%s" % traceback.format_exc()) logger.critical("Transaction rolled back while trying to process %s." % repr(application_form.cleaned_data)) return redirect('new_application_error') with transaction.autocommit(): return render_to_response(template_name, {"form": application_form, "chosen_email_forward": chosen_email_forward, "title": _("Person member application")}, context_instance=RequestContext(request)) # Public access def organization_application(request, template_name='membership/new_organization_application.html'): if settings.MAINTENANCE_MESSAGE != None: return redirect('frontpage') if request.method == 'POST': form = OrganizationApplicationForm(request.POST) if form.is_valid(): f = form.cleaned_data
def notifica(): from aula.apps.alumnes.models import Alumne from django.db import transaction from django.core.exceptions import ObjectDoesNotExist from django.db.models import Q from datetime import timedelta from django.utils.datetime_safe import datetime from aula.apps.presencia.models import EstatControlAssistencia from aula.apps.presencia.models import ControlAssistencia from django.core.mail import send_mail from aula.apps.usuaris.models import Accio urlDjangoAula = settings.URL_DJANGO_AULA urlVideoTutorial = "- No disponible -" with transaction.autocommit(): #actualitzo notificacions sortides: notifica_sortides() #Notificacions ara = datetime.now() fa_2_setmanes = ara - timedelta( days = 14 ) presencies_notificar = EstatControlAssistencia.objects.filter( codi_estat__in = ['F','R','J'] ) q_no_es_baixa = Q(data_baixa__gte = ara ) | Q(data_baixa__isnull = True ) q_no_informat_adreca = Q( correu_relacio_familia_pare = '' ) & Q( correu_relacio_familia_mare = '' ) llista_alumnes = Alumne.objects.filter(q_no_es_baixa).exclude( q_no_informat_adreca ).values_list('pk', flat=True) for alumne_id in llista_alumnes: try: alumne = Alumne.objects.get( pk = alumne_id ) fa_n_dies = ara - timedelta( days = alumne.periodicitat_faltes ) noves_sortides = NotificaSortida.objects.filter( alumne = alumne, relacio_familia_notificada__isnull = True ) noves_incidencies = alumne.incidencia_set.filter( relacio_familia_notificada__isnull = True ) noves_expulsions = alumne.expulsio_set.exclude( estat = 'ES').filter( relacio_familia_notificada__isnull = True ) noves_sancions = alumne.sancio_set.filter( impres=True, relacio_familia_notificada__isnull = True ) noves_faltes_assistencia = ControlAssistencia.objects.filter( alumne = alumne, impartir__dia_impartir__gte = fa_2_setmanes, relacio_familia_notificada__isnull = True, estat__pk__in = presencies_notificar ) hiHaNovetatsPresencia = False hiHaNovetats = False #comprovo si hi ha novetats de presencia i incidències fa_dies_que_no_notifiquem = alumne.relacio_familia_darrera_notificacio is None or \ alumne.relacio_familia_darrera_notificacio < fa_n_dies hiHaNovetatsPresencia = alumne.periodicitat_faltes > 0 and \ fa_dies_que_no_notifiquem and \ noves_faltes_assistencia.exists() hiHaNovetats = ( hiHaNovetatsPresencia or noves_sortides.exists() or ( alumne.periodicitat_incidencies and ( noves_incidencies.exists() or noves_expulsions.exists() or noves_sancions.exists() ) ) ) #print u'Avaluant a {0}'.format( alumne ) enviatOK = False if hiHaNovetats: #enviar correu i marcar novetats com a notificades: missatge = [ u"Portal d'informació a les famílies de " + settings.NOM_CENTRE, u"", u"Teniu novetats de {0} al portal de relació famílies {1}".format(alumne.nom, urlDjangoAula), u"", u"Recordeu que el vostre nom d'usuari és: {0}".format( alumne.get_user_associat().username ), u"", u"Esperem que amb aquesta aplicació us poguem ajudar a fer un seguiment més exahustiu del treball dels vostres fills al centre.", u"", u"Cordialment", u"", settings.NOM_CENTRE, u"", u"", u"Video Tutorial d'ajuda a {0}".format( urlVideoTutorial ), ] try: fromuser = settings.EMAIL_HOST_USER if settings.DEBUG: print u'Enviant missatge a {0}'.format( alumne ) send_mail(u'Novetats al portal de relació famílies', u'\n'.join( missatge ), fromuser, alumne.get_correus_relacio_familia(), fail_silently=False) enviatOK = True except: #cal enviar msg a tutor que no s'ha pogut enviar correu a un seu alumne. enviatOK = False if enviatOK: noves_sortides.update( relacio_familia_notificada = ara ) noves_incidencies.update( relacio_familia_notificada = ara ) noves_expulsions.update( relacio_familia_notificada = ara ) noves_sancions.update( relacio_familia_notificada = ara ) noves_faltes_assistencia.update( relacio_familia_notificada = ara ) #if hiHaNovetatsPresencia: alumne.relacio_familia_darrera_notificacio = ara alumne.save() #LOGGING Accio.objects.create( tipus = 'NF', usuari = alumne.get_user_associat(), l4 = False, impersonated_from = None, text = u"""Notifica Relació Famílies a {0}.""".format( alumne ) ) except ObjectDoesNotExist: pass
def send_to_address( self, address, amount, description="", expires_seconds=settings.BITCOIN_OUTGOING_DEFAULT_DELAY_SECONDS ): if settings.BITCOIN_DISABLE_OUTGOING: raise Exception("Outgoing transactions disabled! contact support.") address = address.strip() if type(amount) != Decimal: amount = Decimal(amount) amount = amount.quantize(Decimal("0.00000001")) if not is_valid_btc_address(str(address)): raise Exception(_("Not a valid bitcoin address") + ":" + address) if amount <= 0: raise Exception(_("Can't send zero or negative amounts")) # concurrency check with db_transaction.autocommit(): db_transaction.enter_transaction_management() db_transaction.commit() avail = self.total_balance() updated = Wallet.objects.filter(Q(id=self.id)).update(last_balance=avail) if amount > avail: raise Exception(_("Trying to send too much")) new_balance = avail - amount updated = Wallet.objects.filter( Q(id=self.id) & Q(transaction_counter=self.transaction_counter) & Q(last_balance=avail) ).update(last_balance=new_balance, transaction_counter=self.transaction_counter + 1) if not updated: print "address transaction concurrency:", new_balance, avail, self.transaction_counter, self.last_balance, self.total_balance() raise Exception(_("Concurrency error with transactions. Please try again.")) # concurrency check end outgoing_transaction = OutgoingTransaction.objects.create( amount=amount, to_bitcoinaddress=address, expires_at=datetime.datetime.now() + datetime.timedelta(seconds=expires_seconds), ) bwt = WalletTransaction.objects.create( amount=amount, from_wallet=self, to_bitcoinaddress=address, outgoing_transaction=outgoing_transaction, description=description, ) process_outgoing_transactions.apply_async((), countdown=(expires_seconds + 1)) # try: # result = bitcoind.send(address, amount) # except jsonrpc.JSONRPCException: # bwt.delete() # updated2 = Wallet.objects.filter(Q(id=self.id) & Q(last_balance=new_balance)).update(last_balance=avail) # raise self.transaction_counter = self.transaction_counter + 1 self.last_balance = new_balance # check if a transaction fee exists, and deduct it from the wallet # TODO: because fee can't be known beforehand, can result in negative wallet balance. # currently isn't much of a issue, but might be in the future, depending of the application # transaction = bitcoind.gettransaction(result) # fee_transaction = None # total_amount = amount # if Decimal(transaction['fee']) < Decimal(0): # fee_transaction = WalletTransaction.objects.create( # amount=Decimal(transaction['fee']) * Decimal(-1), # from_wallet=self) # total_amount += fee_transaction.amount # updated = Wallet.objects.filter(Q(id=self.id))\ # .update(last_balance=new_balance-fee_transaction.amount) if settings.BITCOIN_TRANSACTION_SIGNALING: balance_changed.send(sender=self, changed=(Decimal(-1) * amount), transaction=bwt) balance_changed_confirmed.send(sender=self, changed=(Decimal(-1) * amount), transaction=bwt) return (bwt, None)
def join_ride(request, ride_id): """ Given a ride_id and a previous search request, allow the user to join the ride. Required: User submitted a search request """ context_instance = RequestContext(request) user = context_instance['user'] ride = Ride.objects.get(pk=ride_id) if '_search_request' in request.session: # user is attempting to add this ride sr_post = request.session['_search_request'] rr_form = RideRequestForm(sr_post) user_sr = rr_form.save(commit = False) user_sr.user = context_instance['user'] ride = ride.filled_out(user_sr) if ride.in_bounds(): user_sr.submission_time = datetime.now() # return HttpResponse(str(ride.num_of_people() + 1)) if (ride.num_people + user_sr.num_people) <= Ride.MAX_PEOPLE: user_sr.ride = ride # TODO: Make sure the ride is not full and this person can still join # the ride. If not, then throw an error. # FIXME: Potential race condition with transaction.autocommit(): user_sr.save() ride = ride.filled_out(user_sr) if ride.num_people > Ride.MAX_PEOPLE: user_sr.delete() messages.add_message(request, messages.ERROR, "Sorry, someone beat you to that ride!" + " Pick another or create a new ride.") return redirect('/rides/search/') else: messages.add_message(request, messages.ERROR, "Sorry, someone beat you to that ride!" + " Pick another.") return redirect('/rides/search/') del request.session['_search_request'] messages.add_message(request, messages.SUCCESS, "Thank you for" + " joining a ride! Coordinate with the others" + " to meet and share the fare!" ) #Send e-mail notification to other riders subject = '%s %s has joined your ride!' % (user.first_name, user.last_name) body = '%s has joined a ride you are a part of at CabFriendly.com. Go to http://cabfriendly.com/rides/%d/ to view updated details and coordinate the ride.' % (user.first_name, ride.id) emails = [] for rider in ride.riders: if rider != user: emails.append((subject, body, OUR_EMAIL, [rider.email])) send_mass_mail(emails, fail_silently=False) return redirect('/rides/%d/' % ride.id) else: messages.add_message(request, messages.ERROR, "Sorry, you aren't" + " compatible with this ride." ) return redirect('/rides/search/') else: return redirect('/rides/current/') return redirect('/')
else: # quit if collisions == 0: request.session['infos'] = ['Credit found, but payment transaction still not able to be committed after four attempts. May be due to high system activity.'] else: request.session['infos'] = [e.message] if e.retry: request.session['infos'].append('<a href="/payments/%d/edit/">Click here to retry.</a>' % pmt.id) elif e.amountLacking <> pmt.amount: request.session['infos'].append('<a href="/payments/%d/edit/">Click here to retry with a smaller amount.</a>' % pmt.id) break return HttpResponseRedirect('/payments/%d/' % pmt.id) # set payment view to autocommit -- same behaviour as 0.91 # actual payment transaction uses its own db connection pay = transaction.autocommit(pay) def cancelPayment(request, pmtId): userNode = checkLogin(request) if not userNode: return HttpResponseRedirect('/login/?redirect=%s' % request.path) if not Payment.objects.filter(pk=pmtId).count() > 0: return HttpResponseRedirect('/payments/') pmt = Payment.objects.get(pk=pmtId) if pmt.payer_id != userNode.id: return HttpResponseRedirect('/payments/') if pmt.status not in ('RQ', 'PE'): # requested, pending
a.save() try: b.save except IntegrityError, e: raise transaction.rollback() c.save() transaction.set_autocommit(False) try: #stuff finally: transaction.set_autocommit() #Autocommit. wut? with transaction.autocommit(): try: #run process transaction.set_autocommit(True) try: #this finally: transaction.set_autocommit(False) #turn it off.