def post(self, request, *args, **kwargs): form = self.form(request.POST) error = False if form.is_valid(): posicion = 0 usuario = request.user transaction.savepoint() for formulario in form.forms: pregunta = formulario['pregunta'].data pregunta = Pregunta.objects.get(id=pregunta) respuesta = formulario['respuesta'].data pregunta_secreta = PreguntasSecretas.objects.filter(usuario=usuario, pregunta=pregunta) if not pregunta_secreta.exists(): PreguntasSecretas.objects.create(usuario=usuario, pregunta=pregunta, respuesta=respuesta) else: (self.tipo_mensaje, self.expresion) = msj_expresion('error') self.mensaje = u'Al parecer la pregunta "%s" ya la has elegido más de una vez.' %(pregunta) error=True if not error: transaction.commit() return HttpResponseRedirect('/') else: transaction.rollback() return renderizar_plantilla(request, plantilla=self.template, tipo_mensaje = self.tipo_mensaje, expresion = self.expresion, mensaje = self.mensaje, form = form )
def forwards(self, orm): # Adding model 'ProjectDomain' db.create_table('sentry_projectdomain', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('project', self.gf('django.db.models.fields.related.ForeignKey')(related_name='domain_set', to=orm['sentry.Project'])), ('domain', self.gf('django.db.models.fields.CharField')(max_length=128)), )) db.send_create_signal('sentry', ['ProjectDomain']) # Adding unique constraint on 'ProjectDomain', fields ['project', 'domain'] db.create_unique('sentry_projectdomain', ['project_id', 'domain']) sid = transaction.savepoint() try: # Adding index on 'Message', fields ['checksum'] db.create_index('sentry_message', ['checksum']) except: transaction.savepoint_rollback(sid) sid = transaction.savepoint() try: # Adding index on 'Message', fields ['checksum'] db.create_index('sentry_groupedmessage', ['checksum']) except: transaction.savepoint_rollback(sid)
def GetIPLock(cls,run, ip_address, server_item): """ Obtain a lock for this particular IP address in this run If an entry already exists, report that back, along with the entry """ while True: try: ip = IP_Address.objects.extra(where=['"probedata2_ip_address"."ip_address" = INET(E\''+ip_address+'\')'])[0] except (IP_Address.DoesNotExist, IndexError): try: sid = transaction.savepoint() ip = IP_Address.objects.create(ip_address=ip_address); ip.Construct() sid = transaction.savepoint_commit(sid) except: sid = transaction.savepoint_rollback(sid) continue break; while True: try: sid = transaction.savepoint() (probedalready,created) = cls.objects.get_or_create( part_of_run=run, ip_address=ip, port = server_item.port, protocol =server_item.protocol, defaults={"server":server_item}) sid = transaction.savepoint_commit(sid) except: sid = transaction.savepoint_rollback(sid) time.sleep(0.1) continue; break; return (probedalready,created)
def handle(self, *arguments, **options): self.parse_arguments(*arguments) for rel in User._meta.get_all_related_objects(): sid = transaction.savepoint() try: self.process_field(rel.model, rel.field.name) transaction.savepoint_commit(sid) except Exception as error: self.stdout.write(('Warning: %s\n' % error).encode('utf-8')) transaction.savepoint_rollback(sid) transaction.commit() for rel in User._meta.get_all_related_many_to_many_objects(): sid = transaction.savepoint() try: self.process_m2m_field(rel.model, rel.field.name) transaction.savepoint_commit(sid) except Exception as error: self.stdout.write(('Warning: %s\n' % error).encode('utf-8')) transaction.savepoint_rollback(sid) transaction.commit() self.process_custom_user_fields() self.cleanup() transaction.commit()
def check_cache(self, chrom): if not getattr(self, 'chrom', None) or chrom != self.chrom: log.debug('Loading cache for chr{0}...'.format(chrom)) if hasattr(self, 'chrom'): transaction.savepoint() self.cache_variants(chrom) self.chrom = chrom log.debug('done')
def add_peakcalling_to_db(): f = FileHandler("../../data/peakcalling.txt") for r in f: tokens = r.split('\t') s_name, method_name, method_version = tokens[0:3] peak_file = tokens[3] params = tokens[4:] print "a" sample = Sample.objects.get(name=s_name) method = Method.objects.get(method_name = method_name, method_version = method_version) print 'b' ''' run = models.ForeignKey(Call_peak) chrom = models.CharField(max_length=40) start = models.PositiveIntegerField(null=False) #0-based end = models.PositiveIntegerField(null=False) #1-based size = models.PositiveIntegerField(null=False) strand = models.CharField(max_length=1, choices=(('+', 'Positive'), ('-', 'Negative'), ('.', 'Unspecified'))) tag_count = models.FloatField(null=False) summit_pos_pileup = models.PositiveSmallIntegerField(null=False) #Origin summit location by peak calling method summit_val_pileup = models.FloatField(null=False) summit_pos_5 = models.PositiveSmallIntegerField() summit_val_5 = models.FloatField() p_value = models.FloatField() q_value = models.FloatField() fold = models.FloatField() ''' sid = transaction.savepoint() try: param_set = add_params_to_db(params, method) callpeak = Call_peak(sample_obj = sample, method_obj = method, params = param_set) callpeak.save() sid = transaction.savepoint() ph = FileHandler(peak_file) for p in ph: ptokens = p.split('\t') chrom, start, end, length, strand, abs_summit, pileup, pileup_5, p_val, fold, q_val, _, tc, rpkm = ptokens start = int(start) abs_summit = int(abs_summit) tc = float(tc) #print p newp = Peak(run=callpeak, chrom=chrom, start=start-1, end=end, size=length, strand=strand, tag_count=tc, summit_pos_pileup=abs_summit-1-start, summit_val_pileup = pileup, summit_pos_5 = abs_summit-1-start, summit_val_5=pileup_5, p_value = p_val, q_value = q_val, fold = fold) newp.save() transaction.savepoint_commit(sid) ph.close() except IntegrityError, msg: print msg transaction.savepoint_rollback(sid)
def check_cache(self, chrom): if not getattr(self, 'chrom', None) or chrom != self.chrom: sys.stdout.write('\nLoading cache for chr{0}...'.format(chrom)) sys.stdout.flush() if hasattr(self, 'chrom'): transaction.savepoint() self.cache_variants(chrom) self.chrom = chrom sys.stdout.write('done\n') sys.stdout.flush()
def transaction(self,bValor): try: if bValor: pass else: transaction.savepoint() # self._micounterTran+=1 except: return False else: return True
def get_or_create_tracker(self, user, topic): is_new = True sid = transaction.savepoint(using=self.db) try: with get_atomic_func()(): obj = TopicReadTracker.objects.create(user=user, topic=topic) transaction.savepoint(sid) except DatabaseError: transaction.savepoint_rollback(sid) obj = TopicReadTracker.objects.get(user=user, topic=topic) is_new = False return obj, is_new
def FetchOrCreateItem(cls,probed): """ Locate the entry for this combination of IP address, port, protocol, and server names, based on the key_hash field """ server_aliases = [probed.server] + list(probed.server_aliases.all()) assert(len(server_aliases) > 0) params = dict( ip_address = probed.ip_address, port = (probed.port if probed.port else probed.server.port), protocol = (probed.protocol if probed.protocol else probed.server.protocol), ) key_hash = cls._CalculateKeyAliases(server_aliases = server_aliases, **params) item = None try: sid = transaction.savepoint() item = CommonServerIPProbed.objects.get(key_hash = key_hash) transaction.savepoint_commit(sid) return item except: transaction.savepoint_rollback(sid) params["server_group"]=CommonServerGroup.FetchOrCreateItem(server_aliases) params["key_hash"]=cls._CalculateKeyHash(**params) assert(params["key_hash"] == key_hash) key = cls._CalculateKey(**params) while True: try: sid = transaction.savepoint() item,created = CommonServerIPProbed.objects.get_or_create(key=key, defaults=params) transaction.savepoint_commit(sid) except DatabaseError: transaction.savepoint_rollback(sid) time.sleep(0.1) continue except IntegrityError: transaction.savepoint_rollback(sid) time.sleep(0.1) continue break; return item
def add_method_to_db(): f = FileHandler("../../data/methods.txt") sid = transaction.savepoint() for r in f: tokens = r.split('\t') m = Method(method_name=tokens[0], method_type=tokens[1], method_version=tokens[2]) sid = transaction.savepoint() try: m.save() transaction.savepoint_commit(sid) except DatabaseError: transaction.savepoint_rollback(sid) f.close()
def FetchOrCreate(cls,s): """ Find an entry for this set of intolerances, check the cache first. Otherwise check the database, if necessary creating a new item. """ if s in cls.__cache: return cls.__cache[s]; while True: created =False try: sid = transaction.savepoint() e, created = cls.objects.get_or_create(intolerant_for_extension = s) transaction.savepoint_commit(sid) except DatabaseError: transaction.savepoint_rollback(sid) time.sleep(0.1) continue except IntegrityError: transaction.savepoint_rollback(sid) time.sleep(0.1) continue break; if not created: cls.__cache[s]=e return e;
def bulk_insert(cursor, rpm_nevra, filename, srpm_nevra=None): nvra = parse_nvra(rpm_nevra) if srpm_nevra: srpm_name = parse_nvra(srpm_nevra)["name"] else: srpm_name = nvra["name"] sql = add_returning("""INSERT INTO %s (name, epoch, version, release, arch, srpm_nevra, srpm_name, filename) VALUES (%%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s)""" % RPM._meta.db_table) try: sid = transaction.savepoint() RPM.check_srpm_nevra(rpm_nevra, srpm_nevra) cursor.execute(sql, [nvra["name"], nvra["epoch"], nvra["version"], nvra["release"], nvra["arch"], srpm_nevra, srpm_name, filename]) if connection.features.can_return_id_from_insert: insert_id = connection.ops.fetch_returned_insert_id(cursor) else: insert_id = connection.ops.last_insert_id(cursor, RPM._meta.db_table, "id") except (IntegrityError, ValidationError): transaction.savepoint_rollback(sid) cursor.execute("""SELECT %s FROM %s WHERE name=%%s AND epoch=%%s AND version=%%s and release=%%s AND arch=%%s""" % ("id", RPM._meta.db_table), [nvra["name"], nvra["epoch"], nvra["version"], nvra["release"], nvra["arch"]]) insert_id = int(cursor.fetchone()[0]) transaction.savepoint_commit(sid) return insert_id
def get_or_create(self, **kwargs): """ Looks up an object with the given kwargs, creating one if necessary. Returns a tuple of (object, created), where created is a boolean specifying whether an object was created. """ assert kwargs, \ 'get_or_create() must be passed at least one keyword argument' defaults = kwargs.pop('defaults', {}) try: return self.get(**kwargs), False except self.model.DoesNotExist: try: params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) params.update(defaults) obj = self.model(**params) sid = transaction.savepoint() obj.save(force_insert=True) transaction.savepoint_commit(sid) return obj, True except IntegrityError, e: transaction.savepoint_rollback(sid) try: return self.get(**kwargs), False except self.model.DoesNotExist: raise e
def save(self, actor, organization, ip_address): om = super(InviteOrganizationMemberForm, self).save(commit=False) om.organization = organization om.type = OrganizationMemberType.MEMBER try: existing = OrganizationMember.objects.filter(organization=organization, user__email__iexact=om.email)[0] except IndexError: pass else: return existing, False sid = transaction.savepoint(using="default") try: om.save() except IntegrityError: transaction.savepoint_rollback(sid, using="default") return OrganizationMember.objects.get(email__iexact=om.email, organization=organization), False transaction.savepoint_commit(sid, using="default") AuditLogEntry.objects.create( organization=organization, actor=actor, ip_address=ip_address, target_object=om.id, event=AuditLogEntryEvent.MEMBER_INVITE, data=om.get_audit_log_data(), ) om.send_invite_email() return om, True
def __enter__(self): if transaction.is_managed(self.using): # We're already in a transaction; create a savepoint. self.sid = transaction.savepoint(self.using) else: transaction.enter_transaction_management(using=self.using) transaction.managed(True, using=self.using)
def save(self, *args, **kwargs): if not self.pk and not self.slug: self.slug = self.slugify(self.name) if django.VERSION >= (1, 2): from django.db import router using = kwargs.get("using") or router.db_for_write(type(self), instance=self) # Make sure we write to the same db for all attempted writes, # with a multi-master setup, theoretically we could try to # write and rollback on different DBs kwargs["using"] = using trans_kwargs = {"using": using} else: trans_kwargs = {} i = 0 while True: i += 1 try: sid = transaction.savepoint(**trans_kwargs) res = super(TagBase, self).save(*args, **kwargs) transaction.savepoint_commit(sid, **trans_kwargs) return res except IntegrityError: transaction.savepoint_rollback(sid, **trans_kwargs) self.slug = self.slugify(self.name, i) else: return super(TagBase, self).save(*args, **kwargs)
def forwards(self, orm): # Changing field 'Tale.slug' db.alter_column('tales_tale', 'slug', self.gf('django.db.models.fields.SlugField')(max_length=100, unique=True, null=True, db_index=False)) if not db.dry_run: orm.Tale.objects.all().update(slug=None) # Adding unique constraint on 'Tale', fields ['slug'] db.create_unique('tales_tale', ['slug']) if not db.dry_run: for tale in orm.Tale.objects.order_by('pk'): tale.slug = slugify(tale.title) i = 0 while True: i += 1 try: sid = transaction.savepoint() tale.save() transaction.savepoint_commit(sid) break except IntegrityError: transaction.savepoint_rollback(sid) tale.slug = slugify(tale.title, i) # Removing null=True. We need it only for update. db.alter_column('tales_tale', 'slug', self.gf('django.db.models.fields.SlugField')(max_length=100, unique=True, null=False, db_index=False))
def accept_results(request): """accept data submissions from the lab via POST. see connection() in extract.py for how to submit; attempts to save raw data/partial data if for some reason the full content is not parseable or does not validate to the model schema""" if request.META['CONTENT_TYPE'] != 'text/json': logger.warn('incoming post does not have text/json content type') content = request.raw_post_data payload_date = datetime.now() payload_user = request.user try: data = json.loads(content) except: data = None #safety -- no matter what else happens, we'll have the original data payload = labresults.Payload.objects.create(incoming_date=payload_date, auth_user=payload_user, parsed_json=data is not None, raw=content) sid = transaction.savepoint() if not data: #if payload does not parse as valid json, save raw content and return error return HttpResponse('CANNOT PARSE (%d bytes received)' % len(content)) try: process_payload(payload, data) except: logging.exception('second stage result parsing failed; rolling back ' 'to savepoint.') transaction.savepoint_rollback(sid) return HttpResponse('SUCCESS')
def test_prevent_rollback(self): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") sid = transaction.savepoint() # trigger a database error inside an inner atomic without savepoint with self.assertRaises(DatabaseError): with transaction.atomic(savepoint=False): connection.cursor().execute( "SELECT no_such_col FROM transactions_reporter" ) # prevent atomic from rolling back since we're recovering manually self.assertTrue(transaction.get_rollback()) transaction.set_rollback(False) transaction.savepoint_rollback(sid) self.assertQuerysetEqual(Reporter.objects.all(), ['<Reporter: Tintin>']) self.assertAtomicSignalCalls( # Enter atomic transaction block. enter_block_atomic_signal_call_sequence(True) + # Create Reporter. create_model_atomic_signal_call_sequence() + # Enter and leave atomic transaction block. enter_block_atomic_signal_call_sequence(False, savepoint=False) + leave_block_atomic_signal_call_sequence(False, False, savepoint=False) + # Leave atomic transaction with recovered rollback. leave_block_atomic_signal_call_sequence(True, True) )
def get_or_create(self, **kwargs): """ This is a copy of QuerySet.get_or_create, that forces calling our custom create method when the get fails. """ assert kwargs, \ 'get_or_create() must be passed at least one keyword argument' defaults = kwargs.pop('defaults', {}) try: self._for_write = True return self.get(**kwargs), False except self.actual_model.DoesNotExist: params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) params.update(defaults) obj = self.model(**params) self._for_write = True self._instance = obj using = self.db try: sid = transaction.savepoint(using=using) obj.save(force_insert=True, using=using) except IntegrityError, e: transaction.savepoint_rollback(sid, using=using) try: return self.get(**kwargs), False except self.actual_model.DoesNotExist, e: raise self.actual_model.DoesNotExist(unicode(e).replace(self.model.__name__, self.actual_model.__name__))
def save_company_information(request): # pdb.set_trace() sid = transaction.savepoint() try: if request.method == "POST": company_obj = Company( company_name = request.POST.get('company_name'), company_email = request.POST.get('company_email'), company_phone_no = request.POST.get('contact_number'), company_address = request.POST.get('address_line'), company_city = request.POST.get('city'), company_state = request.POST.get('state'), company_country = request.POST.get('country'), company_pincode = request.POST.get('pincode'), company_creation_date=datetime.datetime.now() ) company_obj.save() print "done" company_obj.company_unique_id=str('CO'+ datetime.date.today().strftime('%d%m%y') + str(company_obj.company_id).zfill(4)) company_obj.save() transaction.savepoint_commit(sid) data = {'success': 'true', 'company_id':company_obj.company_id, 'company_name':company_obj.company_name } else: print 'Invalid Request' transaction.savepoint_rollback(sid) data = {'sucess': 'false' , ExceptionLabel.ERROR_MESSAGE :'Invalid Request'} except Exception, e: transaction.savepoint_rollback(sid) print 'error',e data = {'sucess': 'false', ExceptionLabel.ERROR_MESSAGE :'Server Error'}
def save_manager_information(request): sid = transaction.savepoint() try: if request.method == "POST": rm_obj = RelationShipManager( rm_first_name = request.POST.get('first_name'), rm_last_name = request.POST.get('last_name'), rm_email = request.POST.get('manager_email'), rm_contactno = request.POST.get('manager_number'), rm_status = 1, rm_creation_date=datetime.datetime.now() ) rm_obj.save() print "done" rm_obj.rm_unique_id=str('RM'+ datetime.date.today().strftime('%d%m%y') + str(rm_obj.relationship_manager_id).zfill(4)) rm_obj.save() transaction.savepoint_commit(sid) data = {'success': 'true', 'manager_id':rm_obj.relationship_manager_id, 'manager_name':rm_obj.rm_first_name +" "+ rm_obj.rm_last_name } else: print 'Invalid Request' data = {'sucess': 'false' , ExceptionLabel.ERROR_MESSAGE :'Invalid Request'} except Exception, e: transaction.savepoint_rollback(sid) print 'error',e data = {'sucess': 'false', ExceptionLabel.ERROR_MESSAGE :'Server Error'}
def testmail(request): try: kwargs = dict(instance=models.Email.objects.order_by("-id")[0]) except IndexError: kwargs = {} form = forms.EmailForm(request.POST, **kwargs) if not form.is_valid(): return JsonResp(request, form=form) email = bsdUsers.objects.get(bsdusr_username="******").bsdusr_email if not email: return JsonResp(request, error=True, message=_("You must configure the root email" " (Accounts->Users->root)")) sid = transaction.savepoint() form.save() error = False if request.is_ajax(): sw_name = get_sw_name() error, errmsg = send_mail( subject=_("Test message from %s" % (sw_name)), text=_("This is a message test from %s" % (sw_name,)) ) if error: errmsg = _("Your test email could not be sent: %s") % errmsg else: errmsg = _("Your test email has been sent!") transaction.savepoint_rollback(sid) return JsonResp(request, error=error, message=errmsg)
def create(row, model, model_attrs, related_attrs, processor): sid = transaction.savepoint() try: with transaction.atomic(): _create_instances(model, model_attrs, related_attrs) except (Error, ValueError, AttributeError, TypeError, IndexError): transaction.savepoint_rollback(sid) error_message = traceback.format_exc() if 'File' in error_message: error_message = 'File{}'.format( error_message.split('File')[-1]) value = { 'model_attrs': model_attrs, 'related_attrs': related_attrs } error_raised.send(processor, error=error_message, position=row, value=value, step=ErrorChoicesMixin.IMPORT_DATA)
def FetchOrCreateItem(cls,**params): """ Either find an existing entry matching the config & results in the params dictionary, or create a new entry. """ key = cls._CalculateKey(**params) if key in cls.__cache: return cls.__cache[key]; params1 = dict(params) params1.pop("servername",None) params1.pop("part_of_run", None) while True: created =False try: sid = transaction.savepoint() item,created = ProbeCommonResultEntry.objects.get_or_create(key=key, defaults=params1) transaction.savepoint_commit(sid) except DatabaseError: transaction.savepoint_rollback(sid) time.sleep(0.1) continue except IntegrityError: transaction.savepoint_rollback(sid) time.sleep(0.1) continue break; if not created: cls.__cache[key] = item return item
def delete(self, request, *args, **kwargs): sid = transaction.savepoint() try: association = Association.objects.get( id=UserToken.get_association_id(request.session)) if Enterprise.objects.filter(headquar__association_id=UserToken.get_association_id(request.session)).count() == 1: raise Exception( (u"Asociación <b>%(name)s</b> no puede quedar sin ninguna sede asociada.") % {"name": association.name}) d = self.get_object() # rastrear dependencias deps, msg = get_dep_objects(d) if deps: messages.warning(self.request, _('Cannot delete %(name)s') % { "name": capfirst(force_text(self.model._meta.verbose_name)) + ' "' + force_text(d) + '"' }) raise Exception(msg) d.delete() msg = _('The %(name)s "%(obj)s" was deleted successfully.') % { 'name': capfirst(force_text(self.model._meta.verbose_name)), 'obj': force_text(d) } if not d.id: messages.success(self.request, msg) log.warning(msg, extra=log_params(self.request)) except Exception, e: try: transaction.savepoint_rollback(sid) except: pass messages.error(request, e) log.warning(force_text(e), extra=log_params(self.request))
def form_valid(self, form): sid = transaction.savepoint() try: self.object = form.save(commit=True) headquar = Headquar() headquar.name = self.request.POST.get("sede") headquar.association_id = UserToken.get_association_id( self.request.session) headquar.enterprise = self.object headquar.save() msg = _('The %(name)s "%(obj)s" was added successfully.') % { 'name': capfirst(force_text(self.model._meta.verbose_name)), 'obj': force_text(self.object) } if self.object.id: messages.success(self.request, msg) log.warning(msg, extra=log_params(self.request)) return super(EnterpriseCreateView, self).form_valid(form) except Exception, e: try: transaction.savepoint_rollback(sid) except: pass messages.success(self.request, e) log.warning(force_text(e), extra=log_params(self.request)) return super(EnterpriseCreateView, self).form_invalid(form)
def migrate_app(sender, *args, **kwargs): """ Migrate all models of this app registered """ from .registration import registry if 'app_config' not in kwargs: return app_config = kwargs['app_config'] app_name = app_config.label fields = [fld for fld in registry._field_registry.keys() if fld.startswith(app_name)] sid = transaction.savepoint() for fld in fields: model_name, field_name = fld.split('.')[1:] if field_exists(app_name, model_name, field_name): continue model = app_config.get_model(model_name) try: with connection.schema_editor() as schema_editor: schema_editor.add_field(model, registry._field_registry[fld]) transaction.savepoint_commit(sid) except ProgrammingError: transaction.savepoint_rollback(sid) continue
def test_savepoint_rollback(self): """Tests rollbacks of savepoints""" from django.db import transaction from testapp.models import Genre, Publisher from johnny import cache if not connection.features.uses_savepoints: return self.failUnless(transaction.is_managed() == False) self.failUnless(transaction.is_dirty() == False) connection.queries = [] cache.local.clear() transaction.enter_transaction_management() transaction.managed() g = Genre.objects.get(pk=1) start_title = g.title g.title = "Adventures in Savepoint World" g.save() g = Genre.objects.get(pk=1) self.failUnless(g.title == "Adventures in Savepoint World") sid = transaction.savepoint() g.title = "In the Void" g.save() g = Genre.objects.get(pk=1) self.failUnless(g.title == "In the Void") transaction.savepoint_rollback(sid) g = Genre.objects.get(pk=1) self.failUnless(g.title == "Adventures in Savepoint World") transaction.rollback() g = Genre.objects.get(pk=1) self.failUnless(g.title == start_title) transaction.managed(False) transaction.leave_transaction_management()
def set_lock(*locks, timeout=600, fail=False, renew=True, create=True, **options) -> LockSetResult: """ This function is for advanced users, offering multiple lock creation, renewing, along with "all or nothing" locking with database rollback via the argument ``fail``. Unlike other lock management functions, set_lock returns a :class:`.LockSetResult` object, which is designed to allow you to see clearly as to what locks were created, renewed, or skipped. **Example Usage** Let's set two locks, ``hello`` and ``world``. >>> res = set_lock('hello', 'world') >>> res['locks'] [<Lock name='hello' locked_by='example.org' locked_until='2019-11-22 02:01:55.439390+00:00'>, <Lock name='world' locked_by='example.org' locked_until='2019-11-22 02:01:55.442734+00:00'>] >>> res['counts'] {'created': 2, 'renewed': 0, 'skip_create': 0, 'skip_renew': 0} If we run ``set_lock`` again with the same arguments, we'll still get the locks list, but we'll see the counts show that they were renewed instead of created. >>> x = set_lock('hello', 'world') >>> x['locks'] [<Lock name='hello' locked_by='example.org' locked_until='2019-11-22 02:03:06.762620+00:00'>, <Lock name='world' locked_by='example.org' locked_until='2019-11-22 02:03:06.766804+00:00'>] >>> x['counts'] {'created': 0, 'renewed': 2, 'skip_create': 0, 'skip_renew': 0} Since the result is an object, you can also access attributes via dot notation, as well as dict-like notation. We can see inside of the ``statuses`` list - the action that was taken on each lock we specified, so we can see what locks were created, renewed, or skipped etc. >>> x.statuses[0] ('hello', {'was_locked': True, 'status': 'extend', 'locked': True}) >>> x.statuses[1] ('world', {'was_locked': True, 'status': 'extend', 'locked': True}) :param str locks: One or more lock names, as positional arguments, to create or renew. :param int timeout: On existing locks, update locked_until to ``now + timeout`` (seconds) :param bool fail: (Default: False) If ``True``, all lock creations will be rolled back if an existing lock is encountered, and :class:`.LockFail` will be raised. :param bool renew: (Default: True) If ``True``, any existing locks in ``locks`` will be renewed to ``now + timeout`` (seconds). If False, existing locks will just be skipped. :param bool create: (Default: True) If ``True``, any names in ``locks`` which aren't yet locked, will have a lock created for them, with their expiry set to ``timeout`` seconds from now. :key str locked_by: (Default: system hostname) What server/app is trying to obtain this lock? :key int process_id: (Optional) The process ID requesting the lock :return LockSetResult results: A :class:`.LockSetResult` object containing the results of the set_lock operation. """ fail = is_true(fail) timeout = int(timeout) process_id = options.get('process_id') process_id: int = int(process_id) if process_id is not None else None locked_by: str = options.get('locked_by') lock_args = dict(expires=timeout, locked_by=locked_by, lock_process=process_id) result = LockSetResult(locks=[], counts=dict(created=0, renewed=0, skip_create=0, skip_renew=0), statuses=[]) try: with transaction.atomic(): sid = transaction.savepoint() for l in locks: try: if not create and not is_locked(l): log.debug( f" > The lock '{l}' doesn't exist, but create=False was specified. Not locking." ) result['statuses'] += [(l, LockSetStatus(was_locked=False, status='skip', locked=False))] # result['locks'] += [lck] result.counts['skip_create'] += 1 continue lck = get_lock(l, **lock_args) log.info( f" > Lock {l} did not yet exist. Successfully locked '{l}' - expiry: {lck.locked_until}" ) result.statuses += [(l, LockSetStatus(was_locked=False, status='create', locked=True))] result.locks += [lck] result.counts['created'] += 1 except Locked: lck = Lock.objects.get(name=l) if fail: transaction.savepoint_rollback(sid) raise LockFail( f"Lock '{l}' already existed. Aborting!", lock=lck) if not renew: result['statuses'] += [(l, LockSetStatus(was_locked=True, status='skip', locked=True))] result['locks'] += [lck] log.debug( f" > The lock '{l}' already exists, but renew=False - not renewing this lock." ) log.debug(f"\tLock: %s\t", lck) log.debug(" > Skipping this lock...\n") result.counts['skip_renew'] += 1 continue lck = renew_lock(lck, **lock_args) result['locks'] += [lck] result['statuses'] += [(l, LockSetStatus(was_locked=True, status='extend', locked=True))] result.counts['renewed'] += 1 log.info( f" > The lock '{l}' already exists. Renewed it's expiry to: {lck.locked_until}" ) except LockFail as e: log.error( "Error: An existing lock was found while fail=True" "\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" " !!! An existing lock was found:\n" f" !!! \t{e.lock}\n" " !!! As you have specified fail=True, any locks created during this session will now be\n" " !!! rolled back for your safety.\n" " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" ) transaction.rollback() log.error( "Any locks created during this session should now have been removed..." ) raise e return result
def _create_sampleAttributeValue(csvSampleDict, request, user, sample): """ save sample customer attribute value to db. """ customAttributes = SampleAttribute.objects.filter(isActive=True) currentDateTime = timezone.now() ##datetime.datetime.now() for attribute in customAttributes: newValue = None if attribute.displayedName not in csvSampleDict.keys(): # add mandatory custom attributes for an imported sample if user has not added it if attribute.isMandatory: if attribute.dataType and attribute.dataType.dataType == "Integer": newValue = "0" else: newValue = "" else: newValue = csvSampleDict.get(attribute.displayedName, "") if newValue is None: logger.debug( "import_sample_processor._create_sampleAttributeValue SKIPPING due to NO VALUE for attribute=%s;" % (attribute.displayedName)) else: logger.debug( "import_sample_processor._create_sampleAttributeValue going to get_or_create sample=%s; attribute=%s; value=%s" % (sample.displayedName, attribute.displayedName, newValue)) sampleAttributeValues = SampleAttributeValue.objects.filter( sample=sample, sampleAttribute=attribute) if sampleAttributeValues: sampleAttributeValue = sampleAttributeValues[0] # logger.debug("import_sample_processor._create_sampleAttributeValue ORIGINAL VALUE pk=%s; sample=%s; attribute=%s; orig value=%s" %(sampleAttributeValue.id, sample.displayedName, attribute.displayedName, sampleAttributeValue.value)) # there should only be 1 attribute value for each sample/attribute pair if the old entry has value but the new import doesn't, do not override it. if newValue: sampleAttributeValue_kwargs = { 'value': newValue, 'lastModifiedUser': user, 'lastModifiedDate': currentDateTime } for field, value in sampleAttributeValue_kwargs.iteritems( ): setattr(sampleAttributeValue, field, value) sampleAttributeValue.save() # logger.debug("import_sample_processor._create_sampleAttributeValue UPDATED pk=%s; sample=%s; attribute=%s; newValue=%s" %(sampleAttributeValue.id, sample.displayedName, attribute.displayedName, newValue)) else: # logger.debug("import_sample_processor._create_sampleAttributeValue going to DELETE pk=%s; sample=%s; attribute=%s; newValue=%s" %(sampleAttributeValue.id, sample.displayedName, attribute.displayedName, newValue)) sampleAttributeValue.delete() else: # create a record only there is a value if newValue: sampleAttributeValue_kwargs = { 'sample': sample, 'sampleAttribute': attribute, 'value': newValue, 'creator': user, 'creationDate': currentDateTime, 'lastModifiedUser': user, 'lastModifiedDate': currentDateTime } sampleAttributeValue = SampleAttributeValue( **sampleAttributeValue_kwargs) sampleAttributeValue.save() logger.debug( "import_sample_processor._create_sampleAttributeValue CREATED sampleAttributeValue.pk=%d; sample=%s; attribute=%s; newValue=%s" % (sampleAttributeValue.pk, sample.displayedName, attribute.displayedName, newValue)) siv_sid = transaction.savepoint() return siv_sid
def post(self, request): user = request.user # 获取数据 前端发送的数据类型是json字符串 data = json.loads(request.body.decode()) title = data.get('title') # 标题 price = data.get('price') # 价格 area_id = data.get('area_id') # 城区id address = data.get('address') # 房屋地址 room_count = data.get('room_count') # 房屋数目 acreage = data.get('acreage') # 房屋面积 unit = data.get('unit') # 房屋单元,如:几室几厅 capacity = data.get('capacity') # 房屋容纳的人数 beds = data.get('beds') # 房屋床铺的配置 deposit = data.get('deposit') # 房屋押金 min_days = data.get('min_days') # 最少入住天数 max_days = data.get('max_days') # 最大入住天数,0表示不限制 # 类型是列表可能为空 不需要去判断这个数据是否存在 facility_ids = data.get('facility') # 用户选择的设施信息id列表,如:[7, 8] # 验证数据 if not all([ title, price, area_id, address, room_count, acreage, unit, capacity, beds, deposit, min_days, max_days ]): return http.JsonResponse({"errno": RET.PARAMERR, "errmsg": "参数错误"}) # 最少入住天数不能小于0 if eval(min_days) < 0 or eval(max_days) < 0: return http.JsonResponse({"errno": RET.PARAMERR, "errmsg": "参数错误"}) try: # 将 price 和 deposit进行数据类型转换因为获取的这两个数据是字符串类型, 数据库中是int # 数据里这两个单位是分 price = int(float(price) * 100) deposit = int(float(deposit) * 100) except Exception as e: logger.error(e) return http.JsonResponse({"errno": RET.PARAMERR, "errmsg": "参数错误"}) # 因为添加数据的时候 对同一个对象会操作两次 使用事件 with transaction.atomic(): save_id = transaction.savepoint() # 保存数据 try: house = House.objects.create(user=user, title=title, price=price, area_id=area_id, address=address, room_count=room_count, acreage=acreage, unit=unit, capacity=capacity, beds=beds, deposit=deposit, min_days=min_days, max_days=max_days) except DatabaseError as e: logger.error(e) transaction.savepoint_rollback(save_id) return http.JsonResponse({ "errno": RET.DBERR, "errmsg": "数据库保存失败" }) # 添加设施 房间表和设施表是多对多的关系 facility_ids 中保存的是设施的id try: if facility_ids: # facility_ids 不为空 则查找对应的设施名字 in表示范围查询, 是否包含在范围内 facilitys = Facility.objects.filter(id__in=facility_ids) for facility in facilitys: house.facility.add(facility) except DatabaseError as e: logger.error(e) transaction.savepoint_rollback(save_id) return http.JsonResponse({ "errno": RET.DBERR, "errmsg": "数据库保存失败" }) transaction.savepoint_commit(save_id) return http.JsonResponse({ "errno": RET.OK, "errmsg": "发布成功", "data": { "house_id": house.id } })
def addon_view_or_download_file(auth, path, provider, **kwargs): extras = request.args.to_dict() extras.pop('_', None) # Clean up our url params a bit action = extras.get('action', 'view') node = kwargs.get('node') or kwargs['project'] node_addon = node.get_addon(provider) provider_safe = markupsafe.escape(provider) path_safe = markupsafe.escape(path) project_safe = markupsafe.escape(node.project_or_component) if not path: raise HTTPError(httplib.BAD_REQUEST) if not isinstance(node_addon, BaseStorageAddon): raise HTTPError( httplib.BAD_REQUEST, data={ 'message_short': 'Bad Request', 'message_long': 'The {} add-on containing {} is no longer connected to {}.'. format(provider_safe, path_safe, project_safe) }) if not node_addon.has_auth: raise HTTPError( httplib.UNAUTHORIZED, data={ 'message_short': 'Unauthorized', 'message_long': 'The {} add-on containing {} is no longer authorized.'.format( provider_safe, path_safe) }) if not node_addon.complete: raise HTTPError( httplib.BAD_REQUEST, data={ 'message_short': 'Bad Request', 'message_long': 'The {} add-on containing {} is no longer configured.'.format( provider_safe, path_safe) }) savepoint_id = transaction.savepoint() file_node = BaseFileNode.resolve_class(provider, BaseFileNode.FILE).get_or_create( node, path) # Note: Cookie is provided for authentication to waterbutler # it is overriden to force authentication as the current user # the auth header is also pass to support basic auth version = file_node.touch( request.headers.get('Authorization'), **dict(extras, cookie=request.cookies.get(settings.COOKIE_NAME))) if version is None: # File is either deleted or unable to be found in the provider location # Rollback the insertion of the file_node transaction.savepoint_rollback(savepoint_id) if not file_node.pk: redirect_file_node = BaseFileNode.load(path) # Allow osfstorage to redirect if the deep url can be used to find a valid file_node if redirect_file_node and redirect_file_node.provider == 'osfstorage' and not redirect_file_node.is_deleted: return redirect( redirect_file_node.node.web_url_for( 'addon_view_or_download_file', path=redirect_file_node._id, provider=redirect_file_node.provider)) raise HTTPError(httplib.NOT_FOUND, data={ 'message_short': 'File Not Found', 'message_long': 'The requested file could not be found.' }) return addon_deleted_file(file_node=file_node, path=path, **kwargs) else: transaction.savepoint_commit(savepoint_id) # TODO clean up these urls and unify what is used as a version identifier if request.method == 'HEAD': return make_response(('', httplib.FOUND, { 'Location': file_node.generate_waterbutler_url( **dict(extras, direct=None, version=version.identifier, _internal=extras.get('mode') == 'render')) })) if action == 'download': format = extras.get('format') _, extension = os.path.splitext(file_node.name) # avoid rendering files with the same format type. if format and '.{}'.format(format) != extension: return redirect('{}/export?format={}&url={}'.format( MFR_SERVER_URL, format, urllib.quote( file_node.generate_waterbutler_url( **dict(extras, direct=None, version=version.identifier, _internal=extras.get('mode') == 'render'))))) return redirect( file_node.generate_waterbutler_url( **dict(extras, direct=None, version=version.identifier, _internal=extras.get('mode') == 'render'))) if action == 'get_guid': draft_id = extras.get('draft') draft = DraftRegistration.load(draft_id) if draft is None or draft.is_approved: raise HTTPError(httplib.BAD_REQUEST, data={ 'message_short': 'Bad Request', 'message_long': 'File not associated with required object.' }) guid = file_node.get_guid(create=True) guid.referent.save() return dict(guid=guid._id) if len(request.path.strip('/').split('/')) > 1: guid = file_node.get_guid(create=True) return redirect( furl.furl('/{}/'.format(guid._id)).set(args=extras).url) return addon_view_file(auth, node, file_node, version)
def form_valid(self, form): sid = transaction.savepoint() try: self.object = form.save(commit=False) msg = 'El %(name)s "%(obj)s" fue cambiado satisfactoriamente.' % { 'name': self.model._meta.verbose_name, 'obj': self.object } messages.success(self.request, msg) try: precio_local = PrecioVenta.objects.get(producto_general=self.get_object(), ruta=LOCAL, estado=True) except: precio_local = PrecioVenta() precio_local.producto_general = self.object precio_local.estado = True precio_local.ruta = LOCAL precio_local.user = self.request.user precio_local.precio = form.cleaned_data['precio_local'] precio_local.save() precio_local.precio = form.cleaned_data['precio_local'] precio_local.save() try: precio_regional = PrecioVenta.objects.get(producto_general=self.get_object(), ruta=REGIONAL, estado=True) except: precio_regional = PrecioVenta() precio_regional.producto_general = self.object precio_regional.estado = True precio_regional.ruta = REGIONAL precio_regional.user = self.request.user precio_regional.precio = form.cleaned_data['precio_regional'] precio_regional.save() precio_regional.precio = form.cleaned_data['precio_regional'] precio_regional.save() try: precio_nacional = PrecioVenta.objects.get(producto_general=self.get_object(), ruta=NACIONAL, estado=True) except: precio_nacional = PrecioVenta() precio_nacional.producto_general = self.object precio_nacional.estado = True precio_nacional.ruta = NACIONAL precio_nacional.user = self.request.user precio_nacional.precio = form.cleaned_data['precio_nacional'] precio_nacional.save() precio_nacional.precio = form.cleaned_data['precio_nacional'] precio_nacional.save() return super(ProductoGeneralUpdateView, self).form_valid(form) except Exception as e: try: transaction.savepoint_rollback(sid) except: pass messages.success(self.request, e) return super(ProductoGeneralUpdateView, self).form_invalid(form)
def post(self, request): # 创建订单 # 判断用户是否登录 user = request.user if not user.is_authenticated(): return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接收数据 addr_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') # 判断数据是否完整 if not all([addr_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '数据不完整'}) # 校验支付方式 if pay_method not in OrderInfo.PAY_METHODS.keys(): return JsonResponse({'res': 2, 'errmsg': '非法的支付方式'}) # 校验地址 try: addr = Address.objects.get(id=addr_id) except Exception as e: # 地址不存在 return JsonResponse({'res': 3, 'errmsg': '地址不存在'}) # 创建订单核心 # 组织参数 # 订单id:20190605231212+用户id order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id) # 运费 transit_price = 10 # 总数目和总金额 total_count = 0 total_price = 0 # 设置事物保存点 save_id = transaction.savepoint() try: # 向df_order_info插入一条数据 order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_price=total_price, total_count=total_count, transit_price=transit_price) # 向df_order_goods表中插入几条数据 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id sku_ids = sku_ids.split(',') for sku_id in sku_ids: # 获取商品的信息 for i in range(3): try: # sku = GoodsSKU.objects.select_for_update().get(id=sku_id) 解决并发冲突 --> 悲观锁 sku = GoodsSKU.objects.get(id=sku_id) except Exception as e: # 商品不存在 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在'}) # 从redis中获取用户所要购买的商品的数量 count = conn.hget(cart_key, sku_id) # 判断商品的库存 if int(count) > sku.stock: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 6, 'errmsg': '商品库存不足'}) # 更新商品的库存和销量 orgin_stock = sku.stock new__stock = orgin_stock - int(count) new_sales = sku.sales + int(count) # 返回受影响的行 res = GoodsSKU.objects.filter( id=sku_id, stock=orgin_stock).update(stock=new__stock, sales=new_sales) if res == 0: if i == 2: # 尝试第三次 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'errmsg': '下单失败'}) continue # 向df_order_goods表中插入1条数据 OrderGoods.objects.create(order=order, sku=sku, count=count, price=sku.price) # 累加订单商品的总数量和总金额 amount = sku.price * int(count) total_count += int(count) total_price += amount # 跳出循环 break # 更新订单信息表中的商品总数量和总金额 order.total_price = total_price order.total_count = total_count order.save() except Exception as e: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'errmsg': '下单失败'}) # 提交事物 transaction.savepoint_commit(save_id) # 清楚用户购物车中对应的记录 conn.hdel(cart_key, *sku_ids) # 返回应答 return JsonResponse({'res': 5, 'message': '创建成功'})
def post(self, request): # 接收数据 data = json.loads(request.body) commodityId_list = data['commodityId_list'] address_id = data['address_id'] note = data['note'] token = data['token'] commodity_num = data['num'] discount_price = data['card_price'] card_token = data['card_token'] open_id = PublicFunction().getOpenIdByToken(token) # 参数校验 if not all([commodityId_list, address_id, token]): return Response({'msg': '数据不完整'}) # 校验用户 if open_id: try: wx_user = WxUser.objects.get(open_id=open_id) except: return Response({'msg': '用户不存在'}) # 校验地址信息 try: address = Address.objects.get(id=address_id) except Address.DoesNotExist: # 地址不存在 return Response({'msg': '地址信息错误'}) # 业务逻辑 # 生成订单号 order_id = PublicFunction().orderNum() # 运费 transit_price = 0 # 总数目和总价格 total_count = 0 total_price = 0 # 设置事务保存点 sid = transaction.savepoint() try: # 向gd_order_info表中添加一条记录 order = OrderInfo.objects.create(wx_user=wx_user, address=address.address, name=address.name, phone=address.phone, order_id=order_id, commodity_total_price=total_price, total_price=total_price, total_count=total_count, transit_price=transit_price, discount_price=discount_price, note=note) # 遍历向gd_order_list中添加记录 for commodity_id in commodityId_list: # 获取商品的信息(行锁查询) try: commodity = Commodity.objects.select_for_update().get( id=commodity_id) except Commodity.DoesNotExist: # 商品不存在,回滚到sid事务保存点 transaction.savepoint_rollback(sid) return Response({'msg': '商品不存在'}) # 获取用户要购买商品的数量 conn = get_redis_connection('Cart') cart_key = 'cart_' + open_id if commodity_num == '': count = conn.hget(cart_key, commodity_id) else: count = commodity_num print(count) print(commodity.stock) # 判断商品的库存 if int(count) > commodity.stock: # 商品库存不足,回滚到sid事务保存点 transaction.savepoint_rollback(sid) return Response({'msg': '商品库存不足'}) # 向gd_order_list中添加一条记录 OrderList.objects.create( order_info=order, wx_user=wx_user, commodity=commodity, commodity_name=commodity.name, commodity_specifications=commodity.code + ' ' + commodity.color, commodity_price=commodity.price, commodity_count=int(count), commodity_image=commodity.image) # 减少商品的库存,增加销量 commodity.stock -= int(count) commodity.sales += int(count) commodity.save() # 加计算用户要购买的商品的总数目和总价格 total_count += int(count) total_price += commodity.price * int(count) # 更新order对应记录中的total_count和total_price order.total_count = total_count order.total_price = round( float(total_price) + float(transit_price) - float(discount_price), 2) order.commodity_total_price = total_price order.save() except Exception as e: # 数据库操作出错,回滚到sid事务保存点 print(e) transaction.savepoint_rollback(sid) return Response({'msg': '下单失败'}) # 删除购物车中对应的记录 sku_ids=[1,2] conn.hdel(cart_key, *commodityId_list) if (card_token != 'no'): user_card = UserCard.objects.get(token=card_token) user_card.is_use = True # 返回应答 return Response({'msg': '订单创建成功', 'order_id': order_id})
def post(self, request): IsAuthenticate2.has_permission(self, request, "modules:enterprise:update") api_response = {"code": 0, "msg": "success"} if 'token' not in request.data or request.data.get('token') is "": raise ValidationError("参数不能为空") token = request.data.get('token') accName = rc4_decode_main(secretkey, token) if 'etpCode' not in request.data or request.data.get('etpCode') is "": raise ValidationError("参数不能为空") etpCode = request.data.get('etpCode') sid = transaction.savepoint() # 开启事务 enterprise = Enterprise.objects.filter(etpCode=etpCode).first() if not enterprise: raise NotFoundError('该组织机构不存在') try: updateTime = (request.data.get('updateTime') if ('updateTime' in request.data) else datetime.now()) enterprise.updateTime = updateTime if ('dhcpServerIP' in request.data): enterprise.dhcpServerIP = request.data.get('dhcpServerIP') if ('etpName' in request.data): enterprise.etpName = request.data.get('etpName') if ('LDAPCode' in request.data): enterprise.LDAPCode = request.data.get('LDAPCode') if ('TFTPServerIP' in request.data): enterprise.TFTPServerIP = request.data.get('TFTPServerIP') if ('FTPServerIP' in request.data): enterprise.FTPServerIP = request.data.get('FTPServerIP') if ('serverInfolist' in request.data): serverInfolist = request.data.get('serverInfolist') for server in serverInfolist: serverType = (server.get('serverType') if ('serverType' in server) else 0) serverInfo = Server.objects.filter( etpCode=etpCode, serverType=serverType).first() if not serverInfo: serverIP = (server.get('serverIP') if ('serverIP' in server) else "") serverUsername = (server.get('serverUsername') if ('serverUsername' in server) else "") serverPasswd = (server.get('serverPasswd') if ('serverPasswd' in server) else "") newServer = Server() newServer.etpCode = etpCode newServer.serverType = serverType newServer.serverIP = serverIP newServer.serverUsername = serverUsername newServer.serverPasswd = serverPasswd newServer.save() else: if ('serverIP' in server): serverInfo.serverIP = server.get('serverIP') if ('serverUsername' in server): serverInfo.serverUsername = server.get( 'serverUsername') if ('serverPasswd' in server): serverInfo.serverPasswd = server.get( 'serverPasswd') serverInfo.save() enterprise.save() try: transaction.savepoint_commit(sid) # 提交 except Exception as ie: Logger('error.log', level='error').logger.error( "[事务提交失败]accName:【%s】%s" % (accName, ie)) transaction.savepoint_rollback(sid) # 回滚 return Response(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[修改企业异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return Response(api_response), 500
def post(self, request): '''订单创建''' # 判断用户是否登录 user = request.user if not user.is_authenticated(): # 用户未登录 return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接收参数: 收货地址id,支付方式id,商品id addr_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') # 1,3 # 校验参数 if not all([addr_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '参数不完整'}) # 校验支付方式 if pay_method not in OrderInfo.PAY_METHODS.keys(): return JsonResponse({'res': 2, 'errmsg': '非法的支付方式'}) # 校验地址 try: addr = Address.objects.get(id=addr_id) except Address.DoesNotExist: # 地址不存在 return JsonResponse({'res': 3, 'errmsg': '地址非法'}) # todo: 创建订单核心业务 # 组织参数 # 订单id: 20171122181630+用户id order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id) # 运费 transit_price = 10 # 总数目和总金额 total_count = 0 total_price = 0 # 设置事务保存点 save_id = transaction.savepoint() try: # todo: 向df_order_info表中添加一条记录 order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_price=transit_price) # todo: 用户的订单中有几个商品,需要向df_order_goods表中加入几条记录 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id sku_ids = sku_ids.split(',') for sku_id in sku_ids: # 获取商品的信息 try: # select * from df_goods_sku where id=sku_id for update; 悲观锁,解决高并发时库存数量问题 sku = GoodsSKU.objects.select_for_update().get(id=sku_id) except: # 商品不存在, 回滚到事务保存点:save_id transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在'}) # 从redis中获取用户所要购买的商品的数量 count = conn.hget(cart_key, sku_id) # todo: 判断商品的库存 if int(count) > sku.stock: # 回滚到事务保存点:save_id transaction.savepoint_rollback(save_id) return JsonResponse({'res': 6, 'errmsg': '商品库存不足'}) # todo: 向df_order_goods表中添加一条记录 OrderGoods.objects.create(order=order, sku=sku, count=count, price=sku.price) # todo: 更新商品的库存和销量 sku.stock -= int(count) sku.sales += int(count) sku.save() # todo: 累加计算订单商品的总数量和总价格 amount = sku.price * int(count) total_count += int(count) total_price += amount # todo: 更新订单信息表中的商品的总数量和总价格 order.total_count = total_count order.total_price = total_price order.save() except Exception as e: # 如果上面不成功,回滚 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'message': '下单失败'}) # 提交事务,下单生效 transaction.savepoint_commit(save_id) # todo: 清除用户购物车中对应的记录,*具有拆包的作用 conn.hdel(cart_key, *sku_ids) # 返回应答 return JsonResponse({'res': 5, 'message': '创建成功'})
def post(self, request): '''订单创建''' # 判断用户是否登录 user = request.user if not user.is_authenticated(): # 用户未登录 return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接收参数: 收货地址id,支付方式id,商品id addr_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') # 1,3 # 校验参数 if not all([addr_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '参数不完整'}) # 校验支付方式 if pay_method not in OrderInfo.PAY_METHODS.keys(): return JsonResponse({'res': 2, 'errmsg': '非法的支付方式'}) # 校验地址 try: addr = Address.objects.get(id=addr_id) except Address.DoesNotExist: # 地址不存在 return JsonResponse({'res': 3, 'errmsg': '地址非法'}) # todo: 创建订单核心业务 # 组织参数 # 订单id: 20171122181630+用户id order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id) # 运费 transit_price = 10 # 总数目和总金额 total_count = 0 total_price = 0 # 设置事务保存点 save_id = transaction.savepoint() try: # todo: 向df_order_info表中添加一条记录 order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_price=transit_price) # todo: 用户的订单中有几个商品,需要向df_order_goods表中加入几条记录 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id sku_ids = sku_ids.split(',') for sku_id in sku_ids: for i in range(3): # 尝试3次下单 # 获取商品的信息 try: sku = GoodsSKU.objects.get(id=sku_id) except: # 商品不存在, 回滚到事务保存点:save_id transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在'}) # 从redis中获取用户所要购买的商品的数量 count = conn.hget(cart_key, sku_id) # todo: 判断商品的库存 if int(count) > sku.stock: # 回滚到事务保存点:save_id transaction.savepoint_rollback(save_id) return JsonResponse({'res': 6, 'errmsg': '商品库存不足'}) # todo: 更新商品的库存和销量 orgin_stock = sku.stock # 商品的原库存 new_stock = orgin_stock - int(count) # 更新之后的库存 new_sales = sku.sales + int(count) # 更新之后的销量 # 更新good_sku数据表:update (df_goods_sku) set (stock=new_stock, sales=new_sales) # where (id=sku_id and stock = orgin_stock) # 返回更新的数据条数,根据id和库存去查商品 查到对应商品并更新:返回1条数据 查不到(库存被其他用户购买发生改变):返回0条数据 res = GoodsSKU.objects.filter( id=sku_id, stock=orgin_stock).update(stock=new_stock, sales=new_sales) if res == 0: # 如果更新的数据条数为0,表示库存发生了改变,其他用户刚购买了此商品 if i == 2: # 尝试的第3次时, transaction.savepoint_rollback(save_id) # 事务回滚 return JsonResponse({'res': 7, 'errmsg': '下单失败2'}) continue # 结束本次循环,重新读取被其他用户购买之后的库存数,重新下单 # todo: 向df_order_goods表中添加一条记录 OrderGoods.objects.create(order=order, sku=sku, count=count, price=sku.price) # todo: 累加计算订单商品的总数量和总价格 amount = sku.price * int(count) total_count += int(count) total_price += amount break # 如果下单成功跳出后续的循环 # todo: 更新订单信息表中的商品的总数量和总价格 order.total_count = total_count order.total_price = total_price order.save() except Exception as e: # 如果上面不成功,回滚 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'message': '下单失败'}) # 提交事务,下单生效 transaction.savepoint_commit(save_id) # todo: 清除用户购物车中对应的记录,*具有拆包的作用 conn.hdel(cart_key, *sku_ids) # 返回应答 return JsonResponse({'res': 5, 'message': '创建成功'})
def get(self, request, *args, **kwargs): """ 获取服务检测信息 --- parameters: - name: tenantName description: 租户名 required: true type: string paramType: path - name: serviceAlias description: 服务别名 required: true type: string paramType: path - name: check_uuid description: 检测id required: true type: string paramType: query """ sid = None try: check_uuid = request.GET.get("check_uuid", None) if not check_uuid: return Response(general_message(400, "params error", "参数错误,请求参数应该包含请求的ID"), status=400) code, msg, data = app_check_service.get_service_check_info( self.tenant, self.service.service_region, check_uuid) # 如果已创建完成 if self.service.create_status == "complete": # 删除原有build类型env,保存新检测build类型env save_code, save_msg = app_check_service.upgrade_service_env_info( self.tenant, self.service, data) if save_code != 200: logger.debug('======构建时运行参数更新失败=====>{0}'.format(save_msg)) # 重新检测后对端口做加法 save_code, save_msg = app_check_service.add_service_check_port( self.tenant, self.service, data) if save_code != 200: logger.debug('======对检测出来的端口做加法=====>{0}'.format(save_msg)) check_brief_info = app_check_service.wrap_service_check_info( self.service, data) return Response( general_message(200, "success", "请求成功", bean=check_brief_info)) # 开启保存点 sid = transaction.savepoint() logger.debug("start save check info ! {0}".format( self.service.create_status)) save_code, save_msg = app_check_service.save_service_check_info( self.tenant, self.service, data) if save_code != 200: transaction.savepoint_rollback(sid) data["check_status"] = "failure" save_error = { "error_type": "check info save error", "solve_advice": "修改相关信息后重新尝试", "error_info": "{}".format(save_msg) } if data["error_infos"]: data["error_infos"].append(save_error) else: data["error_infos"] = [save_error] else: transaction.savepoint_commit(sid) logger.debug("check result = {0}".format(data)) check_brief_info = app_check_service.wrap_service_check_info( self.service, data) result = general_message(200, "success", "请求成功", bean=check_brief_info) except Exception as e: logger.exception(e) result = error_message(e.message) if sid: transaction.savepoint_rollback(sid) return Response(result, status=result["code"])
def save(self, project, raw=False): # TODO: culprit should default to "most recent" frame in stacktraces when # it's not provided. project = Project.objects.get_from_cache(id=project) data = self.data.copy() # First we pull out our top-level (non-data attr) kwargs event_id = data.pop('event_id') message = data.pop('message') level = data.pop('level') culprit = data.pop('culprit', None) or '' time_spent = data.pop('time_spent', None) logger_name = data.pop('logger', None) server_name = data.pop('server_name', None) site = data.pop('site', None) checksum = data.pop('checksum', None) platform = data.pop('platform', None) release = data.pop('release', None) date = datetime.fromtimestamp(data.pop('timestamp')) date = date.replace(tzinfo=timezone.utc) kwargs = { 'message': message, 'platform': platform, } event = Event(project=project, event_id=event_id, data=data, time_spent=time_spent, datetime=date, **kwargs) # Calculate the checksum from the first highest scoring interface if checksum: hashes = [checksum] else: hashes = get_hashes_for_event(event) # TODO(dcramer): remove checksum usage event.checksum = hashes[0] group_kwargs = kwargs.copy() group_kwargs.update({ 'culprit': culprit, 'logger': logger_name, 'level': level, 'last_seen': date, 'first_seen': date, 'time_spent_total': time_spent or 0, 'time_spent_count': time_spent and 1 or 0, }) tags = data['tags'] tags.append(('level', LOG_LEVELS[level])) if logger_name: tags.append(('logger', logger_name)) if server_name: tags.append(('server_name', server_name)) if site: tags.append(('site', site)) if release: # TODO(dcramer): we should ensure we create Release objects tags.append(('sentry:release', release)) for plugin in plugins.for_project(project): added_tags = safe_execute(plugin.get_tags, event) if added_tags: tags.extend(added_tags) result = safe_execute(self._save_aggregate, event=event, tags=tags, hashes=hashes, **group_kwargs) if result is None: return group, is_new, is_regression, is_sample = result using = group._state.db event.group = group # save the event unless its been sampled if not is_sample: sid = transaction.savepoint(using=using) try: event.save() except IntegrityError: transaction.savepoint_rollback(sid, using=using) return event transaction.savepoint_commit(sid, using=using) sid = transaction.savepoint(using=using) try: EventMapping.objects.create(project=project, group=group, event_id=event_id) except IntegrityError: transaction.savepoint_rollback(sid, using=using) return event transaction.savepoint_commit(sid, using=using) transaction.commit_unless_managed(using=using) if not raw: post_process_group.delay( group=group, event=event, is_new=is_new, is_sample=is_sample, is_regression=is_regression, ) index_event.delay(event) # TODO: move this to the queue if is_regression and not raw: regression_signal.send_robust(sender=Group, instance=group) return event
def post(self, request): # 判断用户是否登录 user = request.user if not user.is_authenticated: # 用户未登录 return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接受参数 addr_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') # 校验参数 if not all([addr_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '数据不完整'}) # 校验支付方式 if pay_method not in OrderInfo.PAY_METHODS.keys(): return JsonResponse({'res': 2, 'errmsg': '非法的支付方式'}) # 校验地址 try: addr = Address.object.get(id=addr_id) except Address.DoesNotExist: # 地址不存在 return JsonResponse({'res': 3, 'errmsg': '地址不存在'}) # todo: 创建订单核心业务 # 组织参数 # 订单id: 年月日时分秒+用户id order_id = datetime.now().strftime('%Y%m%d%H%M%S')+str(user.id) # 运费 transit_price = 10 # 总数目和总金额 total_count = 0 total_price = 0 # 设置事务保存点 save_id = transaction.savepoint() try: # todo: 向df_order_info表汇总添加一条记录 order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_price=transit_price) # todo: 向df_order_goods表中加入记录 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id sku_ids = sku_ids.split(',') for sku_id in sku_ids: # 获取商品的信息 try: # select * from df_goods_sku where id=sku_id for update; sku = GoodsSKU.objects.select_for_update().get(id=sku_id) except: # 商品不存在 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在'}) print('user:%d stock:%d' % (user.id, sku.stock)) import time time.sleep(10) # 从redis中获取用户所要购买的商品的数量 count = conn.hget(cart_key, sku_id) # todo: 判断商品的库存 if int(count) > sku.stock: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 6, 'errmsg': '商品库存不足'}) # 添加记录 OrderGoods.objects.create(order=order, sku=sku, count=count, price=sku.price) # todo: 更新商品的库存和销量 sku.stock -= int(count) sku.sales += int(count) sku.save() # todo: 计算订单商品的总数量和总价格 amount = sku.price*int(count) total_count += int(count) total_price += amount # todo: 更新订单信息表中的商品的总数量和总价格 order.total_count = total_count order.total_price = total_price order.save() except Exception as e: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'errmsg': '库存不足'}) # 提交事务 transaction.savepoint_commit(save_id) # todo: 删除购物车中对应的记录 conn.hdel(cart_key, *sku_ids) # 返回应答 return JsonResponse({'res': 5, 'message': '创建成功'})
def post(self, request, *args, **kwargs): """ 添加团队 --- parameters: - name: tenant_name description: 团队名 required: true type: string paramType: form - name: enterprise_id description: 企业ID required: true type: string paramType: form - name: useable_regions description: 可用数据中心 ali-sh,ali-hz required: false type: string paramType: form """ sid = None try: tenant_name = request.data.get("tenant_name", None) if not tenant_name: return Response( generate_result("1003", "team name is none", "团对名称不能为空")) enterprise_id = request.data.get("enterprise_id", None) if not enterprise_id: return Response( generate_result("1003", "enterprise id is none", "企业ID不能为空")) enter = enterprise_services.get_enterprise_by_enterprise_id( enterprise_id) if not enter: return Response( generate_result("0404", "enterprise not found", "企业在云帮不存在")) team = console_team_service.get_team_by_team_alias_and_eid( tenant_name, enterprise_id) if team: return Response( generate_result("0409", "team alias is exist", "团队别名{0}在该企业已存在".format(tenant_name))) creater = request.data.get("creater", None) if not creater: return Response( generate_result("0412", "please specify owner", "请指定拥有者")) user = user_repo.get_user_by_username(creater) useable_regions = request.data.get("useable_regions", "") logger.debug("team name {0}, usable regions {1}".format( tenant_name, useable_regions)) regions = [] if useable_regions: regions = useable_regions.split(",") # 开启保存点 sid = transaction.savepoint() code, msg, team = console_team_service.create_team( user, enter, regions, tenant_name) # 创建用户在团队的权限 perm_info = { "user_id": user.user_id, "tenant_id": team.ID, "identity": "owner", "enterprise_id": enter.pk } console_perm_service.add_user_tenant_perm(perm_info) for r in regions: code, msg, tenant_region = console_region_service.create_tenant_on_region( team.tenant_name, r) if code != 200: logger.error(msg) if sid: transaction.savepoint_rollback(sid) return Response(generate_result("0500", "add team error", msg), status=code) transaction.savepoint_commit(sid) bean = { "tenant_name": team.tenant_name, "tenant_id": team.tenant_id, "tenant_alias": team.tenant_alias, "user_num": 1 } result = generate_result("0000", "success", "租户添加成功", bean=bean) except TenantOverFlowError as e: result = generate_result("7001", "tenant over flow", "{}".format(e.message)) except TenantExistError as e: result = generate_result("7002", "tenant exist", "{}".format(e.message)) except NoEnableRegionError as e: result = generate_result("7003", "no enable region", "{}".format(e.message)) except UserNotExistError as e: result = generate_result("7004", "not user", "{}".format(e.message)) except Exception as e: logger.exception(e) if sid: transaction.savepoint_rollback(sid) result = generate_error_result() return Response(result)
def post(self, request): """ 保存订单数据 1. 订单基本信息表 和 订单商品表 """ # # 1. 接收参数 transport_id = request.POST.get('transport') sku_ids = request.POST.getlist('sku_ids') address_id = request.POST.get('address') # 接收用户的id user_id = request.session.get("ID") user = Users.objects.get(pk=user_id) # 验证数据的合法性 try: transport_id = int(transport_id) address_id = int(address_id) sku_ids = [int(i) for i in sku_ids] except: return JsonResponse(json_msg(2, "参数错误!")) # 验证收货地址和运输方式存在 try: address = UserAddress.objects.get(pk=address_id) except UserAddress.DoesNotExist: return JsonResponse(json_msg(3, "收货地址不存在!")) try: transport = Transport.objects.get(pk=transport_id) except Transport.DoesNotExist: return JsonResponse(json_msg(4, "运输方式不存在!")) # 2. 操作数据 # 创建保存点 sid = transaction.savepoint() # >>>1 . 操作订单基本信息表 order_sn = "{}{}{}".format(datetime.now().strftime("%Y%m%d%H%M%S"), user_id, random.randrange(10000, 99999)) address_info = "{}{}{}-{}".format(address.hcity, address.hproper, address.harea, address.brief) try: order = Order.objects.create( user=user, order_sn=order_sn, transport_price=transport.price, transport=transport.name, username=address.username, phone=address.phone, address=address_info ) except: return JsonResponse(json_msg(8, "创建订单基本数据失败!")) # >>>2. 操作订单商品表 # 操作redis r = get_redis_connection() cart_key = get_cart_key(user_id) # 准备个变量保存商品总金额 goods_total_price = 0 for sku_id in sku_ids: # 获取商品对象 try: # .select_for_update()悲观锁,解决并发问题 goods_sku = GoodsSKU.objects.select_for_update().get(pk=sku_id, is_delete=False, is_on_sale=True) except GoodsSKU.DoesNotExist: # 回滚数据 transaction.savepoint_rollback(sid) return JsonResponse(json_msg(5, "商品不存在!")) # 获取购物车中商品的数量 # redis 基于内存的存储,有可能数据会丢失 try: count = r.hget(cart_key, sku_id) count = int(count) except: # 回滚数据 transaction.savepoint_rollback(sid) return JsonResponse(json_msg(6, "购物车中数量不存在!")) # 判断库存是否足够 if goods_sku.stock < count: # 回滚数据 transaction.savepoint_rollback(sid) return JsonResponse(json_msg(7, "库存不足!")) # 保存订单商品表 order_goods = OrderGoods.objects.create( order=order, goods_sku=goods_sku, price=goods_sku.price, count=count ) # 添加商品总金额 goods_total_price += goods_sku.price * count # 扣除库存, 销量增加 goods_sku.stock -= count goods_sku.sale_num += count goods_sku.save() # 3. 反过头来操作订单基本信息表 商品总金额 和 订单总金额 # 订单总金额 try: order_price = goods_total_price + transport.price order.goods_total_price = goods_total_price order.order_price = order_price order.save() except: # 回滚 transaction.savepoint_rollback(sid) return JsonResponse(json_msg(9, "更新订单失败!")) # 4. 清空redis中的购物车数据(对应sku_id) r.hdel(cart_key, *sku_ids) # 下单成功, 提交事务 transaction.savepoint_commit(sid) # 3. 合成响应 return JsonResponse(json_msg(0, "创建订单成功!", data=order_sn))
def post(self, request): # 接收数据 data = json.loads(request.body.decode()) address_id = data.get('address_id') pay_method = data.get('pay_method') # 验证数据 if not all([address_id, pay_method]): return JsonResponse({'code': RETCODE.PARAMERR, 'errmsg': '参数不全'}) # 数据入库 # 1 先写入订单基本信息 # 1.1 获取用户信息 user = request.user # 1.2 获取地址信息 try: address = Address.objects.get(id=address_id, user=user) except Address.DoesNotExist: return JsonResponse({'code': RETCODE.PARAMERR, 'errmsg': '地址不正确'}) # 1.3 order_id 年月日时分秒+9位用户id from django.utils import timezone # Y year # m month # d day # H Hour # M Minute # S Second # f 微秒 order_id = timezone.localtime().strftime( '%Y%m%d%H%M%S') + '%09d' % user.id # 1.4 总数量(0),总金额(0),运费 total_count = 0 #总数量 from decimal import Decimal total_amount = Decimal('0') #总金额 freight = Decimal('10.00') #运费 # 小数是以 无限接近于真实值的形式存在 # 100/3 = 33.33 # 33.33*3=99.99 # 33.33 33.33 33.34 # 1.5 支付方式 # if pay_method not in [1,2]: # return JsonResponse({'code':RETCODE.PARAMERR,'errmsg':'支付方式错误'}) # 增加了代码的可读性 if pay_method not in [ OrderInfo.PAY_METHODS_ENUM['CASH'], OrderInfo.PAY_METHODS_ENUM['ALIPAY'] ]: return JsonResponse({'code': RETCODE.PARAMERR, 'errmsg': '支付方式错误'}) # 1.6 支付状态 由支付方式决定 if pay_method == OrderInfo.PAY_METHODS_ENUM['CASH']: # 现金支付 status = OrderInfo.ORDER_STATUS_ENUM['UNSEND'] else: #支付宝 status = OrderInfo.ORDER_STATUS_ENUM['UNPAID'] from django.db import transaction # with 语句就是针对于特定(部分)代码进行事务 with transaction.atomic(): # ① 创建事务的保存点 savepoint = transaction.savepoint() try: order_info = OrderInfo.objects.create( order_id=order_id, user=user, address=address, total_count=total_count, total_amount=total_amount, freight=freight, pay_method=pay_method, status=status) # 2 再写入订单商品信息 # 2.1 获取redis中,指定用户的选中信息 [1,2] redis_conn = get_redis_connection('carts') id_counts = redis_conn.hgetall('carts_%s' % user.id) selected_ids = redis_conn.smembers('selected_%s' % user.id) #选中商品的id #selected_dict selected_dict = {} for id in selected_ids: selected_dict[int(id)] = int(id_counts[id]) # selected_dict = {sku_id:count,sku_id:count,....} # 2.2 根据id查询商品信息 for sku_id, count in selected_dict.items(): while True: sku = SKU.objects.get(id=sku_id) # 2.3 判断商品库存是否充足 if sku.stock < count: #说明库存不足 #② 回滚 transaction.savepoint_rollback(savepoint) return JsonResponse({ 'code': RETCODE.STOCKERR, 'errmsg': '库存不足' }) # 2.4 商品库存减少,销量增加 # import time # time.sleep(7) # sku.stock -= count # sku.sales += count # sku.save() # 乐观锁 # 乐观锁 第一步,先记录库存 old_stock = sku.stock # 乐观所 第二步 计算更新后的数据 new_stock = sku.stock - count new_sales = sku.sales + count #更新前,再判断一次,相同则更新数据 # 乐观所 第三步,更新前,再判断一次,相同则更新数据 rect = SKU.objects.filter( id=sku_id, stock=old_stock).update(stock=new_stock, sales=new_sales) if rect == 0: continue #说明修改失败 # transaction.savepoint_rollback(savepoint) # return JsonResponse({'code':RETCODE.STOCKERR,'errmsg':'下单失败'}) # 2.5 将商品信息写入到订单商品信息表中 OrderGoods.objects.create(order=order_info, sku=sku, count=count, price=sku.price) # 2.6 累加计算,总数量和总金额 order_info.total_count += count order_info.total_amount += (count * sku.price) break # # 3.更新订单基本信息中的总数量和总金额 order_info.save() except Exception as e: transaction.savepoint_rollback(savepoint) else: # ③ 提交 transaction.savepoint_commit(savepoint) # 4.选中的数据应该删除 # redis_conn.hdel('carts_%s'%user.id,*selected_ids) # redis_conn.srem('selected_%s'%user.id,*selected_ids) # 返回相应 return JsonResponse({ 'code': RETCODE.OK, 'errmsg': 'ok', 'order_id': order_info.order_id, 'payment_amount': order_info.total_amount, 'pay_method': order_info.pay_method })
def import_data_inner(self, dataset, dry_run, raise_errors, using_transactions, collect_failed_rows, **kwargs): result = self.get_result_class()() result.diff_headers = self.get_diff_headers() result.total_rows = len(dataset) if using_transactions: # when transactions are used we want to create/update/delete object # as transaction will be rolled back if dry_run is set sp1 = savepoint() try: with atomic_if_using_transaction(using_transactions): self.before_import(dataset, using_transactions, dry_run, **kwargs) except Exception as e: logger.debug(e, exc_info=e) tb_info = traceback.format_exc() result.append_base_error(self.get_error_result_class()(e, tb_info)) if raise_errors: raise instance_loader = self._meta.instance_loader_class(self, dataset) # Update the total in case the dataset was altered by before_import() result.total_rows = len(dataset) if collect_failed_rows: result.add_dataset_headers(dataset.headers) for i, row in enumerate(dataset.dict, 1): with atomic_if_using_transaction(using_transactions): row_result = self.import_row( row, instance_loader, using_transactions=using_transactions, dry_run=dry_run, row_number=i, raise_errors=raise_errors, **kwargs) result.increment_row_result_total(row_result) if row_result.errors: if collect_failed_rows: result.append_failed_row(row, row_result.errors[0]) if raise_errors: raise row_result.errors[-1].error elif row_result.validation_error: result.append_invalid_row(i, row, row_result.validation_error) if collect_failed_rows: result.append_failed_row(row, row_result.validation_error) if raise_errors: raise row_result.validation_error if (row_result.import_type != RowResult.IMPORT_TYPE_SKIP or self._meta.report_skipped): result.append_row_result(row_result) if self._meta.use_bulk: # bulk persist any instances which are still pending with atomic_if_using_transaction(using_transactions): self.bulk_create(using_transactions, dry_run, raise_errors) self.bulk_update(using_transactions, dry_run, raise_errors) self.bulk_delete(using_transactions, dry_run, raise_errors) try: with atomic_if_using_transaction(using_transactions): self.after_import(dataset, result, using_transactions, dry_run, **kwargs) except Exception as e: logger.debug(e, exc_info=e) tb_info = traceback.format_exc() result.append_base_error(self.get_error_result_class()(e, tb_info)) if raise_errors: raise if using_transactions: if dry_run or result.has_errors(): savepoint_rollback(sp1) else: savepoint_commit(sp1) return result
def post(self, request): '''订单创建''' # 判断用户是否登录 user = request.user if not user.is_authenticated(): # 用户未登录 return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接收参数 addr_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') print(pay_method) print(sku_ids) print(addr_id) # 校验参数 if not all([addr_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '数据不完整'}) # 校验支付方式 if pay_method not in OrderInfo.PAY_METHODS.keys(): return JsonResponse({'res': 2, 'errmsg': '非法的支付方式'}) # 校验地址 try: addr = Address.objects.get(id=addr_id) except Address.DoesNotExist: # 地址不存在 return JsonResponse({'res': 3, 'errmsg': '地址非法'}) print(addr) # 创建订单核心业务 # 组织参数 # 订单id:20171122181630+用户id order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id) transit_price = 10 # 总数目和总金额 total_count = 0 total_price = 0 # 设置事务保存点 save_id = transaction.savepoint() try: # 向df_order_info表中添加信息 order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_prices=transit_price) # 用户的订单中有几个商品,需要向df_order_goods表中加入几条记录 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id sku_ids = sku_ids.split(',') for sku_id in sku_ids: for i in range(3): # 获取商品的信息 try: sku = GoodsSKU.objects.get(id=sku_id) except: # 商品不存在 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在'}) # 从redis中获取用户所要购买的商品和数量 count = conn.hget(cart_key, sku_id) # #判断商品的库存 if int(count) > sku.stock: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 6, 'errmsg': '商品库存不足'}) # 更新商品的库存和销量 origin_stock = sku.stock new_stock = origin_stock - int(count) new_sales = sku.sales + int(count) # print('user:%d times:%d stock:%d' %(user.id,i,sku.stock)) # import time # time.sleep(10) #update df_goods_sku set stock=new_stock,sales=new_sales #where id =sku_id and stock = origin_stock #返回受影响的行数 res = GoodsSKU.objects.filter(id=sku_id, stock=origin_stock).update( stock=new_stock, sales=new_sales) if res == 0: if i == 2: #尝试的第三次失败 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'errmsg': '下单失败2'}) continue # 向df_order_goods表中加入记录 OrderGoods.objects.create(order=order, sku=sku, count=count, price=sku.price) # 累加计算订单商品的总数目和总价格 amount = sku.price * int(count) total_count += int(count) total_price += amount #跳出循环 break # 更新订单信息表中的商品总数量和总价格 order.total_count = total_count order.total_price = total_price order.save() except Exception as e: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'errmsg': '下单失败'}) # 提交事务 transaction.savepoint_commit(save_id) # 清除用户购物车中对应的记录 conn.hdel(cart_key, *sku_ids) # 返回应答 return JsonResponse({'res': 5, 'message': '创建成功'})
def post(request, lang): sid = transaction.savepoint() if lang == 'ja': form = VideoForm(request.POST) video_model = Video() else: form = VideoEnForm(request.POST) video_model = VideoEn() if form.errors: messages.add_message(request, messages.INFO, dict(form.errors.items())) if form.is_valid(): try: res_video = video_model.create_video({ 'published_at': form.cleaned_data.get('published_at'), 'title': form.cleaned_data.get('title'), 'text': form.cleaned_data.get('text'), 'youtube_id': form.cleaned_data.get('youtube_id'), }) add_introductions = form.cleaned_data.get('introductions') if add_introductions: video_model.add_introduction(res_video.id, add_introductions) add_categories = form.cleaned_data.get('categories') if add_categories: video_model.add_category(res_video.id, add_categories) add_topics = form.cleaned_data.get('topics') if add_topics: video_model.add_topic(res_video.id, add_topics) transaction.savepoint_commit(sid) return HttpResponseRedirect('/{}/admin/videos'.format(lang)) except: transaction.savepoint_rollback(sid) pass if lang == 'ja': topic_model = Topic() introduction_model = Introduction() else: topic_model = TopicEn() introduction_model = IntroductionEn() select_categories = [] if form.cleaned_data.get('categories'): category_ids = list(map(int, form.cleaned_data.get('categories'))) select_categories = Category.get_by_ids(category_ids) select_introductions = [] if form.cleaned_data.get('introductions'): title_ids = list(map(int, form.cleaned_data.get('introductions'))) select_introductions = introduction_model.get_by_ids(title_ids) select_topics = [] if form.cleaned_data.get('topics'): topic_ids = list(map(int, form.cleaned_data.get('topics'))) select_topics = topic_model.get_by_ids(topic_ids) select_videos = [] if form.cleaned_data.get('videos'): video_ids = list(map(int, form.cleaned_data.get('videos'))) select_videos = video_model.get_by_ids(video_ids) groups = Group.get_all() return TemplateResponse( request, 'video_create.html', { 'title': '新規投稿 | 動画 | FEED App 管理', 'select_categories': select_categories, 'select_introductions': select_introductions, 'select_topics': select_topics, 'select_videos': select_videos, 'groups': groups, 'form_data': form.cleaned_data, 'error_messages': get_error_message(request), 'lang': lang, })
def create(self, validated_data): """ 保存订单 """ # 获取当前下单用户 user = self.context['request'].user # 组织订单编号 20170903153611+user.id # timezone.now() -> datetime order_id = timezone.now().strftime('%Y%m%d%H%M%S') + ('%09d' % user.id) address = validated_data['address'] pay_method = validated_data['pay_method'] # 生成订单 with transaction.atomic(): # 创建一个保存点 save_id = transaction.savepoint() try: # 创建订单信息 order = OrderInfo.objects.create( order_id=order_id, user=user, address=address, total_count=0, total_amount=Decimal(0), freight=Decimal(10), pay_method=pay_method, status=OrderInfo.ORDER_STATUS_ENUM['UNSEND'] if pay_method == OrderInfo.PAY_METHODS_ENUM['CASH'] else OrderInfo.ORDER_STATUS_ENUM['UNPAID']) # 获取购物车信息 redis_conn = get_redis_connection("cart") redis_cart = redis_conn.hgetall("cart_%s" % user.id) cart_selected = redis_conn.smembers('cart_selected_%s' % user.id) # 将bytes类型转换为int类型 cart = {} for sku_id in cart_selected: cart[int(sku_id)] = int(redis_cart[sku_id]) # # 一次查询出所有商品数据 # skus = SKU.objects.filter(id__in=cart.k0eys()) # 处理订单商品 sku_id_list = cart.keys() for sku_id in sku_id_list: while True: sku = SKU.objects.get(id=sku_id) sku_count = cart[sku.id] # 判断库存 origin_stock = sku.stock # 原始库存 origin_sales = sku.sales # 原始销量 if sku_count > origin_stock: transaction.savepoint_rollback(save_id) raise serializers.ValidationError('商品库存不足') # 用于演示并发下单 import time time.sleep(5) # 减少库存 # sku.stock -= sku_count # sku.sales += sku_count # sku.save() new_stock = origin_stock - sku_count new_sales = origin_sales + sku_count # 根据原始库存条件更新,返回更新的条目数,乐观锁 ret = SKU.objects.filter(id=sku.id, stock=origin_stock).update( stock=new_stock, sales=new_sales) if ret == 0: continue # 累计商品的SPU 销量信息 sku.goods.sales += sku_count sku.goods.save() # 累计订单基本信息的数据 order.total_count += sku_count # 累计总金额 order.total_amount += (sku.price * sku_count) # 累计总额 # 保存订单商品 OrderGoods.objects.create( order=order, sku=sku, count=sku_count, price=sku.price, ) # 更新成功 break # 更新订单的金额数量信息 order.total_amount += order.freight order.save() except serializers.ValidationError: raise except Exception as e: logger.error(e) transaction.savepoint_rollback(save_id) raise # 提交事务 transaction.savepoint_commit(save_id) # 更新redis中保存的购物车数据 pl = redis_conn.pipeline() pl.hdel('cart_%s' % user.id, *cart_selected) pl.srem('cart_selected_%s' % user.id, *cart_selected) pl.execute() return order
def post(self, request): user = request.user if not user.is_authenticated: return JsonResponse({'res': 0, 'errmsg': '用户未登录!'}) addr_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') if not all([addr_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '参数不完整!'}) # todo 校验支付方式 if pay_method not in OrderInfo.PAY_METHODS.keys(): return JsonResponse({'res': 2, 'errmsg': '非法的支付方式!'}) try: addr = Address.objects.get(pk=addr_id) except Address.DoesNotExist: return JsonResponse({'res': 3, 'errmsg': '地址非法'}) # TODO: 创建订单 order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id) # 运费 transit_price = 10 # 假数据, 写死 total_count = 0 total_price = 0 coon = get_redis_connection('default') cart_key = 'cart_%d' % user.id # 设置保存点 save_id = transaction.savepoint() try: # TODO 想订单信息表中添加一条记录 order = OrderInfo.objects.create(order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_price=transit_price) # todo 用户的订单中有几个商品就要添加几条记录 sku_ids = sku_ids.split(',') for sku_id in sku_ids: try: sku = GoodsSKU.objects.select_for_update().get( id=sku_id) # 悲观锁 except GoodsSKU.DoesNotExist: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在!'}) # 获取商品的数目 count = coon.hget(cart_key, sku_id) # todo 判断商品的库存 if int(count) > sku.stock: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 6, 'errmsg': '商品库存不足'}) # todo to订单信息表中添加一条记录 OrderGoods.objects.create( order=order, sku=sku, count=int(count), price=sku.price, ) # todo 更新商品的库存销量和库存 sku.stock -= int(count) sku.sales += int(count) sku.save() # todo 累加计算商品的订单的总数量和总价格 amount = sku.price * int(count) total_count += int(count) total_price += amount # todo 更新订单信息表中的商品的总数量和价格 order.total_count = total_count order.total_price = total_price order.save() except Exception: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'errmsg': '下单失败!'}) # 提交 transaction.savepoint_commit(save_id) # todo 清楚用户的购物车记录 coon.hdel(cart_key, *sku_ids) return JsonResponse({'res': 5, 'message': '创建成功!'})
def post(self, request): '''订单创建''' # 判断用户是否登陆 user = request.user if not user.is_authenticated(): # 用户未登录 return JsonResponse({'res': 0, 'errmsg': '用户未登录'}) # 接收参数 adder_id = request.POST.get('addr_id') pay_method = request.POST.get('pay_method') sku_ids = request.POST.get('sku_ids') # 参数校验 if not all([adder_id, pay_method, sku_ids]): return JsonResponse({'res': 1, 'errmsg': '数据出错'}) # 校验支付方式 if pay_method not in OrderInfo.PAY_METHOD.keys(): return JsonResponse({'res': 2, 'errmsg': '非法支付方式'}) # 校验地址 try: addr = Address.objects.get(id=adder_id) except Address.DoesNotExist: return JsonResponse({'res': 3, 'errmsg': '地址非法'}) # todo:创建订单核心业务 # 组织参数 # 订单id: order_id = datetime.now().strftime('%Y%m%d%H%M%S') + str(user.id) # 运费 transit_price = 10 # 总数目与总价格 total_count = 0 total_price = 0 # 设置事物保存点 save_id = transaction.savepoint() try: # todo:向df_order_info表中添加一条记录 order = OrderInfo.objects.create( order_id=order_id, user=user, addr=addr, pay_method=pay_method, total_count=total_count, total_price=total_price, transit_price=transit_price, ) # todo: 用户订单有几个商品, 需要向df_order_goods表中加入几条记录 conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id sku_ids = sku_ids.split(',') for sku_id in sku_ids: # 获取商品的信息 try: # select * from df_goods_sku where id=sku_id for update; sku = GoodsSKU.objects.select_for_update().get( id=sku_id) # 加锁 except GoodsSKU.DoesNotExist: # 商品不存在,回滚保存点 transaction.savepoint_rollback(save_id) return JsonResponse({'res': 4, 'errmsg': '商品不存在'}) # 从redis中拿出商品的数目 count = conn.hget(cart_key, sku_id) # todo: 判断商品的库存 if int(count) > sku.stock: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 5, 'errmsg': '商品库存不足'}) # todo:向df_order_goods表中添加一条记录 OrderGoods.objects.create(order=order_id, sku=sku, count=count, price=sku.price) # 更新商品的库存与销量 sku.stock -= int(count) sku.sales += int(count) sku.save() # 累加计算订单商品的总数目和总价格 amount = sku.price * int(count) total_count += int(count) total_price += amount # todo:更新订单信息表中商品的总数量和总价格 order.total_count = total_count order.total_price = total_price order.save() except Exception: transaction.savepoint_rollback(save_id) return JsonResponse({'res': 7, 'message': '下单失败'}) # 提交事务 transaction.savepoint_commit(save_id) # todo:清除购物车中·对应的记录 conn.hdel(cart_key, *sku_ids) # 返回应答 return JsonResponse({'res': 6, 'message': '订单创建成功'})
def get_or_create_map(generator, args): """ A wrapper function for a map generator which protects against race conditions in map generation. You should use this instead of calling a generator directly. @param generator: The generator to use @param args: Any extra arguments to pass to the generator function This assumes that generator is functional, i.e. its return value is determined solely by its arguments. In the case that a map is requested again before the original map generation has finished, the following happens: * Nothing has yet been saved to the database as we have to wait for the metadata to come back. Hence, we get a DoesNotExist. * The map is regenerated. Due to the atomic nature of filesystem writes we can guarantee that we won't get a funnily spliced file written by the function f. * The first call saves the GeneratedMap instance to the database. * The second call attempts to do the same, but it would result in a duplicate primary key, and so it throws an IntegrityError. * Due to the functional nature of f, we know we have the correct metadata for the map that has been generated, and that it will match that already stored in the database. As such, we can leave the database alone and return the metadata just generated. Assuming that if we get a DoesNotExist that there won't be one by the time we come to write leads to a race condition given the non-zero duration of f. Subsequent attempts to get the map with that hash then throws a MultipleObjectsReturned exception. Yes, this did happen. Seven times, no less. """ start_time = time.time() # Generate a hash based on the arguments for this map hash = hashlib.sha224(pickle.dumps(repr(args))).hexdigest()[:16] # Try fetching the map if it already exists try: generated_map = GeneratedMap.objects.get(hash=hash) generated_map.last_accessed = datetime.utcnow() generated_map.save() metadata = generated_map.metadata # Except if the current map is marked as faulty (e.g., missing OSM tile, # in which case we delete it and raise DoesNotExist to get it # regenerated) if generated_map.faulty: generated_map.delete() logger.debug("Found previously generated map: %s, but it's " + \ "faulty, so regenerating", hash) raise GeneratedMap.DoesNotExist() else: logger.debug("Found previously generated map: %s", hash) # If it doesn't exist, generate it except GeneratedMap.DoesNotExist: generated_map_dir = get_generated_map_dir() if not os.path.exists(generated_map_dir): os.makedirs(generated_map_dir) try: # Call the generator to generate it metadata = generator(filename=os.path.join(generated_map_dir, hash), *args) except MapGenerationError as e: # If a map generation error occurs, then mark this map as faulty # this means that it is deleted next time it is requested, forcing # it to be re-generated next time (hopefully the error is transient) metadata = e.metadata faulty = True else: # If no exception was raised, we mark it as non-faulty faulty = False generated_map = GeneratedMap( hash = hash, generated = datetime.utcnow(), last_accessed = datetime.utcnow(), faulty = faulty, ) generated_map.metadata = metadata # This may fail, so we use a transaction savepoint in case we need to # roll back the transaction - not doing this causes any future database # queries to fail savepoint = transaction.savepoint() try: generated_map.save() except IntegrityError: # This means a race error was generated, but because of the # functional nature of generator, we can carry on here logger.debug("Map generated: %s, took %.5f seconds (with race)", hash, time.time()-start_time) transaction.savepoint_rollback(savepoint) else: logger.debug("Map generated: %s, took %.5f seconds", hash, time.time()-start_time) transaction.savepoint_commit(savepoint) # If there are any maps older than a week, regenerate them to_delete = GeneratedMap.objects.filter( generated__lt=datetime.now()-timedelta(weeks=1)).order_by('generated') if to_delete.count() > 0: # But only clear up 50 at a time try: youngest = to_delete[0].last_accessed to_delete = to_delete[:50] for generated_map in to_delete: generated_map.delete() age = (datetime.now()-youngest) age = age.days*24 + age.seconds/3600.0 logger.info("Cleared out old maps, youngest is %f hours", age) except IndexError: logger.info("Maps disappeared whilst trying to delete - race condition?", exc_info=True) return hash, metadata
def order_handle(request): tran_id = transaction.savepoint() #接受购物车编号 cart_ids = request.POST.get('cart_ids') # print(cart_ids) # print('输出cart_ids') try: #创建订单对象 order = OrderInfo() now = datetime.now() uid = request.session['user_id'] order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid order.odate = now # print('在total之前') order.ototal = Decimal(request.POST.get('total')) # print('在total之后') order.save() #创建详单对象 # print('创建详单') cart_ids1 = [int(item) for item in cart_ids.split(',')] #[45, 46] # print(cart_ids1) for id1 in cart_ids1: detail = OrderDetailInfo() detail.order = order #查询购物车信息 # print('查询购物车信息') cart = CartInfo.objects.get(id=id1) #盘攒商品库存 # print('盘赞商品库存') goods = cart.goods if goods.gkucun >= cart.count: #如果库存大于购买数量 #判断商品库存 # print('判断商品库存') goods.gkucun = cart.goods.gkucun - cart.count goods.save() #完善订单信息 # print('完善订单信息') detail.goods_id = goods.id detail.price = goods.gprice detail.count = cart.count detail.save() # print('保存订单信息') #删除购物车数据 cart.delete() # print('删除订单信息') else: #如果库存小于购买数量 transaction.savepoint_rollback(tran_id) return redirect('/cart/') #return HttpResponse('no') transaction.savepoint_commit(tran_id) except Exception as e: print('===================%s' % e) transaction.savepoint_rollback(tran_id) # return HttpResponse('ok') # print('ok') return redirect('/user/order/')
def import_data(self, dataset, dry_run=False, raise_errors=False, use_transactions=None, **kwargs): """ Imports data from ``tablib.Dataset``. Refer to :doc:`import_workflow` for a more complete description of the whole import process. :param dataset: A ``tablib.Dataset`` :param raise_errors: Whether errors should be printed to the end user or raised regularly. :param use_transactions: If ``True`` import process will be processed inside transaction. :param dry_run: If ``dry_run`` is set, or error occurs, transaction will be rolled back. """ result = self.get_result_class()() result.diff_headers = self.get_diff_headers() result.totals = OrderedDict([(RowResult.IMPORT_TYPE_NEW, 0), (RowResult.IMPORT_TYPE_UPDATE, 0), (RowResult.IMPORT_TYPE_DELETE, 0), (RowResult.IMPORT_TYPE_SKIP, 0), (RowResult.IMPORT_TYPE_ERROR, 0), ('total', len(dataset))]) if use_transactions is None: use_transactions = self.get_use_transactions() if use_transactions is True: # when transactions are used we want to create/update/delete object # as transaction will be rolled back if dry_run is set real_dry_run = False sp1 = savepoint() else: real_dry_run = dry_run try: self.before_import(dataset, real_dry_run, **kwargs) except Exception as e: logging.exception(e) tb_info = traceback.format_exc() result.base_errors.append(self.get_error_result_class()(e, tb_info)) if raise_errors: if use_transactions: savepoint_rollback(sp1) raise instance_loader = self._meta.instance_loader_class(self, dataset) # Update the total in case the dataset was altered by before_import() result.totals['total'] = len(dataset) for row in dataset.dict: row_result = self.import_row(row, instance_loader, real_dry_run, **kwargs) if row_result.errors: result.totals[row_result.IMPORT_TYPE_ERROR] += 1 if raise_errors: if use_transactions: savepoint_rollback(sp1) raise row_result.errors[-1].error else: result.totals[row_result.import_type] += 1 if (row_result.import_type != RowResult.IMPORT_TYPE_SKIP or self._meta.report_skipped): result.append_row_result(row_result) try: self.after_import(dataset, result, real_dry_run, **kwargs) except Exception as e: logging.exception(e) tb_info = traceback.format_exc() result.base_errors.append(self.get_error_result_class()(e, tb_info)) if raise_errors: if use_transactions: savepoint_rollback(sp1) raise if use_transactions: if dry_run or result.has_errors(): savepoint_rollback(sp1) else: savepoint_commit(sp1) return result
def import_data(self, dataset, dry_run=False, raise_errors=False, use_transactions=None, **kwargs): """ Imports data from ``dataset``. ``use_transactions`` If ``True`` import process will be processed inside transaction. If ``dry_run`` is set, or error occurs, transaction will be rolled back. """ result = Result() result.diff_headers = self.get_diff_headers() if use_transactions is None: use_transactions = self.get_use_transactions() if use_transactions is True: # when transactions are used we want to create/update/delete object # as transaction will be rolled back if dry_run is set real_dry_run = False sp1 = savepoint() else: real_dry_run = dry_run try: self.before_import(dataset, real_dry_run, **kwargs) except Exception as e: logging.exception(e) tb_info = traceback.format_exc(2) result.base_errors.append(Error(repr(e), tb_info)) if raise_errors: if use_transactions: savepoint_rollback(sp1) raise instance_loader = self._meta.instance_loader_class(self, dataset) for row in dataset.dict: try: row_result = RowResult() instance, new = self.get_or_init_instance(instance_loader, row) if new: row_result.import_type = RowResult.IMPORT_TYPE_NEW else: row_result.import_type = RowResult.IMPORT_TYPE_UPDATE row_result.new_record = new original = deepcopy(instance) if self.for_delete(row, instance): if new: row_result.import_type = RowResult.IMPORT_TYPE_SKIP row_result.diff = self.get_diff( None, None, real_dry_run) else: row_result.import_type = RowResult.IMPORT_TYPE_DELETE self.delete_instance(instance, real_dry_run) row_result.diff = self.get_diff( original, None, real_dry_run) else: self.import_obj(instance, row, real_dry_run) if self.skip_row(instance, original): row_result.import_type = RowResult.IMPORT_TYPE_SKIP else: self.save_instance(instance, real_dry_run) self.save_m2m(instance, row, real_dry_run) # Add object info to RowResult for LogEntry row_result.object_repr = force_text(instance) row_result.object_id = instance.pk row_result.diff = self.get_diff(original, instance, real_dry_run) except Exception as e: # There is no point logging a transaction error for each row # when only the original error is likely to be relevant if not isinstance(e, TransactionManagementError): logging.exception(e) tb_info = traceback.format_exc(2) row_result.errors.append(Error(e, tb_info, row)) if raise_errors: if use_transactions: savepoint_rollback(sp1) six.reraise(*sys.exc_info()) if (row_result.import_type != RowResult.IMPORT_TYPE_SKIP or self._meta.report_skipped): result.rows.append(row_result) if use_transactions: if dry_run or result.has_errors(): savepoint_rollback(sp1) else: savepoint_commit(sp1) return result
def update(self, request, project_pk, pk=None): errors = {} is_valid = True nested_data = dict( sample_event=request.data.get("sample_event"), quadrat_collection=request.data.get("quadrat_collection"), observers=request.data.get("observers"), obs_quadrat_benthic_percent=request.data.get( "obs_quadrat_benthic_percent"), obs_colonies_bleached=request.data.get("obs_colonies_bleached"), ) bleaching_qc_data = { k: v for k, v in request.data.items() if k not in nested_data } bleaching_qc_id = bleaching_qc_data["id"] context = dict(request=request) # Save models in a transaction sid = transaction.savepoint() try: bleaching_qc = BleachingQuadratCollection.objects.get( id=bleaching_qc_id) # Observers check, errs = save_one_to_many( foreign_key=("transectmethod", bleaching_qc_id), database_records=bleaching_qc.observers.all(), data=request.data.get("observers") or [], serializer_class=ObserverSerializer, context=context, ) if check is False: is_valid = False errors["observers"] = errs # Observations - Colonies Bleached check, errs = save_one_to_many( foreign_key=("bleachingquadratcollection", bleaching_qc_id), database_records=bleaching_qc.obscoloniesbleached_set.all(), data=request.data.get("obs_colonies_bleached") or [], serializer_class=ObsColoniesBleachedSerializer, context=context, ) if check is False: is_valid = False errors["obs_colonies_bleached"] = errs # Observations - Quadrat Benthic Percent check, errs = save_one_to_many( foreign_key=("bleachingquadratcollection", bleaching_qc_id), database_records=bleaching_qc.obsquadratbenthicpercent_set.all( ), data=request.data.get("obs_quadrat_benthic_percent") or [], serializer_class=ObsQuadratBenthicPercentSerializer, context=context, ) if check is False: is_valid = False errors["obs_quadrat_benthic_percent"] = errs # Sample Event check, errs = save_model( data=nested_data["sample_event"], serializer_class=SampleEventSerializer, context=context, ) if check is False: is_valid = False errors["sample_event"] = errs # Quadrat Collection check, errs = save_model( data=nested_data["quadrat_collection"], serializer_class=QuadratCollectionSerializer, context=context, ) if check is False: is_valid = False errors["quadrat_collection"] = errs # Bleaching Quadrat Collection check, errs = save_model( data=bleaching_qc_data, serializer_class=BleachingQuadratCollectionSerializer, context=context, ) if check is False: is_valid = False errors["bleaching_quadrat_collection"] = errs if is_valid is False: transaction.savepoint_rollback(sid) return Response(data=errors, status=status.HTTP_400_BAD_REQUEST) clean_sample_event_models(nested_data["sample_event"]) transaction.savepoint_commit(sid) bleaching_qc = BleachingQuadratCollection.objects.get( id=bleaching_qc_id) return Response( BleachingQuadratCollectionMethodSerializer(bleaching_qc).data, status=status.HTTP_200_OK, ) except: transaction.savepoint_rollback(sid) raise
def _create_sampleSetItem(csvSampleDict, request, user, sampleSet_ids): sampleDisplayedName = csvSampleDict.get(COLUMN_SAMPLE_NAME, '').strip() sampleExtId = csvSampleDict.get(COLUMN_SAMPLE_EXT_ID, '').strip() sampleGender = csvSampleDict.get(COLUMN_GENDER, '').strip() sampleControlType = csvSampleDict.get(COLUMN_CONTROLTYPE, '').strip() sampleGroupType = csvSampleDict.get(COLUMN_GROUP_TYPE, None) sampleGroup = csvSampleDict.get(COLUMN_GROUP, '0').strip() sampleDescription = csvSampleDict.get(COLUMN_SAMPLE_DESCRIPTION, '').strip() barcodeKit = csvSampleDict.get(COLUMN_BARCODE_KIT, '').strip() barcodeAssignment = csvSampleDict.get(COLUMN_BARCODE, '').strip() nucleotideType = csvSampleDict.get( COLUMN_NUCLEOTIDE_TYPE, "").strip() or csvSampleDict.get( ALTERNATE_COLUMN_NUCLEOTIDE_TYPE, "").strip() cancerType = csvSampleDict.get(COLUMN_CANCER_TYPE, "").strip() cellularityPct = csvSampleDict.get(COLUMN_CELLULARITY_PCT, None).strip() pcrPlateRow = csvSampleDict.get(COLUMN_PCR_PLATE_POSITION, "").strip() biopsyDays = csvSampleDict.get(COLUMN_BIOPSY_DAYS, "0").strip() coupleId = csvSampleDict.get(COLUMN_COUPLE_ID, None).strip() embryoId = csvSampleDict.get(COLUMN_EMBRYO_ID, "").strip() if not sampleGroup: sampleGroup = '0' isValid, errorMessage, nucleotideType_internal_value = sample_validator.validate_nucleotideType( nucleotideType) # validation has been done already, this is just to get the official value isValid, errorMessage, gender_CV_value = sample_validator.validate_sampleGender( sampleGender) isValid, errorMessage, role_CV_value = sample_validator.validate_sampleGroupType( sampleGroupType) isValid, errorMessage, controlType_CV_value = sample_validator.validate_controlType( sampleControlType) isValid, errorMessage, cancerType_CV_value = sample_validator.validate_cancerType( cancerType) isValid, errorMessage, pcrPlateRow_internal_value = sample_validator.validate_pcrPlateRow( pcrPlateRow) sampleName = sampleDisplayedName.replace(' ', '_') sample_kwargs = { 'displayedName': sampleDisplayedName, 'status': 'created', 'description': sampleDescription, 'date': timezone.now() ##datetime.datetime.now() } sample, isCreated = Sample.objects.get_or_create(name=sampleName, externalId=sampleExtId, defaults=sample_kwargs) if isCreated: logger.debug( "import_sample_processor._create_sampleSetItem() new sample created for sample=%s; id=%d" % (sampleDisplayedName, sample.id)) else: if (sample.description != sampleDescription): sample.description = sampleDescription sample.save() logger.debug( "import_sample_processor._create_sampleSetItem() just updated sample description for sample=%s; id=%d" % (sampleDisplayedName, sample.id)) # logger.debug("import_sample_processor._create_sampleSetItem() after get_or_create isCreated=%s; sample=%s; sample.id=%d" %(str(isCreated), sampleDisplayedName, sample.id)) for sampleSetId in sampleSet_ids: logger.debug( "import_sample_processor._create_sampleSetItem() going to create sampleSetItem for sample=%s; sampleSetId=%s in sampleSet_ids=%s" % (sampleDisplayedName, str(sampleSetId), sampleSet_ids)) currentDateTime = timezone.now() ##datetime.datetime.now() dnabarcode = None if barcodeKit and barcodeAssignment: dnabarcode = models.dnaBarcode.objects.get( name__iexact=barcodeKit, id_str__iexact=barcodeAssignment) pcrPlateColumn = "1" if pcrPlateRow_internal_value else "" sampleSetItem_kwargs = { 'gender': gender_CV_value, 'relationshipRole': role_CV_value, 'relationshipGroup': sampleGroup, 'cancerType': cancerType_CV_value, 'cellularityPct': cellularityPct if cellularityPct else None, 'biopsyDays': int(biopsyDays) if biopsyDays else 0, "coupleId": coupleId, "embryoId": embryoId, 'creator': user, 'creationDate': currentDateTime, 'lastModifiedUser': user, 'lastModifiedDate': currentDateTime, 'description': sampleDescription, 'controlType': controlType_CV_value } sampleSetItem, isCreated = SampleSetItem.objects.get_or_create( sample=sample, sampleSet_id=sampleSetId, dnabarcode=dnabarcode, description=sampleDescription, nucleotideType=nucleotideType_internal_value, pcrPlateRow=pcrPlateRow_internal_value, pcrPlateColumn=pcrPlateColumn, defaults=sampleSetItem_kwargs) logger.debug( "import_sample_processor._create_sampleSetItem() after get_or_create isCreated=%s; sampleSetItem=%s; samplesetItem.id=%d" % (str(isCreated), sampleDisplayedName, sampleSetItem.id)) ssi_sid = transaction.savepoint() return sample, sampleSetItem, ssi_sid
def post(self, request): IsAuthenticate2.has_permission(self, request, "modules:enterprise:save") api_response = {"code": 0, "msg": "success"} if 'token' not in request.data or request.data.get('token') is "": raise ValidationError("参数不能为空") token = request.data.get("token") if 'etpName' not in request.data or request.data.get('etpName') is "": raise ValidationError("参数不能为空") etpName = request.data.get('etpName') if 'LDAPCode' not in request.data or request.data.get( 'LDAPCode') is "": raise ValidationError("参数不能为空") LDAPCode = request.data.get('LDAPCode') etpCode = LDAPCode + "_" + str(int(time.time() * 10)) if 'serverInfolist' not in request.data or request.data.get( 'serverInfolist') is "": raise ValidationError("参数不能为空") serverInfolist = request.data.get('serverInfolist') for serverinfo in serverInfolist: if 'serverType' not in serverinfo or serverinfo.get( 'serverType') is "": raise ValidationError("参数不能为空") serverType = serverinfo.get('serverType') try: dhcpServerIP = (request.data.get('dhcpServerIP') if ('dhcpServerIP' in request.data) else "") TFTPServerIP = (request.data.get('TFTPServerIP') if ('TFTPServerIP' in request.data) else "") FTPServerIP = (request.data.get('FTPServerIP') if ('FTPServerIP' in request.data) else "") accName = rc4_decode_main(secretkey, token) createAdmin = accName createTime = (request.data.get('createTime') if ('createTime' in request.data) else datetime.now()) sid = transaction.savepoint() # 开启事务 newenterprise = Enterprise() newenterprise.etpName = etpName newenterprise.LDAPCode = LDAPCode newenterprise.etpCode = etpCode newenterprise.dhcpServerIP = dhcpServerIP newenterprise.TFTPServerIP = TFTPServerIP newenterprise.FTPServerIP = FTPServerIP newenterprise.createAdmin = createAdmin newenterprise.createTime = createTime newenterprise.save() for serverinfo in serverInfolist: serverIP = (serverinfo.get('serverIP') if ('serverIP' in serverinfo) else "") serverUsername = (serverinfo.get('serverUsername') if ('serverUsername' in serverinfo) else "") serverPasswd = (serverinfo.get('serverPasswd') if ('serverPasswd' in serverinfo) else "") newServer = Server() newServer.etpCode = etpCode newServer.serverType = serverType newServer.serverIP = serverIP newServer.serverUsername = serverUsername newServer.serverPasswd = serverPasswd newServer.save() try: transaction.savepoint_commit(sid) # 提交 except Exception as ie: Logger('error.log', level='error').logger.error( "[事务提交失败]accName:【%s】%s" % (accName, ie)) transaction.savepoint_rollback(sid) # 回滚 return Response(api_response) except Exception as e: Logger('error.log', level='error').logger.error( "[添加企业异常]accName:【%s】%s" % (accName, e)) api_response["code"] = 500 api_response["msg"] = "服务器未知错误" return Response(api_response), 500
def form_valid(self, form): """"Empresa Crete View form valid.""" self.object = form.save(commit=False) sid = transaction.savepoint() print("______________0_________________") try: print(self.request.POST.get('')) print("======//////////////////========") print('data_venta' in self.request.POST.keys()) venta = json.loads(self.request.POST.get('data_venta')) print("______________1_________________") print(venta) print(self.request.POST.get('cliente')) cantidadVenta = Venta.objects.all().count() def float_round(num, places=0, direction=floor): return direction(num * (10**places)) / float(10**places) self.object.total = venta['total'] self.object.igv = venta['igv'] self.object.cliente_id = venta['cliente'] self.object.codigo = str(''.join((8 - len(str(cantidadVenta))) * ["0"])) + str(cantidadVenta + 1) self.object.save() for p in venta['productos']: producto = Producto.objects.get(pk=p['id']) producto.existencia = producto.existencia - int(p['cantidad']) producto.MontoReal = float_round( producto.MontoReal - (Decimal(p['importe']) - Decimal(p['importe'] * 0.18)), 2, ceil) producto.igv = float_round(producto.igv - Decimal(p['igvp']), 2, ceil) print(producto.igv) producto.save() # producto.cantidad = producto.cantidad + int(p['cantidad']) # print(producto) # producto.update() dv = Detalle_Venta( producto_id=p['id'], venta=self.object, cantidad=p['cantidad'], # igv=p['igv'], importe=p['importe'], ) dv.save() print("__________________", dv) except Exception as e: print("ERRRRRRRRRRRRRRRRRRRRRRRR", e) try: transaction.savepoint_rollback(sid) except: pass messages.error(self.request, e) msg = _(' %(name)s "%(obj)s" fue creado satisfactoriamente.') % { 'name': capfirst(force_text(self.model._meta.verbose_name)), 'obj': force_text(self.object) } messages.success(self.request, msg) log.warning(msg, extra=log_params(self.request)) return super(VentasView, self).form_valid(form)