def handle_noargs(self, *args, **options): database = [] print "Init done" for query_info in conf.IPGEOBASE_COUNTRY_INFO: content = self.get_contents(query_info) for line in content.decode(query_info['coding']).split("\n"): line = line.strip() if not line: continue row = self.get_row(line.split("\t"), query_info) row = self.preprocess_row(row) database.append( (row['start_ip'], row['end_ip'], row['city'], row['region'], row['country'], row['ip_block']), ) try: cursor = connection.cursor() cursor.execute("TRUNCATE TABLE django_ipgeobase_ipgeobase") print cursor.executemany(""" INSERT INTO django_ipgeobase_ipgeobase (start_ip, end_ip, city_id, region_id, country_id, ip_block) VALUES (%s, %s, %s, %s, %s, %s ) """, database) transaction.commit() except Exception, e: transaction.rollback() message = "The data not updated:", e if send_message: mail_admins(subject=ERROR_SUBJECT, message=message) raise CommandError(message)
def add_book(request, groupid): """ Django View. Add new book to the group. @type request: C{django.http.HttpRequest} @param request: Client Request object @type groupid: C{string} @param groupid: Unique group name """ if not request.POST.has_key("book"): return pages.ErrorPage(request, "500.html") book = models.Book.objects.get(url_title=request.POST["book"]) group = models.BookiGroup.objects.get(url_name=groupid) book.group = group try: book.save() except: transaction.rollback() else: transaction.commit() return HttpResponseRedirect(reverse("view_group", args=[group.url_name]))
def create_views(sender, **kwargs): cursor = connection.cursor() app = kwargs['app'] if type(app) == str: import sys app = getattr(sys.modules[app], 'models') for model in models.get_models(app): if issubclass(model, View): # Check if view exists sql = model.create_check() cursor.execute(*sql) result = cursor.fetchone() if not result[0]: # Create View sql = model.create_sql() cursor.execute(*sql) if issubclass(model, MatView): sql = MatView.storedproc_check() expected_resp = sql[2] sql = sql[:2] cursor.execute(*sql) res = cursor.fetchall() if res[0][0] != expected_resp: for sql in MatView.storedproc_sql(): cursor.execute(sql, ()) func = model.create_matview() try: cursor.execute(*func) transaction.commit() except DatabaseError as e: if e.message.startswith('MatView') and e.message.find('already exists') != -1: transaction.rollback() else: raise
def handle(self, path, **options): database = options.get('database') transripts = options.get('transripts') stdout = options.get('stdout') with open(path) as fin: stream = EffectStream(fin, skip_existing=False) if stdout: while True: line = stream.readline() if line == '': break log.debug(line) else: cursor = connections[database].cursor() with transaction.commit_manually(database): try: cursor.execute('TRUNCATE {0}'.format( VariantEffect._meta.db_table)) if transripts: cursor.execute('TRUNCATE {0} CASCADE'.format( Transcript._meta.db_table)) columns = stream.output_columns db_table = VariantEffect._meta.db_table pgcopy_batch(stream, db_table, columns, cursor, database) transaction.commit(database) except Exception as e: transaction.rollback(database) log.exception(e) raise
def null_ok(eat_exception=True): from django.db import connection, transaction # the DBAPI introspection module fails on postgres NULLs. cursor = connection.cursor() # SQLite has weird now() if db.backend_name == "sqlite3": now_func = "DATETIME('NOW')" # So does SQLServer... should we be using a backend attribute? elif db.backend_name == "pyodbc": now_func = "GETDATE()" elif db.backend_name == "oracle": now_func = "SYSDATE" else: now_func = "NOW()" try: if db.backend_name == "pyodbc": cursor.execute("SET IDENTITY_INSERT southtest_spam ON;") cursor.execute("INSERT INTO southtest_spam (id, weight, expires, name) VALUES (100, NULL, %s, 'whatever');" % now_func) except: if eat_exception: transaction.rollback() return False else: raise else: cursor.execute("DELETE FROM southtest_spam") transaction.commit() return True
def account_authsystem_add(request, account): USERNAME, PASSWORD = '******', 'password' if request.POST.has_key(USERNAME): username = request.POST[USERNAME] else: return HttpResponseBadRequest('No username') try: system = AuthSystem.objects.get(short_name = request.POST['system']) #Doesn't account for same account within two different auth systems #if account.auth_systems.count() > 0: # return HttpResponseBadRequest() account.auth_systems.create(username = username.lower().strip(), auth_system = system) except AuthSystem.DoesNotExist: raise PermissionDenied() except: # SZ: Remove once integrity constraint/transactions/get_or_replace() issue is resolved from django.db import transaction transaction.rollback() return HttpResponseBadRequest() if system == AuthSystem.PASSWORD() and request.POST.has_key(PASSWORD): account.password_set(request.POST[PASSWORD]) account.set_state(ACTIVE_STATE) account.save() # return the account info instead return DONE
def slots(request, eventid): event = Event.objects.get(id=eventid) form = SlotAppForm() form.loadFromEvent(event,Player.objects.get(user=request.user)) if request.method == "POST": form.setData(request.POST) form.validate() if form.is_valid(): if form.save(event, request.user): transaction.commit() return HttpResponseRedirect(u"/game/%s/slots_change/" % eventid) else: transaction.rollback() form = SlotAppForm() form.loadFromEvent(event,Player.objects.get(user=request.user)) add_error_message(request, message=u"Omlouváme se, došlo k souběhu a během vyplňování formuláře se některé hry zaplnily. Formulář teď zobrazuje aktuální stav.") transaction.commit() notif_messages, error_messages = triage_messages(request) transaction.commit() return render_to_response(u"events/slots_app.html", {u'user' : request.user, u'eventid' : eventid, u'form' : form, u'notif_messages' : notif_messages, u'error_messages' : error_messages} )
def execute_transaction(sql, output=False, database='default'): "A transaction wrapper for executing a list of SQL statements" my_connection = connection using_args = {} if is_multi_db(): if not database: database = DEFAULT_DB_ALIAS my_connection = connections[database] using_args['using'] = database try: # Begin Transaction transaction.enter_transaction_management(**using_args) transaction.managed(True, **using_args) cursor = my_connection.cursor() # Perform the SQL if output: write_sql(sql, database) execute_sql(cursor, sql) transaction.commit(**using_args) transaction.leave_transaction_management(**using_args) except Exception: transaction.rollback(**using_args) raise
def START(message, list_name=None, host=None): """ This is the central logic for doing most of the stuff that makes Postosaurus worth paying money for. Right now we pull a message off of the work queue, archive it and extract any links. """ try: #db message is the message record created in the database. The #id generated is used as a key to extract the actual message #from tokyo tyrant. We do this so that it's easy to maintain # relational data, like what links are related to a message? A # key value store is not good at storing this kind of data. dbmessage = archive.store_message(list_name, message) # store attached files for retrieval for name in files.file_names(message): files.store_file(list_name, message, name, dbmessage) body = message.body() if body: urls = links.extract_urls_from_text(body) for url in urls: links.add_link(list_name, url, dbmessage) transaction.commit() except: #queue up any messages that failed so we can diagnose #and try later. transaction.rollback() q = queue.Queue("run/error") q.push(message)
def handle(self, csvpath, *args, **options): loader = ContributionLoader( source=options.get('source'), description='load from denormalized CSVs', imported_by="loadcontributions.py (%s)" % os.getenv('LOGNAME', 'unknown'), ) try: input_iterator = VerifiedCSVSource(open(os.path.abspath(csvpath)), FIELDNAMES, skiprows=1 + int(options['skip'])) output_func = chain_filters( LoaderEmitter(loader), #Every(self.COMMIT_FREQUENCY, lambda i: transaction.commit()), Every(self.COMMIT_FREQUENCY, progress_tick)) record_processor = self.get_record_processor(loader.import_session) load_data(input_iterator, record_processor, output_func) transaction.commit() except KeyboardInterrupt: traceback.print_exception(*sys.exc_info()) transaction.rollback() raise except: traceback.print_exception(*sys.exc_info()) transaction.rollback() raise finally: sys.stdout.flush() sys.stderr.flush()
def __exit__(self, exc_type, exc_value, traceback): if exc_value is None: # commit operation if self.sid is None: # Outer transaction try: transaction.commit(self.using) except: transaction.rollback(self.using) raise finally: self._leave_transaction_management() else: # Inner savepoint try: transaction.savepoint_commit(self.sid, self.using) except: transaction.savepoint_rollback(self.sid, self.using) raise else: # rollback operation if self.sid is None: # Outer transaction transaction.rollback(self.using) self._leave_transaction_management() else: # Inner savepoint transaction.savepoint_rollback(self.sid, self.using) return False
def _add_deadlines(self): new_deadline = self.CONTENT['deadline'] text = self.CONTENT['text'] deadlines = [] with transaction.commit_manually(): try: groups = self._query_creategroups() if len(groups) == 0: raise BadRequestFieldError('createmode', _('The given option did not match any groups.')) for group in groups: deadline = Deadline(assignment_group=group) deadline.deadline = new_deadline deadline.text = text deadline.full_clean() deadline.save() deadlines.append(deadline) logger.info('User=%s created Deadline id=%s (%s)', self.user, deadline.id, deadline.deadline) except ValidationError as e: transaction.rollback() raise ValidationErrorResponse(e) except Exception as e: transaction.rollback() raise else: transaction.commit() return deadlines
def cancel_record(request, record_id, is_admin=False): record = RPRecord.objects.get(id=record_id) if is_admin: user = record.user else: user = get_current_user(request) if not record: raise Exception("Record with ID %s is not found in database!"%record_id) if record.user != user: raise Exception("It's not your record!!!") if not is_admin: if record.start_datetime < datetime.now(): return inform_message(_(u'Не можна відмінити прийом, який вже пройшов (або під час його)!'), '/my_records') diff = record.start_datetime - datetime.now() if diff.days == 0 and (diff.seconds/60.0 < MIN_CANCEL_TIME): return inform_message(_(u'Відмінити прийом можна не менше ніж за %(min_time)i хвилин!')% {'min_time':MIN_CANCEL_TIME}, '/my_records') res = record.stop_datetime - record.start_datetime cost = ((res.seconds/60.0) / ATOM_TIME) * record.service.atom_money #check current user balance user.balance += cost try: record.delete() user.save() except Exception, err: transaction.rollback() raise err
def create(self, request, workspace_id): user = get_user_authentication(request) if not request.POST.has_key('tab'): return HttpResponseBadRequest(get_xml_error(_("tab JSON expected")), mimetype='application/xml; charset=UTF-8') #TODO we can make this with deserializers (simplejson) received_json = request.POST['tab'] try: t = simplejson.loads(received_json) if not t.has_key('name'): raise Exception(_('Malformed tab JSON: expecting tab name.')) tab_name = t.get('name') workspace = WorkSpace.objects.get(users__id=user.id, pk=workspace_id) ids = createTab(tab_name, user, workspace) return HttpResponse(json_encode(ids), mimetype='application/json; charset=UTF-8') except Exception, e: transaction.rollback() msg = _("tab cannot be created: ") + unicode(e) log(msg, request) return HttpResponseServerError(get_xml_error(msg), mimetype='application/xml; charset=UTF-8')
def details_post(http_request, factory, factory_form, screensize_form, colordepth_form): """ Process a post request to the details page, e.g. to add or remove a screen size or color depth. """ if factory_form.is_valid(): factory.update_fields( hardware=factory_form.cleaned_data['hardware'], operating_system=factory_form.cleaned_data['operating_system']) return results.redirect(factory, 'updated_factory', factory.name) if screensize_form.is_valid(): try: screen_size = ScreenSize.objects.create(factory=factory, width = screensize_form.cleaned_data['width'], height = screensize_form.cleaned_data['height']) return results.redirect(factory, 'added_screen_size', unicode(screen_size), 'screensizes') except IntegrityError, e: transaction.rollback() if 'duplicate' in str(e).lower(): screensize_form.errors['width'] = [ _("This screen size is already enabled.")] else: screensize_form.errors['width'] = [_("Invalid data.")]
def create(self, request): user = get_user_authentication(request) if not request.POST.has_key('workspace'): return HttpResponseBadRequest(get_xml_error(_("workspace JSON expected")), mimetype='application/xml; charset=UTF-8') #TODO we can make this with deserializers (simplejson) received_json = request.POST['workspace'] try: ts = simplejson.loads(received_json) if not ts.has_key('name'): raise Exception(_('Malformed workspace JSON: expecting workspace uri.')) workspace_name = ts.get('name') ids = createWorkSpace (workspace_name, user) workspaces = get_list_or_404(WorkSpace, users__id=user.id, pk=ids['workspace']['id']) data = serializers.serialize('python', workspaces, ensure_ascii=False) concept_data = {} concept_data['user'] = user workspace_data = get_global_workspace_data(data[0], workspaces[0], concept_data, user) return HttpResponse(json_encode(workspace_data), mimetype='application/json; charset=UTF-8') except Exception, e: transaction.rollback() msg = _("workspace cannot be created: ") + unicode(e) log(msg, request) return HttpResponseServerError(get_xml_error(msg), mimetype='application/xml; charset=UTF-8')
def update(self, request, workspace_id): user = get_user_authentication(request) received_json = PUT_parameter(request, 'workspace') if not received_json: return HttpResponseBadRequest(get_xml_error(_("workspace JSON expected")), mimetype='application/xml; charset=UTF-8') try: ts = simplejson.loads(received_json) workspace = WorkSpace.objects.get(users__id=user.id, pk=workspace_id) if ts.has_key('active'): active = ts.get('active') if (active == 'true'): #Only one active workspace setActiveWorkspace(user, workspace) else: workspace.active = False if ts.has_key('name'): workspace.name = ts.get('name') workspace.save() return HttpResponse('ok') except Exception, e: transaction.rollback() msg = _("workspace cannot be updated: ") + unicode(e) log(msg, request) return HttpResponseServerError(get_xml_error(msg), mimetype='application/xml; charset=UTF-8')
def create(request): """Create a batch for document files in project storage.""" taskname = "run.batchitem" preset = get_object_or_404(Preset, pk=request.POST.get("preset", 0)) pids = request.POST.getlist("pid") form = BatchForm(request.POST) if not request.method == "POST" or not form.is_valid() or not pids: return render( request, "batch/new.html", _new_batch_context(request, taskname, form) ) batch = form.instance batch.script = preset.data batch.save() try: dispatch_batch(batch, pids) except StandardError: transaction.rollback() raise transaction.commit() return HttpResponseRedirect("/batch/show/%s/" % batch.pk)
def update(self, request, response): """ Update the logged in user API handler: PUT /user """ profile = request.user.get_profile() profile_form = self.profile_form(request.PUT, request.FILES, instance=profile) user_form = self.user_form(request.PUT, instance=request.user) if profile_form.is_valid(): profile_form.save() else: response.addErrors(self.format_errors(profile_form)) if user_form.is_valid(): user_form.save() else: response.addErrors(self.format_errors(user_form)) if response._errors: transaction.rollback() return response.send() profile.update_callback() return response.send()
def form_valid(self, form): print "form valid" user = None ip_address = None if not self.request.user.is_anonymous(): user = self.request.user if settings.LOG_IPS_USERS: ip_address = self.request.META.get("REMOTE_ADDR", None) elif settings.LOG_IPS_ANONYMOUS: ip_address = self.request.META.get("REMOTE_ADDR", None) if self.request.user.is_anonymous(): return redirect("wiki:get", "") # if not any([ self.request.POST.get('save', False), # self.request.POST.get('save_changes', False), # self.request.POST.get('slug', False), # ]): try: form = forms.CommentForm(self.request.POST) newcomment = models.Comment.objects.create( article=self.article, author=self.request.user, text=form.data["text"] ) newcomment.save() transaction.commit() # TODO: Handle individual exceptions better and give good feedback. except Exception, e: print e transaction.rollback() if self.request.user.is_superuser: messages.error(self.request, _(u"There was an error creating this article: %s") % str(e)) else: messages.error(self.request, _(u"There was an error creating this article."))
def paypal_error(reason): # Explicitly roll back the transaction, since our # error page will look like a success to django... transaction.rollback() return render_to_response('paypal/error.html', { 'reason': reason, }, context_instance=RequestContext(request))
def handle(self, *args, **options): t_total_start = time() self.connect(**options) self.log( "Fixing mojibake in theme descriptions. Think these mangled " "strings are bad? Have a look at " "https://en.wikipedia.org/wiki/File:Letter_to_Russia" "_with_krokozyabry.jpg") try: count = self.count_themes() self.log("Found %s themes. Hope you're not in a hurry" % count) step = 2500 start = options.get('start', 0) self.log("Starting at offset: %s" % start) for offset in range(start, count, step): t_start = time() self.do_fix(offset, step, **options) self.commit_or_not(options.get('commit')) t_average = 1 / ((time() - t_total_start) / (offset - start + step)) print "> %.2fs for %s themes. Averaging %.2f themes/s" % ( time() - t_start, step, t_average) except: self.log('Error, not committing changes.') transaction.rollback() raise finally: self.commit_or_not(options.get('commit')) self.log("Done. Total time: %s seconds" % (time() - t_total_start))
def deleteGeneLink(self, geneLinkKey, isKey=True): metadata = WebServiceObject() try: if (isKey): geneLink = GeneLink.objects.get(pk__exact=geneLinkKey) else: geneLink = geneLinkKey except (ObjectDoesNotExist, ValueError): raise Errors.INVALID_GENE_LINK_KEY except Exception: raise Errors.INTERNAL_ERROR if not geneLink.writePermissions(self.user): raise Errors.AUTHENTICATION metadata.limitFields(self.fields) metadata.put('id', geneLink.pk) metadata.put('tagId', geneLink.tag.pk) metadata.put('feature', LimitDict(self.fields, { 'uniqueName' : geneLink.feature.uniquename, 'name' : geneLink.feature.name, 'organismId' : geneLink.feature.organism.organism_id }) ) try: geneLink.delete() except DatabaseError as e: transaction.rollback() raise Errors.INTEGRITY_ERROR.setCustom(str(e)) return metadata
def create_instance(username, xml_file, media_files, status=u'submitted_via_web', uuid=None, date_created_override=None, request=None): """ I used to check if this file had been submitted already, I've taken this out because it was too slow. Now we're going to create a way for an admin to mark duplicate instances. This should simplify things a bit. Submission cases: If there is a username and no uuid, submitting an old ODK form. If there is a username and a uuid, submitting a new ODK form. """ try: instance = None submitted_by = request.user \ if request and request.user.is_authenticated() else None if username: username = username.lower() xml = xml_file.read() xform = get_xform_from_submission(xml, username, uuid) check_submission_permissions(request, xform) existing_instance_count = Instance.objects.filter( xml=xml, xform__user=xform.user).count() if existing_instance_count > 0: existing_instance = Instance.objects.filter( xml=xml, xform__user=xform.user)[0] if not existing_instance.xform or\ existing_instance.xform.has_start_time: # Ignore submission as a duplicate IFF # * a submission's XForm collects start time # * the submitted XML is an exact match with one that # has already been submitted for that user. raise DuplicateInstance() # get new and depracated uuid's new_uuid = get_uuid_from_xml(xml) duplicate_instances = Instance.objects.filter(uuid=new_uuid) if duplicate_instances: for f in media_files: Attachment.objects.get_or_create( instance=duplicate_instances[0], media_file=f, mimetype=f.content_type) # ensure we have saved the extra attachments transaction.commit() raise DuplicateInstance() instance = save_submission(xform, xml, media_files, new_uuid, submitted_by, status, date_created_override) # commit all changes transaction.commit() return instance except Exception: transaction.rollback() raise
def exception_handler(func): @transaction.commit_manually def _exception_handler(self, request, *args, **kwargs): try: resp = func(self, request, *args, **kwargs) transaction.commit() except CodeErrorException, ex: if ex.__dict__.has_key('error_dict'): resp = error_response(ex.error_code, ex.error_message, ex.error_class(), ex.error_dict ) else: resp = error_response(ex.error_code, ex.error_message, ex.error_class()) logger.exception(ex) transaction.rollback() except Exception, ex: logger.exception(ex) transaction.rollback() inner_ex = InnerException(ex) resp = error_response(inner_ex.error_code, inner_ex.error_message, inner_ex.error_class())
def deleteTagGroup(self, tagGroupKey, isKey=True): metadata = WebServiceObject() try: if isKey: tagGroup = TagGroup.objects.get(pk__exact=tagGroupKey) else: tagGroup = tagGroupKey except (ObjectDoesNotExist, ValueError): raise Errors.INVALID_TAG_GROUP_KEY except Exception: raise Errors.INTERNAL_ERROR if not tagGroup.writePermissions(self.user): raise Errors.AUTHENTICATION metadata.limitFields(self.fields) # add new tag to response for success metadata.put("id", tagGroup.pk) metadata.put("name", tagGroup.name) metadata.put("dateCreated", tagGroup.dateCreated.strftime("%Y-%m-%d %H:%M:%S")) metadata.put("lastModified", tagGroup.lastModified.strftime("%Y-%m-%d %H:%M:%S")) metadata.put("imageId", tagGroup.picture.pk) try: tagGroup.delete() except DatabaseError as e: transaction.rollback() raise Errors.INTEGRITY_ERROR.setCustom(str(e)) return metadata
def create_nodeset(name, study, assay, summary='', solr_query='', solr_query_components=''): '''Create a new NodeSet. :param name: name of the new NodeSet. :type name: str. :param study: Study model instance. :type study: Study. :param study: Assay model instance. :type study: Assay. :param summary: description of the new NodeSet. :type summary: str. :param solr_query: Solr query representing a list of Node instances. :type solr_query: str. :param solr_query_components: JSON stringyfied representation of components of Solr query representing a list of Node instances. :type solr_query_components: str. :returns: NodeSet -- new instance. :raises: IntegrityError, ValueError ''' try: nodeset = NodeSet.objects.create(name=name, study=study, assay=assay, summary=summary, solr_query=solr_query, solr_query_components=solr_query_components) except (IntegrityError, ValueError) as e: transaction.rollback() logger.error("Failed to create NodeSet: {}".format(e.message)) raise transaction.commit() logger.info("NodeSet created with UUID '{}'".format(nodeset.uuid)) return nodeset
def write(self, impressions): insert_sql = "INSERT INTO bannerimpressions (timestamp, banner, campaign, project_id, language_id, country_id, count) VALUES (%s) ON DUPLICATE KEY update count=count+%d" cursor = connections['default'].cursor() if not len(impressions): return try: for k,c in impressions.iteritems(): try: cursor.execute(insert_sql % ( "%s, %d" % (k, c), c )) except (MySQLdb.Warning, _mysql_exceptions.Warning) as e: pass # We don't care about the message transaction.commit('default') except Exception as e: import sys transaction.rollback("default") self.logger.exception("UNHANDLED EXCEPTION: %s" % str(e)) self.logger.exception(sys.exc_info()[0]) if self.debug: if len(impressions) == 1: self.logger.info(impressions) for r in self.debug_info: self.logger.info("\t%s" % r) finally: reset_queries() del impressions del cursor
def handle(self, *args, **options): datadir = args[0] # Need to do this 50 times, onece for the shapefile of each state for i in range(1,57): # files are named with zero padded FIPS codes if i < 10: padded_i = "0"+str(i) else: padded_i = str(i) shpfile = os.path.abspath(os.path.join(os.path.dirname(__file__), datadir+'/state_tract_shapefiles/tl_2010_'+padded_i+'_tract10/tl_2010_'+padded_i+'_tract10.shp')) print "Attempting import of shapefile "+shpfile try: lm = LayerMapping(CensusTract, shpfile, censustracts_mapping) except OGRException: print "Could not open datasource ", print shpfile else: try: lm.save(strict=True, verbose=False, progress=True) except IntegrityError: print "Already imported ", print shpfile from django.db import transaction transaction.rollback() else: print "Imported shapefile "+shpfile print
def commit_or_not(self, gogo): if gogo == 'yes': self.log('Committing changes.') transaction.commit() else: self.log('Not committing changes, this is a dry run.') transaction.rollback()
def rollback(self): transaction.rollback() transaction.set_autocommit(True)
def addCron(request): """ add cron tab :param request: :return: """ data_result = {'status': 0, 'msg': ''} try: userId = request.session['userID'] account = Account.objects.get(id=userId) if request.POST: try: transaction.set_autocommit(False) data = request.POST.copy() form = CronJobForm(data) if form.is_valid(): data['backup_schedu'] = ','.join( data.getlist('backup_schedu')) if data.getlist('backup_day'): data['backup_day'] = ','.join( data.getlist('backup_day')) else: data['backup_day'] = 0 try: week_cron = data['backup_week'] except KeyError: week_cron = 0 # save record cronjob crontab = CronJob( backup_schedu=data['backup_schedu'], backup_day=data['backup_day'], backup_week=week_cron, backup_type=data['backup_type'], ) crontab.save() if int(data['backup_type']) == 1: # check connect ssh cmd = 'sshpass -p {} ssh -o StrictHostKeyChecking=no -p {} -q {}@{} exit;echo $?'.format( data['password'], data['port'], data['user'], data['host']) res = subprocess.run(cmd, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) if str(res.stdout).replace('\n', '') != '0': raise ValueError('Remote connection failed') crontab.host = data['host'] crontab.port = data['port'] crontab.user = data['user'] crontab.password = data['password'] crontab.path = data['path'] crontab.save() pass elif int(data['backup_type']) == 2: # backup google drive if not os.path.isfile( '/root/.config/rclone/rclone.conf'): raise ValueError( 'You can config access Google Drive!') pass else: # backup localhost pass else: data_result['errors'] = form.errors raise ValueError('Params is validate! Please check value') data_result['status'] = 1 data_result['msg'] = 'Add Crontab Successfully!' data_result['data'] = {'id': crontab.pk} transaction.commit() except BaseException as e: transaction.rollback() data_result['msg'] = str(e) except KeyError: return HttpResponseRedirect('/login') return JsonResponse(data_result)
def wrap_sentry(request, **kwargs): if transaction.is_dirty(): transaction.rollback() return sentry_exception_handler(request, **kwargs)
def handle(self, *fixture_labels, **options): from django.db.models import get_apps from django.core import serializers from django.db import connection, transaction from django.conf import settings self.style = no_style() verbosity = int(options.get('verbosity', 1)) show_traceback = options.get('traceback', False) # commit is a stealth option - it isn't really useful as # a command line option, but it can be useful when invoking # loaddata from within another script. # If commit=True, loaddata will use its own transaction; # if commit=False, the data load SQL will become part of # the transaction in place when loaddata was invoked. commit = options.get('commit', True) # Keep a count of the installed objects and fixtures fixture_count = 0 object_count = 0 models = set() humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path' # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database (if # it isn't already initialized). cursor = connection.cursor() # Start transaction management. All fixtures are installed in a # single transaction to ensure that all references are resolved. if commit: transaction.commit_unless_managed() transaction.enter_transaction_management() transaction.managed(True) self.disable_forward_ref_checks() class SingleZipReader(zipfile.ZipFile): def __init__(self, *args, **kwargs): zipfile.ZipFile.__init__(self, *args, **kwargs) if settings.DEBUG: assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file." def read(self): return zipfile.ZipFile.read(self, self.namelist()[0]) compression_types = { None: file, 'gz': gzip.GzipFile, 'zip': SingleZipReader } if has_bz2: compression_types['bz2'] = bz2.BZ2File app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()] for fixture_label in fixture_labels: parts = fixture_label.split('.') if len(parts) > 1 and parts[-1] in compression_types: compression_formats = [parts[-1]] parts = parts[:-1] else: compression_formats = compression_types.keys() if len(parts) == 1: fixture_name = parts[0] formats = serializers.get_public_serializer_formats() else: fixture_name, format = '.'.join(parts[:-1]), parts[-1] if format in serializers.get_public_serializer_formats(): formats = [format] else: formats = [] if formats: if verbosity > 1: print "Loading '%s' fixtures..." % fixture_name else: self.enable_forward_ref_checks(cursor) sys.stderr.write( self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." % (fixture_name, format))) transaction.rollback() transaction.leave_transaction_management() return if os.path.isabs(fixture_name): fixture_dirs = [fixture_name] else: fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + [''] for fixture_dir in fixture_dirs: if verbosity > 1: print "Checking %s for fixtures..." % humanize(fixture_dir) label_found = False for format in formats: for compression_format in compression_formats: if compression_format: file_name = '.'.join([fixture_name, format, compression_format]) else: file_name = '.'.join([fixture_name, format]) if verbosity > 1: print "Trying %s for %s fixture '%s'..." % \ (humanize(fixture_dir), file_name, fixture_name) full_path = os.path.join(fixture_dir, file_name) open_method = compression_types[compression_format] try: fixture = open_method(full_path, 'r') if label_found: fixture.close() self.enable_forward_ref_checks(cursor) print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % (fixture_name, humanize(fixture_dir))) transaction.rollback() transaction.leave_transaction_management() return else: fixture_count += 1 objects_in_fixture = 0 if verbosity > 0: print "Installing %s fixture '%s' from %s." % \ (format, fixture_name, humanize(fixture_dir)) try: objects = serializers.deserialize(format, fixture) for obj in objects: objects_in_fixture += 1 self.handle_ref_checks(cursor, obj) models.add(obj.object.__class__) obj.save() object_count += objects_in_fixture label_found = True except (SystemExit, KeyboardInterrupt): self.enable_forward_ref_checks(cursor) raise except Exception: import traceback fixture.close() self.enable_forward_ref_checks(cursor) transaction.rollback() transaction.leave_transaction_management() if show_traceback: traceback.print_exc() else: sys.stderr.write( self.style.ERROR("Problem installing fixture '%s': %s\n" % (full_path, ''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))))) return fixture.close() # If the fixture we loaded contains 0 objects, assume that an # error was encountered during fixture loading. if objects_in_fixture == 0: self.enable_forward_ref_checks(cursor) sys.stderr.write( self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" % (fixture_name))) transaction.rollback() transaction.leave_transaction_management() return except Exception, e: if verbosity > 1: print "No %s fixture '%s' in %s." % \ (format, fixture_name, humanize(fixture_dir))
def tearDown(self): super(ConcurrencyTest0, self).tearDown() transaction.rollback() transaction.leave_transaction_management()
@transaction.commit_manually @reviewer_required(only='app') @addon_view def app_review(request, addon): version = addon.latest_version resp = None try: resp = _review(request, addon, version) except SigningError, exc: transaction.rollback() messages.error(request, 'Signing Error: %s' % exc) transaction.commit() return redirect(reverse('reviewers.apps.review', args=[addon.app_slug])) except Exception: transaction.rollback() raise else: transaction.commit() # We (hopefully) have been avoiding sending send post_save and # version_changed signals in the review process till now (_review() # uses ReviewHelper which should have done all of its update() calls # with _signal=False). # # Now is a good time to send them: the transaction we were in has been # committed, so we know everything is ok. This is important: we need # them to index the app or call update_version() if that wasn't done # before already. if request.method == 'POST': try: post_save.send(sender=Webapp, instance=addon, created=False)
def create_instance(username, xml_file, media_files, status=u'submitted_via_web', uuid=None, date_created_override=None, request=None): """ I used to check if this file had been submitted already, I've taken this out because it was too slow. Now we're going to create a way for an admin to mark duplicate instances. This should simplify things a bit. Submission cases: If there is a username and no uuid, submitting an old ODK form. If there is a username and a uuid, submitting a new ODK form. """ try: instance = None user = get_object_or_404(User, username=username.lower()) submitted_by = request.user \ if request and request.user.is_authenticated() else user if username: username = username.lower() xml = xml_file.read() #print 'xml of form'+str(xml) print "#####################################################################################################################################" #valid = check_custom_form_validation(xml,request,username) valid = True if not valid: #print 'Form is not Valid..... ' transaction.rollback() raise ValueError('need to register your tubewell first...') xform = get_xform_from_submission(xml, username, uuid) check_submission_permissions(request, xform) existing_instance_count = Instance.objects.filter( xml=xml, xform__user=xform.user).count() if existing_instance_count > 0: existing_instance = Instance.objects.filter( xml=xml, xform__user=xform.user)[0] if not existing_instance.xform or\ existing_instance.xform.has_start_time: # Ignore submission as a duplicate IFF # * a submission's XForm collects start time # * the submitted XML is an exact match with one that # has already been submitted for that user. raise DuplicateInstance() # get new and depracated uuid's new_uuid = get_uuid_from_xml(xml) duplicate_instances = Instance.objects.filter(uuid=new_uuid) if duplicate_instances: for f in media_files: Attachment.objects.get_or_create( instance=duplicate_instances[0], media_file=f, mimetype=f.content_type) # ensure we have saved the extra attachments transaction.commit() raise DuplicateInstance() instance = save_submission(xform, xml, media_files, new_uuid, submitted_by, status, date_created_override) # commit all changes transaction.commit() return instance except Exception: transaction.rollback() raise
def old_data_importer(self, request): """ Import Form. """ if request.method == 'POST': form = ShopDataImportForm(request.POST, request.FILES) else: form = ShopDataImportForm() form.configure(request) export_form = ShopInventoryExportForm() export_form.configure(request) if request.method == 'POST' and form.is_valid(): d = form.cleaned_data # load data importer import_classname = settings.CUBANE_SHOP_IMPORT.get( 'importer', 'cubane.ishop.apps.merchant.dataimport.importer.ShopDataImporter' ) import_class = get_class_from_string(import_classname) # create importer and start importing... importer = import_class(import_images=d.get('import_images')) importer.import_from_stream(request, request.FILES['csvfile'], encoding=d.get('encoding')) # present information what happend during import if importer.has_errors: transaction.rollback() errors = importer.get_formatted_errors() messages.add_message( request, messages.ERROR, ('Import failed due to %s. No data ' + 'has been imported. Please correct all issues ' + 'and try again.') % pluralize(len(errors), 'error', tag='em')) for message in errors: messages.add_message(request, messages.ERROR, message) # redirect to itself if we have errors return HttpResponseRedirect( get_absolute_url('cubane.ishop.dataimport.index')) else: # success message, render image processing page messages.add_message( request, messages.SUCCESS, pluralize(importer.num_records_processed, 'record', 'processed successfully', tag='em')) # redirect to itself if we have errors return HttpResponseRedirect( get_absolute_url('cubane.ishop.dataimport.index')) return {'form': form, 'export_form': export_form}
def update_cheque_details(request): data = {} sid = transaction.savepoint() try: print '\nRequest IN | cheque_bounce.py | update_cheque_details | User = '******'pay_cheque_id')) bounce_cheque_obj.is_deleted = True bounce_cheque_obj.bounce_charge = request.POST.get('modal_extra_amt') bounce_cheque_obj.bounce_cheque_remark = request.POST.get( 'modal_remark') bounce_cheque_obj.save() hall_booking_obj = HallBooking.objects.get( id=bounce_cheque_obj.hall_booking.id) print '.............hall_booking_obj.......', hall_booking_obj.id other_cheque_objs = HallPaymentDetail.objects.filter( hall_booking=hall_booking_obj, is_deleted=False, payment_status=10).exclude(id=bounce_cheque_obj.id).last() pay_half_gst_amount = round( (Decimal(bounce_cheque_obj.bounce_charge) * Decimal(0.09)), 2) pay_total_tax = pay_half_gst_amount * 2 print '>>>>>>>>>>>>>>>>>>>>>>>....', other_cheque_objs if other_cheque_objs: other_cheque_objs.payable_amount = Decimal( other_cheque_objs.payable_amount ) + Decimal(bounce_cheque_obj.paid_amount) + Decimal( bounce_cheque_obj.bounce_charge) + Decimal(pay_total_tax) other_cheque_objs.save() else: new_payment_obj = HallPaymentDetail( hall_booking=hall_booking_obj, payable_amount=Decimal(bounce_cheque_obj.paid_amount) + Decimal(bounce_cheque_obj.bounce_charge) + Decimal(pay_total_tax), created_by=str(request.user)) new_payment_obj.save() hall_booking_obj.total_rent = Decimal( hall_booking_obj.total_rent) + Decimal( bounce_cheque_obj.bounce_charge) hall_booking_obj.save() half_gst_amount = round( (Decimal(hall_booking_obj.total_rent) * Decimal(0.09)), 2) total_tax = half_gst_amount * 2 hall_booking_obj.gst_amount = total_tax hall_booking_obj.total_payable = round( Decimal(hall_booking_obj.total_rent) + Decimal(hall_booking_obj.deposit) + Decimal(hall_booking_obj.gst_amount), 0) hall_booking_obj.save() total_paid_dict = HallPaymentDetail.objects.filter( hall_booking=hall_booking_obj, is_deleted=False).aggregate(total_paid=Sum('paid_amount')) hall_booking_obj.paid_amount = Decimal(total_paid_dict['total_paid']) hall_booking_obj.save() hall_booking_obj.payment_status = 8 hall_booking_obj.save() if hall_booking_obj.paid_amount == hall_booking_obj.total_payable: hall_booking_obj.payment_status = 1 hall_booking_obj.save() data['success'] = 'true' transaction.savepoint_commit(sid) print '\nResponse OUT | cheque_bounce.py | update_cheque_details | User = '******'success'] = 'false' print '\nException IN | cheque_bounce.py | update_cheque_details | User = ', str( traceback.print_exc()) transaction.rollback(sid)
def inventory_import(self, request): """ Shop Inventory Import (CSV). """ # default encoding initial = {'encoding': get_cms_settings().default_encoding} if request.method == 'POST': form = ShopInventoryImportForm(request.POST, request.FILES) else: form = ShopInventoryImportForm(initial=initial) form.configure(request) export_form = ShopInventoryExportForm(initial=initial) export_form.configure(request) if request.method == 'POST' and form.is_valid(): d = form.cleaned_data # create importer and import date importer = ShopInventoryImporter() importer.import_from_stream(request, request.FILES['csvfile'], encoding=d.get('encoding')) # present information what happend during import if importer.has_errors: transaction.rollback() errors = importer.get_formatted_errors() messages.add_message( request, messages.ERROR, ('Import failed due to %s. No data ' + 'has been imported. Please correct all issues ' + 'and try again.') % pluralize(len(errors), 'error', tag='em')) for message in errors: messages.add_message(request, messages.ERROR, message) # redirect to itself if we have errors return HttpResponseRedirect( get_absolute_url( 'cubane.ishop.inventory.inventory_import')) else: # success message, render image processing page messages.add_message( request, messages.SUCCESS, pluralize(importer.num_records_processed, 'record', 'processed successfully', tag='em')) # redirect to itself if we have errors return HttpResponseRedirect( get_absolute_url( 'cubane.ishop.inventory.inventory_import')) return { 'form': form, 'export_form': export_form, 'export_form_action': reverse('cubane.ishop.inventory.inventory_export') }
def render_to_pdf(template_url,data,studio): ##generate pdf with data #import pdb;pdb.set_trace(); try: global till_time,today,mail_exception response = 0 template = get_template(template_url) data['todayslist']['date'] = today data['todayslist']['time'] = till_time context = Context(data) html = template.render(context) filename = data['todayslist']['studio_name'] + str(datetime.today().date())+"__bookings" result = open(filename+'.pdf', 'wb') #result = StringIO.StringIO() logger_booking.info("File name "+str(filename)) logger_booking.info("File name "+str(data)) try: pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result) except: logger_error.error(traceback.format_exc()) pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result) result.close() if not pdf.err: ###get and save the pdf file_ = open(filename+'.pdf', 'r') pdf = File(file_) rep = DailyBookingConfirmation(studio_id = studio, booking_pdf = pdf, \ service_updated = "studio report sender") rep.save() studio_dt = StudioProfile.objects.values('studio').get(id = studio) studio_email = Studio.objects.values('email').get(id = studio_dt['studio']) try: #send email message = "Please find the booking data" subject = (responses.MAIL_SUBJECTS['DAILY_BOOKING_MAIL'])%(today) generic_utils.sendEmail(studio_email['email'],subject,message,filename+'.pdf') generic_utils.sendEmail('*****@*****.**',subject,message,filename+'.pdf') rep = DailyBookingConfirmation.objects.filter(studio_id = studio, report_date = \ today).update(mail_sent = 1, updated_date_time = datetime.now()) response = 1 logger_booking.info('studio details %s '%(data)) except Exception,e: response = 0 err = {} err['studio'] = studio err['Exception'] = e mail_exception.append(err) logger_error.error(traceback.format_exc()) logger_error.error('studio details %s '%(data)) ##save pdf to table ##location should be inside studio file_.close(); os.remove(filename+'.pdf') except Exception,pdfrenderr: transaction.rollback() err = {} err['studio'] = studio err['Exception'] = pdfrenderr mail_exception.append(err) logger_error.error(traceback.format_exc()) return response
def tearDown(self): transaction.rollback()
def create(self, request, templateName): if 'template_data' not in request.POST: return HttpResponseBadRequest( get_xml_error(_("template_data JSON expected")), mimetype='application/xml; charset=UTF-8') #TODO we can make this with deserializers (simplejson) received_json = request.POST['template_data'] try: #generate an specific template dinamically templateInstance = TemplateInstance() templateInstance.template = Template.objects.get(name=templateName) templateInstance.save() context = simplejson.loads(received_json) # Parse fixed params fixed_params = '' if 'parse_parameters' in context and 'fixed_params' in context: fixed_params = context['fixed_params'].strip() context['fixed_params'] = {} if len(fixed_params) > 0: for param in fixed_params.split(','): param = param.strip() if param != '': context['fixed_params'][param.split( '=')[0].strip()] = param.split('=')[1].strip() # Parse default params default_params_tmp = '' default_params = {} if 'parse_parameters' in context and 'default_params' in context: default_params_tmp = context['default_params'].strip() del context['default_params'] if len(default_params_tmp) > 0: for param in default_params_tmp.split(','): param = param.strip() if param != '': default_params[param.split( '=')[0].strip()] = param.split('=')[1].strip() # Parse URL params, create a params dictionary without the fixed params context['params'] = {} parsedUrl = context['URL'].partition('?') if 'parse_parameters' in context and int( context['parse_parameters']) > 0: #include the parameters of the url context['URL'] = parsedUrl[0] # base without ? queryString = parsedUrl[2] if len(queryString) > 0: for param in queryString.split('&'): if param != '': # Check if params is not on the fixed_params dictionary param_att_value = param.split('=') if not ('fixed_params' in context) or not ( param_att_value[0] in context['fixed_params']): if param_att_value[0] in default_params: context['params'][ param_att_value[0]] = default_params[ param_att_value[0]] else: context['params'][param_att_value[0]] = '' if ('fixed_params' in context and len(context['fixed_params']) > 0) or ( 'params' in context and len(context['params']) > 0): # if we have parameters, fixed or variable, add '?' to the base URL context['URL'] = context['URL'] + parsedUrl[1] # Add fixed params to the base URL and remove from context if ('fixed_params' in context and len(context['fixed_params']) > 0): fixed_params_part = '' for key, value in context['fixed_params'].items(): if fixed_params_part != '': fixed_params_part = fixed_params_part + '&' + key + '=' + value else: fixed_params_part = key + '=' + value context['URL'] = context['URL'] + fixed_params_part del context['fixed_params'] events = [] if 'events' in context: events = context['events'].split(',') context['events'] = [] for event in events: if event.strip() != '': context['events'].append(event.strip()) #include the XHTML url context['XHTML'] = "http://" + request.get_host( ) + "/gadgetGenerator/xhtml/" + templateName + '/' + str( templateInstance.id) templateInstance.context = json_encode(context) templateInstance.save() except Exception, e: transaction.rollback() msg = _("template cannot be created: ") + unicode(e) raise TracedServerError(e, {'template_data': received_json}, request, msg)
def merge(modeladmin, request, queryset): """ Merge two model instances. Move all foreign keys. """ def raw_widget(field, **kwargs): """ force all fields as not required""" kwargs['widget'] = TextInput({'class': 'raw-value', 'readonly': 'readonly'}) kwargs['widget'] = TextInput({'class': 'raw-value', 'size': '30'}) return field.formfield(**kwargs) # Allows to specified a custom Form in the ModelAdmin # MForm = getattr(modeladmin, 'merge_form', MergeForm) merge_form = getattr(modeladmin, 'merge_form', MergeForm) MForm = modelform_factory(modeladmin.model, form=merge_form, formfield_callback=raw_widget) OForm = modelform_factory(modeladmin.model, formfield_callback=raw_widget) tpl = 'adminactions/merge.html' ctx = { '_selected_action': request.POST.getlist(helpers.ACTION_CHECKBOX_NAME), 'select_across': request.POST.get('select_across') == '1', 'action': request.POST.get('action'), 'fields': [f for f in queryset.model._meta.fields if not f.primary_key and f.editable], 'app_label': queryset.model._meta.app_label, 'result': '', 'opts': queryset.model._meta} if 'preview' in request.POST: master = queryset.get(pk=request.POST.get('master_pk')) original = clone_instance(master) other = queryset.get(pk=request.POST.get('other_pk')) formset = formset_factory(OForm)(initial=[model_to_dict(master), model_to_dict(other)]) with transaction.commit_manually(): form = MForm(request.POST, instance=master) other.delete() form_is_valid = form.is_valid() transaction.rollback() if form_is_valid: ctx.update({'original': original}) tpl = 'adminactions/merge_preview.html' elif 'apply' in request.POST: master = queryset.get(pk=request.POST.get('master_pk')) other = queryset.get(pk=request.POST.get('other_pk')) formset = formset_factory(OForm)(initial=[model_to_dict(master), model_to_dict(other)]) with transaction.commit_manually(): form = MForm(request.POST, instance=master) other.delete() if form.is_valid(): form.save() transaction.commit() return HttpResponseRedirect(request.path) else: messages.error(request, form.errors) transaction.rollback() else: try: master, other = queryset.all() except ValueError: messages.error(request, _('Please select exactly 2 records')) return initial = {'_selected_action': request.POST.getlist(helpers.ACTION_CHECKBOX_NAME), 'select_across': 0, 'generic': MergeForm.GEN_IGNORE, 'dependencies': MergeForm.DEP_MOVE, 'action': 'merge', 'master_pk': master.pk, 'other_pk': other.pk} formset = formset_factory(OForm)(initial=[model_to_dict(master), model_to_dict(other)]) form = MForm(initial=initial, instance=master) adminForm = helpers.AdminForm(form, modeladmin.get_fieldsets(request), {}, [], model_admin=modeladmin) media = modeladmin.media + adminForm.media ctx.update({'adminform': adminForm, 'formset': formset, 'media': mark_safe(media), 'master': master, 'other': other}) return render_to_response(tpl, RequestContext(request, ctx))
def rollback(self): if self.is_transaction_owner: transaction.rollback() self.close_cursor()
def _process(self): """ Process the incoming doc """ # Is this document registered in document schema? # Is this document registered in doc_class_rel? # Does doc_class_rel contain 'class'? # FIXME: we should stop using this globals() approach. Not good. (Ben) doc_schema = self.get_document_schema() if doc_schema and \ self.doc_class_rel.has_key(doc_schema.type) and \ globals().has_key(self.doc_class_rel[doc_schema.type]['class']): # Validate the document XML if settings.VALIDATE_XML: schema = self.get_schema_file() if schema: self.validate_xml(self.content, schema) # Get the object that corresponds to the incoming xml node name dp_data_obj = globals()[self.doc_class_rel[doc_schema.type] ['class']]() # Retrieve a stylesheet, if there is one stylesheet = self.get_stylesheet() if stylesheet: self.transformed_doc = self.apply_stylesheet( self.content, stylesheet) self.transformed_dom = self.get_dom(self.transformed_doc) # Test for a process method # If dp_data_obj has a process method then use it # else use document_processing standard if hasattr(dp_data_obj, DP_DOBJ_PROCESS): doc_data = dp_data_obj.process(self.root_node_name, self.transformed_dom) else: doc_data = self.parse_standard_facts_doc( self.root_node_name, self.transformed_dom) # If dp_data_obj has the post_data method # then post it and set and return the fact obj if hasattr(dp_data_obj, DP_DOBJ_POST_DATA) and doc_data: try: f_objs = [] for data in doc_data: f_objs.append(dp_data_obj.post_data(**data)) if f_objs: return {self.f_objs_str: f_objs} except ValueError, e: # FIXME: this rollback here doesn't seem to do the right thing # because we're not in a transaction. If we're going to roll # back a transaction, it should probably happen at a higher level, # probably in the view. try: transaction.rollback() except: # no transaction management is fine pass # we raise the exception, if processing fails we need to stop raise e
def jsonTeacherCrud(request): # Display/Filter is only on GET if request.is_ajax() and request.method == 'GET': id = request.GET.get('id', None) action = request.GET.get('action', None) if (action=="filter"): dictData = Teacher.objects.filter(id=id).values() else: dictData = Teacher.objects.all().values() #print dictData jsonData = json.dumps(list(dictData)) return HttpResponse(jsonData, content_type='application/json') # CRUD is only on POST if request.is_ajax() and request.method == 'POST': action = request.POST.get('action', None) #ADD if (action=="add"): codeAfm = request.POST.get('codeAfm', None) codeGrad = request.POST.get('codeGrad', None) codeSpec = request.POST.get('codeSpec', None) surname = request.POST.get('surname', None) name = request.POST.get('name', None) record = Teacher( codeAfm = codeAfm, codeGrad = codeGrad , codeSpec = codeSpec, surname = surname, name=name, ).save() # return Lesson record dictData = Teacher.objects.filter(id=record.id).values() jsonData = json.dumps(dictData) return HttpResponse(jsonData, content_type='application/json') #DELETE if (action=="delete"): id = request.POST.get('id', None) record = Teacher.objects.filter(id=id) try: record.delete() result = "Teacher (%s) deleted successfully." %(record) except IntegrityError: transaction.rollback() result = "Error Teacher (%s) could not be deleted." %(record) return HttpResponse(result, content_type='application/json') #UPDATE elif (action=="update"): id = request.POST.get('id', None) codeAfm = request.POST.get('codeAfm', None) codeGrad = request.POST.get('codeGrad', None) codeSpec = request.POST.get('codeSpec', None) surname = request.POST.get('surname', None) name = request.POST.get('name', None) record = Teacher.objects.filter(id=id) record.update( codeAfm = codeAfm, codeGrad = codeGrad , codeSpec = codeSpec,surname = surname, name=name , ) dictData = record.values() #print dictData jsonData = json.dumps(list(dictData)) return HttpResponse(jsonData, content_type='application/json') # DEFAULT on View else: dictData = Teacher.objects.all().values() jsonData = json.dumps(list(dictData)) #print jsonData return HttpResponse(jsonData, content_type='application/json')
def upload(request, project): if not project.is_owned_by(request.user): raise Http404 if request.method == 'POST': sample_image = request.POST.get('sample', 0) delete_id = request.POST.get('delete', 0) if delete_id > 0: try: page_to_delete = Page.objects.get(pk=delete_id) page_to_delete.delete() except Page.DoesNotExist: pass elif sample_image > 0 and Page.objects.filter( pk=sample_image).count() == 1: # Return the workflow edit page return redirect('new_workflow', sample_image) else: page_id = request.POST.get('page-id', '') page_sequence_new = request.POST.get('page-sequence-new', '') if page_id != '' and page_sequence_new != '': with transaction.commit_manually(): transaction.commit() try: pages = Page.objects.filter(project=project).all() page = pages.get(pk=page_id) page_sequence_new = int(page_sequence_new) page_sequence_old = page.sequence # null out the sequence to avoid uniqueness constraints when shifting page.sequence = None page.save() # if the page was dragged to the right (i.e increase sequence) if page_sequence_old < page_sequence_new: for p in pages[ page_sequence_old:page_sequence_new]: p.sequence = p.sequence - 1 p.save() else: # if the page was dragged to the left (i.e decrease sequence) # the reason for having a temp_list is because of the ordering # of pages by sequence in the Page model - we need to process # the pages in the range below in reverse order to prevent # uniqueness constraints, but when you reverse the pages QuerySet # the target page that is Null'd out will appear in the list as the first # record to process, which throws a database error. temp_list = [] for p in pages[ page_sequence_new:page_sequence_old]: temp_list.append(p) temp_list.reverse() for p in temp_list: p.sequence = p.sequence + 1 p.save() # change the targer page to its new target sequence value page.sequence = page_sequence_new page.save() except: transaction.rollback() else: transaction.commit() # If there's an image specified, create a new workflow for that page files = request.FILES.getlist('files[]') sequence = Page.objects.filter(project=project).count() for file in files: # The "sequence" is the number of pages in this project already + 1 sequence += 1 new_page = Page.objects.create(project=project, filename=file.name, sequence=sequence) new_page.handle_image_upload(file) # Figure out where to go next # Stay on the same page data = { 'project': project, 'form': True, 'file_upload': True, 'pages': project.page_set.all(), 'num_processing': project.page_set.filter(is_ready=False).count(), } return ('Manage images', data)
def jsonFolderCrud(request): dictData = [] #action = request.POST.get('action', '') if ( request.method == 'POST' ) else request.GET.get( 'action', '' ) #print action # GET * SELECT if request.is_ajax() and request.method == 'GET': action = request.GET.get('action', None) print 'get', action if (action=="filter"): LessonID = request.GET.get('LessonID', None) #dictData = Folder.objects.get(LessonID=LessonID).graders.all().values() #OK dictData = Folder.objects.filter(LessonID=LessonID).values() jsonData = json.dumps(list(dictData)) #print jsonData return HttpResponse(jsonData, content_type='application/json') # POST INS.UPD.DEL if request.is_ajax() and request.method == 'POST': action = request.POST.get('action', None) #print 'POST', action #INSERT COMMAND (need related LessonID ) if (action=="insert"): LessonID = request.POST.get('LessonID', None) no = request.POST.get('no', None) books = request.POST.get('books', None)#.title() type = request.POST.get('type', None)#.title() #status = request.POST.get('status', None) print 'Data:', no, books, type no = Lesson.objects.get(id=LessonID).getNextFolderNo(type = type) print 'Data(new):', no, books, type Folder(LessonID_id = LessonID, no = no, books = books, type = type).save() #t0.save() # Succeeds, but may be undone by transaction rollback #c.save() # Succeeds, but a.save() may have been undone dictData = "Success" #print dictData jsonData = json.dumps(dictData) return HttpResponse(jsonData, content_type='application/json') # UPDATE if (action=="update"): id = request.POST.get('id', None) no = request.POST.get('no', None) books = request.POST.get('books', None) type = request.POST.get('type', None) #status = request.POST.get('status', None) record = FolderLesson.objects.filter(id=id) record.update( no = no, books = books, type = type,) #Folder.objects.filter(id=id).update(isCoordinator = helperStr2Bool(isCoordinator), isgraderC = helperStr2Bool(isgraderC), status = status, ) # return Lesson record dictData = record.values() jsonData = json.dumps(list(dictData)) #print jsonData return HttpResponse(jsonData, content_type='application/json') #DELETE COMMAND if (action=="delete"): id = request.POST.get('id', None) record = Folder.objects.filter(id=id) try: record.delete() result = "Folder (%s) deleted successfully." %(record) except IntegrityError: transaction.rollback() result = "Error Folder (%s) could not be deleted." %(record) return HttpResponse(result, content_type='application/json') #"Default SELECT(*) " dictData = Folder.objects.all().values() #print dictData jsonData = json.dumps(list(dictData)) return HttpResponse(jsonData, content_type='application/json')
def syncdata(self, fixture_labels, options): verbosity = options['verbosity'] show_traceback = options['traceback'] # Keep a count of the installed objects and fixtures fixture_count = 0 object_count = 0 objects_per_fixture = [] models = set() humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path' # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database (if # it isn't already initialized). cursor = connections[self.using].cursor() app_modules = [app.module for app in apps.get_app_configs()] app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in app_modules] for fixture_label in fixture_labels: parts = fixture_label.split('.') if len(parts) == 1: fixture_name = fixture_label formats = serializers.get_public_serializer_formats() else: fixture_name, format = '.'.join(parts[:-1]), parts[-1] if format in serializers.get_public_serializer_formats(): formats = [format] else: formats = [] if formats: if verbosity > 1: print("Loading '%s' fixtures..." % fixture_name) else: raise SyncDataError("Problem installing fixture '%s': %s is not a known serialization format." % (fixture_name, format)) if os.path.isabs(fixture_name): fixture_dirs = [fixture_name] else: fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + [''] for fixture_dir in fixture_dirs: if verbosity > 1: print("Checking %s for fixtures..." % humanize(fixture_dir)) label_found = False for format in formats: if verbosity > 1: print("Trying %s for %s fixture '%s'..." % (humanize(fixture_dir), format, fixture_name)) try: full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format])) fixture = open(full_path, 'r') if label_found: fixture.close() raise SyncDataError("Multiple fixtures named '%s' in %s. Aborting." % (fixture_name, humanize(fixture_dir))) else: fixture_count += 1 objects_per_fixture.append(0) if verbosity > 0: print("Installing %s fixture '%s' from %s." % (format, fixture_name, humanize(fixture_dir))) try: objects_to_keep = {} objects = serializers.deserialize(format, fixture) for obj in objects: object_count += 1 objects_per_fixture[-1] += 1 class_ = obj.object.__class__ if class_ not in objects_to_keep: objects_to_keep[class_] = set() objects_to_keep[class_].add(obj.object) models.add(class_) obj.save() if options['remove']: self.remove_objects_not_in(objects_to_keep, verbosity) label_found = True except (SystemExit, KeyboardInterrupt): raise except Exception: import traceback fixture.close() transaction.rollback() if show_traceback: traceback.print_exc() raise SyncDataError("Problem installing fixture '%s': %s\n" % (full_path, traceback.format_exc())) fixture.close() except SyncDataError as e: raise e except Exception: if verbosity > 1: print("No %s fixture '%s' in %s." % (format, fixture_name, humanize(fixture_dir))) # If any of the fixtures we loaded contain 0 objects, assume that an # error was encountered during fixture loading. if 0 in objects_per_fixture: raise SyncDataError("No fixture data found for '%s'. (File format may be invalid.)" % fixture_name) # If we found even one object in a fixture, we need to reset the # database sequences. if object_count > 0: sequence_sql = connections[self.using].ops.sequence_reset_sql(self.style, models) if sequence_sql: if verbosity > 1: print("Resetting sequences") for line in sequence_sql: cursor.execute(line) if object_count == 0: if verbosity > 1: print("No fixtures found.") else: if verbosity > 0: print("Installed %d object%s from %d fixture%s" % ( object_count, pluralize(object_count), fixture_count, pluralize(fixture_count) ))
def roller_back(): """ Perform a database query, then rollback the transaction """ Mod.objects.count() transaction.rollback()
def jsonSpecialtyCrudv0(request): if request.is_ajax() and request.method == 'GET': id = request.GET.get('id', None) action = request.GET.get('action', None) if (action=="filter"): dictData = SchoolToGrade.objects.filter(id=id).values() else: dictData = SchoolToGrade.objects.all().values() #print dictData jsonData = json.dumps(list(dictData)) return HttpResponse(jsonData, content_type='application/json') if request.is_ajax() and request.method == 'POST': action = request.POST.get('action', None) #ADD if (action=="add"): code = request.POST.get('code', None) name = request.POST.get('name', None) record = Specialty(code = code , name = name ) record.save() dictData = Specialty.objects.filter(id=record.id).values() jsonData = json.dumps(dictData) return HttpResponse(jsonData, content_type='application/json') #DELETE if (action=="delete"): id = request.POST.get('id', None) record = Specialty.objects.filter(id=id) try: record.delete() result = "Specialty (%s) deleted successfully." %(record.name) except IntegrityError: transaction.rollback() result = "Error Specialty (%s) could not be deleted." %(record.name) return HttpResponse(result, content_type='application/json') #UPDATE elif (action=="update"): id = request.POST.get('id', None) code = request.POST.get('code', None) name = request.POST.get('name', None) record = Specialty.objects.filter(id=id) record.update( code = code , name = name, ) dictData = record.values() #print dictData jsonData = json.dumps(list(dictData)) return HttpResponse(jsonData, content_type='application/json') else: dictData = 'POST action Not Found' jsonData = json.dumps(dictData) return HttpResponse(jsonData, content_type='application/json') #raise Http404 # DEFAULT else: dictData = Specialty.objects.all().values() jsonData = json.dumps(list(dictData)) #print jsonData return HttpResponse(jsonData, content_type='application/json')
def load_issue(self, data): if data.type == 'ahead': logger.info('Issue (Ahead) will not be imported') return journal = self.load_journal(data.journal) if not journal: return self._load_journal_status_history(journal, data.journal.status_history) logger.info('Importing Issue (%s)' % (data.label)) try: issue = self._issue_exists(journal, data) or Issue() except exceptions.MultipleObjectsReturned as e: logger.error('Multiple issues found this new issue will not be created') transaction.rollback() return spe = data.number.replace('spe', '') if data.type == 'special' else None suppl = ' '.join([ data.supplement_volume or '', data.supplement_number or '' ]).strip() if data.type == 'supplement' else None issue.journal = journal issue.publication_year = data.publication_date[:4] issue.volume = data.volume or '' issue.number = data.number or '' issue.type = data.type if data.type == 'special': issue.number = data.number.replace('spe', '') if data.number else '' issue.spe_text = spe if data.type == 'supplement' and suppl: issue.suppl_text = suppl issue.is_press_release = data.is_press_release issue.total_documents = data.total_documents or 0 issue.publication_start_month = data.start_month or 0 issue.publication_end_month = data.end_month or 0 issue.is_marked_up = data.is_marked_up issue.ctrl_vocabulary = data.controlled_vocabulary[0] if data.controlled_vocabulary else '' issue.editorial_standard = data.editorial_standard[0] if data.editorial_standard else '' try: issue.save(force_insert=True) except DatabaseError as e: logger.error(e.message) logger.error('Issue (%s) not imported' % (data.label)) transaction.rollback() return except IntegrityError as e: logger.error(e.message) logger.error('Issue (%s) not imported' % (data.label)) transaction.rollback() return self._post_save_issue(issue, data) logger.info('Issue (%s) created' % (data.label))
def jsonLessonCrud(request): # Display on GET if request.is_ajax() and request.method == 'GET': id = request.GET.get('id', None) action = request.GET.get('action', None) if (action=="filter"): dictData = Lesson.objects.filter(id=id).values() else: dictData = Lesson.objects.all().values() #print dictData jsonData = json.dumps(list(dictData)) return HttpResponse(jsonData, content_type='application/json') # CRUD on POST if request.is_ajax() and request.method == 'POST': action = request.POST.get('action', None) #dictData = Lesson.objects.all().values() #ADD if (action=="add"): #Fakeloi 1o 2o xeri (AB) #booksAB = models.IntegerField(default=0, blank=False) #booksABFolders = models.IntegerField(default=0, blank=False) #booksC = request.POST.get('booksC', None) #booksCFolders = request.POST.get('booksCFolders', None) category = request.POST.get('category', None) name = request.POST.get('name', None) type = request.POST.get('type', None) record = Folder(category = category, name = name, type = type) record.save() # return record dictData = Lesson.objects.filter(id=record.id).values() jsonData = json.dumps(dictData) return HttpResponse(jsonData, content_type='application/json') # UPDATE elif (action=="update"): id = request.POST.get('id', None) #Fakeloi 1o 2o xeri (AB) #booksAB booksABFolders booksC = request.POST.get('booksC', None) booksCFolders = request.POST.get('booksCFolders', None) category = request.POST.get('category', None) name = request.POST.get('name', None) type = request.POST.get('type', None) record = Lesson.objects.filter(id=id) record.update( booksC = booksC, booksCFolders = booksCFolders, category = category, name=name , type = type, ) # return Lesson record dictData = record.values() jsonData = json.dumps(list(dictData)) #print jsonData return HttpResponse(jsonData, content_type='application/json') #DELETE if (action=="delete"): id = request.POST.get('id', None) record = Lesson.objects.filter(id=id) try: record.delete() result = "Lesson (%s) deleted successfully." %(record.name) except IntegrityError: transaction.rollback() result = "Error Lesson (%s) could not be deleted." %(record.name) return HttpResponse(result, content_type='application/json') # else: No POST Default # DEFAULT else: dictData = Lesson.objects.all().values() jsonData = json.dumps(list(dictData)) #print jsonData return HttpResponse(jsonData, content_type='application/json')
def move_page(locale, slug, new_slug, user_id): transaction.set_autocommit(False) User = get_user_model() try: user = User.objects.get(id=user_id) except User.DoesNotExist: transaction.rollback() logging.error('Page move failed: no user with id %s' % user_id) return try: doc = Document.objects.get(locale=locale, slug=slug) except Document.DoesNotExist: transaction.rollback() message = """ Page move failed. Move was requested for document with slug %(slug)s in locale %(locale)s, but no such document exists. """ % { 'slug': slug, 'locale': locale } logging.error(message) send_mail('Page move failed', textwrap.dedent(message), settings.DEFAULT_FROM_EMAIL, [user.email]) transaction.set_autocommit(True) return try: doc._move_tree(new_slug, user=user) except PageMoveError as e: transaction.rollback() message = """ Page move failed. Move was requested for document with slug %(slug)s in locale %(locale)s, but could not be completed. Diagnostic info: %(message)s """ % { 'slug': slug, 'locale': locale, 'message': e.message } logging.error(message) send_mail('Page move failed', textwrap.dedent(message), settings.DEFAULT_FROM_EMAIL, [user.email]) transaction.set_autocommit(True) return except Exception as e: transaction.rollback() message = """ Page move failed. Move was requested for document with slug %(slug)s in locale %(locale)s, but could not be completed. %(info)s """ % { 'slug': slug, 'locale': locale, 'info': e } logging.error(message) send_mail('Page move failed', textwrap.dedent(message), settings.DEFAULT_FROM_EMAIL, [user.email]) transaction.set_autocommit(True) return transaction.commit() transaction.set_autocommit(True) # Now that we know the move succeeded, re-render the whole tree. for moved_doc in [doc] + doc.get_descendants(): moved_doc.schedule_rendering('max-age=0') subject = 'Page move completed: ' + slug + ' (' + locale + ')' full_url = settings.SITE_URL + '/' + locale + '/docs/' + new_slug # Get the parent document, if parent doc is None, it means its the parent document parent_doc = doc.parent or doc other_locale_urls = [ settings.SITE_URL + translation.get_absolute_url() for translation in parent_doc.translations.exclude( locale=doc.locale).order_by('locale') ] # If the document is a translation we should include the parent document url to the list if doc.parent: other_locale_urls = [ settings.SITE_URL + doc.parent.get_absolute_url() ] + other_locale_urls message = textwrap.dedent(""" Page move completed. The move requested for the document with slug %(slug)s in locale %(locale)s, and all its children, has been completed. The following localized articles may need to be moved also: %(locale_urls)s You can now view this document at its new location: %(full_url)s. """) % { 'slug': slug, 'locale': locale, 'full_url': full_url, 'locale_urls': '\n'.join(other_locale_urls) } send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email])
def submitIngresarActa(request): if request.POST: institucion = request.POST['institucion'] departamento = request.POST['departamentos'] provincia = request.POST['provinciaDeTrabajo'] ciudad = request.POST['ciudades'] #es el CANTON lugarDeInstalacion = request.POST['lugarDeInstalacion'] fechaDeInstalacion = request.POST['fechaDeInstalacion'] # solo date fechaDeCreacion = request.POST['fechaDeCreacion'] #datetime nombreUnidad = request.POST['nombreUnidad'] codigoUnidad = request.POST['codigoUnidad'] marcaVehiculo = request.POST['marcaVehiculo'] modeloVehiculo = request.POST['modeloVehiculo'] placaVehiculo = request.POST['placaVehiculo'] anioVehiculo = request.POST['anioVehiculo'] estadoVehiculo = request.POST['estadoVehiculo'] puntoDeInstalacion = request.POST['puntoDeInstalacion'] codigoImei = request.POST['codigoImei'] imei = request.POST['imei'] numeroDeSerie = request.POST['numeroDeSerie'] nombreDeServidor = request.POST['nombreDeServidor'] IDnroTarjeta = request.POST['IDnroTarjeta'] nroTarjeta = request.POST['nroTarjeta'] #es la LINEA IDSimCard = request.POST['IDSimCard'] nroSerialTarjetaSim = request.POST['nroSerialTarjetaSim'] contrasenia = request.POST['contrasenia'] intervaloTransmision = request.POST['intervaloTransmision'] voltaje = request.POST['voltaje'] tipoVehiculo = request.POST['tipoVehiculo'] nota = request.POST['nota'] imagen1 = None imagen2 = None try: imagen1 = request.FILES['imagen1'] except Exception as ex: print "No se ha ingresado imagen 1" try: imagen2 = request.FILES['imagen2'] except Exception as ex: print "No se ha ingresado imagen 2" #*** dos paneles de abajo*** nombreInstalador = request.POST['nombreInstalador'] cargoInstalador = request.POST['cargoInstalador'] nombreResponsable = request.POST['nombreResponsable'] cargoResponsable = request.POST['cargoResponsable'] #*************************** idLinea = gps_lineas.objects.get(li_id=IDnroTarjeta) idImei = gps_imei.objects.get(im_id=codigoImei) idUnidad = gps_unidades.objects.get(un_id=codigoUnidad) idGpsSimCard = gps_sim_card.objects.get(si_id=IDSimCard) idDepartamento = gps_departamento.objects.get(de_id=departamento) try: registroULI = gps_imei_linea_unidad( uli_imei=imei, uli_linea=nroTarjeta, uli_canton=ciudad, uli_unidad=nombreUnidad, uli_estado_unidad=estadoVehiculo, uli_fecha_inicio=fechaDeInstalacion, uli_estado_actual="ACTIVO", uli_fecha_creacion=fechaDeCreacion, uli_fecha_modificacion=fechaDeCreacion, uli_estado_registro=True, uli_linea_id=idLinea, uli_imei_id=idImei, uli_unidades_id=idUnidad, uli_sim_card_id=idGpsSimCard, uli_departamento_id=idDepartamento) registroULI.save() extraerInstitucion = gps_instituciones.objects.get( in_id=institucion) idUli = gps_imei_linea_unidad.objects.get( uli_imei=imei, uli_linea=nroTarjeta, uli_canton=ciudad, uli_unidad=nombreUnidad, uli_estado_unidad=estadoVehiculo, uli_fecha_inicio=fechaDeInstalacion) registroActas = gps_actas( ac_tipo="", ac_fecha_instalacion=fechaDeInstalacion, ac_punto_instalacion=puntoDeInstalacion, ac_nombre_servidor=nombreDeServidor, ac_contrasenia=contrasenia, ac_intervalo=intervaloTransmision, ac_voltaje=voltaje, ac_nota=nota, ac_lugar_de_instalacion=lugarDeInstalacion, ac_canton=ciudad, ac_provincia=provincia, ac_imei=imei, ac_serie_imei=numeroDeSerie, ac_linea=nroTarjeta, ac_sim_card=nroSerialTarjetaSim, ac_unidad=nombreUnidad, ac_institucion=extraerInstitucion.in_nombre, ac_departamento=idDepartamento.de_departmentName, ac_marca_unidad=marcaVehiculo, ac_modelo_unidad=modeloVehiculo, ac_placa_unidad=placaVehiculo, ac_anio_unidad=anioVehiculo, ac_estado_unidad=estadoVehiculo, ac_tipo_vehiculo=tipoVehiculo, ac_tecnico=nombreInstalador, ac_cargo_tecnico=cargoInstalador, ac_responsable_unidad=nombreResponsable, ac_cargo_responsable=cargoResponsable, ac_estado_registro=True, ac_uli_id=idUli, ac_imagen1=imagen1, ac_imagen2=imagen2) registroActas.save() transaction.commit() return render_to_response( 'mensajes.html', { "mensaje": "Acta Guardada Correctamente... Redireccionando, por favor espere...", "pagina": "/ingresarActa/" }) except: transaction.rollback() return render_to_response('mensajes.html', { "mensaje": "Ha ocurrido un error...", "pagina": "/ingresarActa/" }) else: return HttpResponseRedirect('/ingresarActa/')
def tearDown(self): # The tests access the database after exercising 'atomic', making the # connection dirty; a rollback is required to make it clean. transaction.rollback() transaction.leave_transaction_management()
def load_journal(self, data): issns = set() issns.add(data.scielo_issn) issns.add(data.print_issn) issns.add(data.electronic_issn) try: journal = Journal.objects.get( Q(print_issn__in=issns) | Q(eletronic_issn__in=issns)) logger.info('Journal already exists, skiping journal creation') return journal except exceptions.ObjectDoesNotExist: logger.info('Journal do no exists, creating journal') logger.info('Importing Journal (%s)' % data.title) journal = Journal() journal.creator_id = self.user.pk journal.collection = self.collection journal.scielo_issn = 'electronic' if data.scielo_issn == data.electronic_issn else 'print' journal.print_issn = data.print_issn or '' journal.eletronic_issn = data.electronic_issn or '' journal.title = data.title or '' journal.title_iso = data.abbreviated_iso_title or '' journal.short_title = data.abbreviated_title or '' journal.medline_title = data.title_nlm or '' journal.acronym = data.acronym journal.subject_descriptors = '\n'.join(data.subject_descriptors or []) journal.index_coverage = '\n'.join(data.subject_descriptors or []) journal.copyrighter = data.copyrighter or '' journal.init_year = data.first_year or '' journal.init_vol = data.first_volume or '' journal.init_num = data.first_number or '' journal.final_year = data.last_year or '' journal.final_vol = data.last_volume or '' journal.final_num = data.last_number or '' journal.cnn_code = data.cnn_code or '' journal.frequency = data.periodicity[0] if data.periodicity else '' journal.url_online_submission = data.submission_url or '' journal.url_journal = data.institutional_url or data.url() or '' journal.pub_status = data.current_status or '' journal.editorial_standard = data.editorial_standard[0] if data.editorial_standard else '' journal.ctrl_vocabulary = data.controlled_vocabulary[0] if data.controlled_vocabulary else '' journal.pub_level = data.publication_level[0] if data.publication_level else '' journal.secs_code = data.secs_code or '' journal.publisher_name = '; '.join(data.publisher_name) if data.publisher_name else '' journal.publisher_country = data.publisher_country[0] if data.publisher_country else '' journal.publisher_state = data.publisher_state or '' journal.publisher_city = data.publisher_city or '' journal.editor_address = data.editor_address or '' journal.editor_email = data.editor_email or '' journal.is_indexed_scie = data.is_indexed_in_scie journal.is_indexed_ssci = data.is_indexed_in_ssci journal.is_indexed_aehci = data.is_indexed_in_ahci try: journal.save(force_insert=True) except DatabaseError as e: logger.error(e.message) logger.error('Journal (%s) not imported' % (data.title)) transaction.rollback() return except IntegrityError as e: logger.error(e.message) logger.error('Journal (%s) not imported' % (data.title)) transaction.rollback() return self._post_save_journal(journal, data) logger.info('Journal (%s) created' % data.title) return journal
def tearDown(self): # The tests access the database after exercising 'atomic', initiating # a transaction ; a rollback is required before restoring autocommit. transaction.rollback() transaction.set_autocommit(True)
def test_atomic_prevents_calling_transaction_methods(self): with transaction.atomic(): with self.assertRaises(transaction.TransactionManagementError): transaction.commit() with self.assertRaises(transaction.TransactionManagementError): transaction.rollback()