def main(): if Membership.objects.count() > 0 or Payment.objects.count() > 0: print "Database not empty, refusing to generate test data" sys.exit(1) # Approved members for i in xrange(1,1000): membership = create_dummy_member(i) membership.preapprove(user) membership.approve(user) create_payment(membership) transaction.commit() # Pre-approved members for i in xrange(1000,1100): membership = create_dummy_member(i) membership.preapprove(user) transaction.commit() # New applications for i in xrange(1100,1200): membership = create_dummy_member(i) transaction.commit() management.call_command('makebills') transaction.commit() for payment in Payment.objects.all(): try: attach_payment_to_cycle(payment) transaction.commit() except: pass
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 getNextSong(self): # commit transaction to force fresh queryset result try: transaction.enter_transaction_management() transaction.commit() except BaseException: pass try: data = Queue.objects.all() data = data.annotate(VoteCount=Count("User")) data = data.annotate(MinCreated=Min("Created")) data = data.order_by("-VoteCount", "MinCreated")[0:1].get() self.addToHistory(data.Song, data.User) song_instance = data.Song data.delete() except ObjectDoesNotExist: try: song_instance = self.getRandomSongByPreferences() self.addToHistory(song_instance, None) except ObjectDoesNotExist: song_instance = Song.objects.order_by('?')[0:1].get() self.addToHistory(song_instance, None) # remove missing files if not os.path.exists(song_instance.Filename.name.encode('utf8')): Song.objects.all().filter(id=song_instance.id).delete() return self.getNextSong() return song_instance
def buildStarted(self, builderName, build): log.msg("build started on %s" % builderName) builder = Builder.objects.get(master=dbm, name=builderName) slave, _ = Slave.objects.get_or_create(name=build.getSlavename()) ss = modelForSource(dbm, build.getSourceStamp()) starttime = timeHelper(build.getTimes()[0]) dbbuild, created = \ builder.builds.get_or_create(buildnumber=build.getNumber(), slave=slave, starttime=starttime, reason=build.getReason(), sourcestamp=ss) if not created: log.msg("%s build %d not created, endtime is %s" % (builderName, build.getNumber(), dbbuild.endtime)) log.msg("not watch this build, to make sure") return for key, value, source in build.getProperties().asList(): dbbuild.setProperty(key, value, source) dbbuild.save() transaction.commit() basedir = os.path.join(build.getBuilder().basedir, '..') return BuildReceiver(dbbuild, basedir)
def _fixture_teardown(cls): """Empty (only) the tables we loaded fixtures into, then commit.""" if hasattr(cls, 'fixtures'): for db in cls._databases(): tables = tables_used_by_fixtures(cls.fixtures, using=db) # TODO: Think about respecting _meta.db_tablespace, not just # db_table. if tables: connection = connections[db] cursor = connection.cursor() # TODO: Rather than assuming that anything added to by a # fixture can be emptied, remove only what the fixture # added. This would probably solve input.mozilla.com's # failures (since worked around) with Site objects; they # were loading additional Sites with a fixture, and then # the Django-provided example.com site was evaporating. if uses_mysql(connection): cursor.execute('SET FOREIGN_KEY_CHECKS=0') for table in tables: # Truncate implicitly commits. cursor.execute('TRUNCATE `%s`' % table) # TODO: necessary? cursor.execute('SET FOREIGN_KEY_CHECKS=1') else: for table in tables: cursor.execute('DELETE FROM %s' % table) transaction.commit(using=db)
def pull_from_xqueue(): """ Constant loop that pulls from queue and posts to grading controller """ log.info(' [*] Pulling from xqueues...') #Define sessions for logging into xqueue and controller xqueue_session = util.xqueue_login() controller_session = util.controller_login() #Sleep for some time to allow other pull_from_xqueue processes to get behind/ahead time_sleep_value = random.uniform(0, .1) time.sleep(time_sleep_value) #Loop through each queue that is given in arguments for queue_name in settings.GRADING_QUEUES_TO_PULL_FROM: #Check for new submissions on xqueue, and send to controller pull_from_single_grading_queue(queue_name,controller_session,xqueue_session, project_urls.ControllerURLs.submit, project_urls.ControllerURLs.status) #Loop through message queues to see if there are any messages for queue_name in settings.MESSAGE_QUEUES_TO_PULL_FROM: pull_from_single_grading_queue(queue_name,controller_session,xqueue_session, project_urls.ControllerURLs.submit_message, project_urls.ControllerURLs.status) #Check for finalized results from controller, and post back to xqueue transaction.commit() submissions_to_post = check_for_completed_submissions() for submission in list(submissions_to_post): post_one_submission_back_to_queue(submission, xqueue_session) # Log out of the controller session, which deletes the database row. util.controller_logout(controller_session) transaction.commit()
def run_select_for_update(self, status, nowait=False): """ Utility method that runs a SELECT FOR UPDATE against all Person instances. After the select_for_update, it attempts to update the name of the only record, save, and commit. This function expects to run in a separate thread. """ status.append('started') try: # We need to enter transaction management again, as this is done on # per-thread basis transaction.enter_transaction_management() people = list( Person.objects.all().select_for_update(nowait=nowait) ) people[0].name = 'Fred' people[0].save() transaction.commit() except DatabaseError as e: status.append(e) finally: # This method is run in a separate thread. It uses its own # database connection. Close it without waiting for the GC. transaction.abort() connection.close()
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 destroy(request, app_name, model_name, user, id=None): ''' ' Receive a model_name and data object via ajax, and remove that item, ' returning either a success or error message. ''' cls = apps.get_model(app_name, model_name) try: obj = cls.objects.get_editable_by_pk(user, id) if obj is None: transaction.rollback() error = "User %s does not have permission to delete this object." % user return HttpResponse(json.dumps({'errors': error}, indent=4), content_type="application/json") except Exception as e: transaction.rollback() error = "There was an error for user %s trying to delete this object: %s" % (user, str(e)) return HttpResponse(json.dumps({'errors': error}, indent=4), content_type="application/json") try: obj.delete() except Exception as e: transaction.rollback() error = "Unexpected error deleting object: %s: %s" % (type(e), e) return HttpResponse(json.dumps({'errors': error}, indent=4), content_type="application/json") transaction.commit() dump = json.dumps({'success': 'Successfully deleted item with primary key: %s' % id}, indent=4) response = HttpResponse(dump, content_type="application/json") response.status_code = 201 return response
def rest_notes(request): """ View to create new advisor notes via RESTful POST (json) """ if request.method != 'POST': resp = HttpResponse(content='Only POST requests allowed', status=405) resp['Allow'] = 'POST' transaction.rollback() return resp if request.META['CONTENT_TYPE'] != 'application/json' and not request.META['CONTENT_TYPE'].startswith('application/json;'): transaction.rollback() return HttpResponse(content='Contents must be JSON (application/json)', status=415) try: rest.new_advisor_notes(request.body) except UnicodeDecodeError: transaction.rollback() return HttpResponse(content='Bad UTF-8 encoded text', status=400) except ValueError: transaction.rollback() return HttpResponse(content='Bad JSON in request body', status=400) except ValidationError as e: transaction.rollback() return HttpResponse(content=e.messages[0], status=422) except Exception as e: transaction.rollback() raise transaction.commit() return HttpResponse(status=200)
def save_values(item_container, old, new): """ Abspeichern der geaenderten Werte """ new = encode_html_dir(new) item_container.container.save_values(old, new) item_container.item.save_values(old, new) item_container.save_modified_values(old, new) transaction.commit()
def store_similarities(self, itemMatch): try: logger.info('saving similarities') count = 0 for object_id, scores in itemMatch: object_target, object_target_site = self.resolve_identifier( object_id) for related_object_id, score in scores: if not math.isnan(score) and score > self.threshold_similarities: object_related, object_related_site = self.resolve_identifier( related_object_id) if object_target != object_related: count = count + 1 Similarity.objects.set_score_for_objects( object_target=object_target, object_target_site=object_target_site, object_related=object_related, object_related_site=object_related_site, score=score ) if count % RECOMMENDS_STORAGE_COMMIT_THRESHOLD == 0: logger.debug( 'saved %s similarities...' % count) transaction.commit() finally: logger.info('saved %s similarities...' % count) transaction.commit()
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 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 process_response(self, request, response): """Commits and leaves transaction management.""" if transaction.is_managed(using=self.get_tenant(request)): if transaction.is_dirty(using=self.get_tenant(request)): transaction.commit(using=self.get_tenant(request)) transaction.leave_transaction_management(using=self.get_tenant(request)) return response
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 save_values(item_container, old, new): old['integer_3'] = old['lernrestyp'] old['integer_4'] = old['medienformat'] #old['integer_5'] = old['zertifikat'] new['integer_3'] = new['lernrestyp'] new['integer_4'] = new['medienformat'] #new['integer_5'] = new['zertifikat'] edu_item = get_eduitem(item_container.item) save_modified_schlagworte(edu_item, old, new) #old['extra'] = set_extra_data(schlagwort_org=old['schlagwort']) #new['extra'] = encode_html(set_extra_data(schlagwort_org=new['schlagwort'])) old['extra'] = set_extra_data(schlagwort_org=decode_html(old['schlagwort'])) new['extra'] = set_extra_data(schlagwort_org=encode_html(new['schlagwort'])) save_modified_multiple_checkbox(old, new, 'fach_sachgebiet', edu_item.fach_sachgebiet) save_modified_multiple_checkbox(old, new, 'schulart', edu_item.schulart) save_modified_multiple_checkbox(old, new, 'schulstufe', edu_item.schulstufe) save_modified_multiple_checkbox(old, new, 'sprache', edu_item.sprache) save_modified_multiple_checkbox(old, new, 'zielgruppe', edu_item.zielgruppe) edu_item.save_modified_values(old, new) if item_container.is_data_object: save_item(item_container, old, new) else: save_item_container(item_container, old, new) transaction.commit()
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 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 _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 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 test_threads(): if db.connection.vendor == 'sqlite': pytest.skip("in-memory sqlite db can't be used between threads") obj = TriggerConcurrentModel.objects.create() transaction.commit() @concurrently(25) def run(): for i in range(5): while True: x = refetch(obj) transaction.commit() x.count += 1 try: x.save() transaction.commit() except RecordModifiedError: # retry pass else: break run() assert refetch(obj).count == 5 * 25
def override_field_for_ccx(ccx, block, name, value): """ Overrides a field for the `ccx`. `block` and `name` specify the block and the name of the field on that block to override. `value` is the value to set for the given field. """ field = block.fields[name] value_json = field.to_json(value) serialized_value = json.dumps(value_json) try: override = CcxFieldOverride.objects.create( ccx=ccx, location=block.location, field=name, value=serialized_value ) except IntegrityError: transaction.commit() override = CcxFieldOverride.objects.get( ccx=ccx, location=block.location, field=name ) override.value = serialized_value override.save() _get_overrides_for_ccx(ccx).setdefault(block.location, {})[name] = value_json
def schedule_changed(self): try: close_old_connections() # If MySQL is running with transaction isolation level # REPEATABLE-READ (default), then we won't see changes done by # other transactions until the current transaction is # committed (Issue #41). try: transaction.commit() except transaction.TransactionManagementError: pass # not in transaction management. last, ts = self._last_timestamp, self.Changes.last_change() except DatabaseError as exc: logger.exception('Database gave error: %r', exc) return False except InterfaceError: warning( 'DatabaseScheduler: InterfaceError in schedule_changed(), ' 'waiting to retry in next call...' ) return False try: if ts and ts > (last if last else ts): return True finally: self._last_timestamp = ts return False
def update(self, request, workspace_id, tab_id, igadget_id): user = get_user_authentication(request) received_json = PUT_parameter(request, 'variables') # Gets JSON parameter from request if not received_json: return HttpResponseBadRequest(get_xml_error(_("iGadget variables JSON expected")), mimetype='application/xml; charset=UTF-8') try: received_variables = simplejson.loads(received_json) tab = Tab.objects.get(workspace__users__id=user.id, workspace__pk=workspace_id, pk=tab_id) server_variables = Variable.objects.filter(igadget__tab=tab) # Gadget variables collection update for varServer in server_variables: for varJSON in received_variables: if (varServer.vardef.pk == varJSON['pk'] and varServer.igadget.pk == varJSON['iGadget']): varServer.value = varJSON['value'] varServer.save() transaction.commit() except Tab.DoesNotExist, e: msg = _('referred tab %(tab_id)s does not exist.') % {'tab_id': tab_id} raise TracedServerError(e, {'workspace': workspace_id, 'tab': tab_id, 'igadget': igadget_id}, request, msg)
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 delete_floating_ip(floating_ip): if floating_ip.nic: # This is safe, you also need for_update to attach floating IP to # instance. server = floating_ip.nic.machine if server is None: msg = ("Floating IP '%s' is used by port '%s'" % (floating_ip.id, floating_ip.nic_id)) else: msg = ("Floating IP '%s' is used by server '%s'" % (floating_ip.id, floating_ip.nic.machine_id)) raise faults.Conflict(msg) # Lock network to prevent deadlock Network.objects.select_for_update().get(id=floating_ip.network_id) # Return the address of the floating IP back to pool floating_ip.release_address() # And mark the floating IP as deleted floating_ip.deleted = True floating_ip.save() # Release quota for floating IP quotas.issue_and_accept_commission(floating_ip, action="DESTROY") transaction.commit() # Delete the floating IP from DB log.info("Deleted floating IP '%s' of user '%s", floating_ip, floating_ip.userid) floating_ip.delete()
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 update(self, **kwargs): """ Updates all elements in the current QuerySet, setting all the given fields to the appropriate values. """ assert self.query.can_filter(), \ "Cannot update a query once a slice has been taken." query = self.query.clone(sql.UpdateQuery) query.add_update_values(kwargs) if not transaction.is_managed(): transaction.enter_transaction_management() forced_managed = True else: forced_managed = False try: rows = query.execute_sql(None) if forced_managed: transaction.commit() else: transaction.commit_unless_managed() finally: if forced_managed: transaction.leave_transaction_management() self._result_cache = None return rows
def my_books (request, username): """ Django View. Show user books. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. """ from django.contrib.auth.models import User from booki.editor import models try: user = User.objects.get(username=username) except User.DoesNotExist: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) books = models.Book.objects.filter(owner=user) if request.POST.get("action") == "hide": book = models.Book.objects.get(url_title=request.POST.get("book")) book.hidden = True book.save() transaction.commit() elif request.POST.get("action") == "unhide": book = models.Book.objects.get(url_title=request.POST.get("book")) book.hidden = False book.save() transaction.commit() if request.method == 'POST' and not request.POST.get("action"): project_form = BookForm(request.POST) import_form = ImportForm(request.POST) if import_form.is_valid() and import_form.cleaned_data["id"]: project_form = BookForm() # reset the other form try: ID = import_form.cleaned_data["id"] import_type = import_form.cleaned_data["type"] rename_title = import_form.cleaned_data["rename_title"] extraOptions = {} if rename_title: extraOptions['book_title'] = rename_title if import_form.cleaned_data["hidden"]: extraOptions['hidden'] = True import_sources = { # base_url source= 'flossmanuals': (TWIKI_GATEWAY_URL, "en.flossmanuals.net"), "archive": (ESPRI_URL, "archive.org"), "wikibooks": (ESPRI_URL, "wikibooks"), "epub": (ESPRI_URL, "url"), } if import_type == "booki": ID = ID.rstrip("/") booki_url, book_url_title = ID.rsplit("/", 1) base_url = "%s/export/%s/export" % (booki_url, book_url_title) source = "booki" else: base_url, source = import_sources[import_type] common.importBookFromUrl2(user, base_url, args=dict(source=source, book=ID), **extraOptions ) except Exception: transaction.rollback() logError(traceback.format_exc()) return render_to_response('account/error_import.html', {"request": request, "user": user}) else: transaction.commit() #XXX should this be elif? even if the POST is valid as both forms, the # transaction will end up being commited twice. if project_form.is_valid() and project_form.cleaned_data["title"]: import_form = ImportForm() # reset the other form from booki.utils.book import createBook title = project_form.cleaned_data["title"] try: book = createBook(user, title) license = project_form.cleaned_data["license"] lic = models.License.objects.get(abbrevation=license) book.license = lic book.hidden = project_form.cleaned_data["hidden"] book.save() except: transaction.rollback() else: transaction.commit() return HttpResponseRedirect(reverse("my_books", args=[username])) else: project_form = BookForm() import_form = ImportForm() try: return render_to_response('account/my_books.html', {"request": request, "user": user, "project_form": project_form, "import_form": import_form, "books": books,}) except: transaction.rollback() finally: transaction.commit()
if not request.PUT.has_key('igadgets'): return HttpResponseBadRequest(get_xml_error(_("iGadget JSON expected")), mimetype='application/xml; charset=UTF-8') #TODO we can make this with deserializers (simplejson) received_json = request.PUT['igadgets'] try: received_data = eval(received_json) except Exception, e: return HttpResponseBadRequest(get_xml_error(unicode(e)), mimetype='application/xml; charset=UTF-8') igadgets = received_data.get('iGadgets') try: for igadget in igadgets: UpdateIGadget(igadget, user, screen_id, igadget_id=None) transaction.commit() return HttpResponse('ok') except Exception, e: transaction.rollback() msg = _("iGadgets cannot be updated: ") + unicode(e) log(msg, request) return HttpResponseServerError(get_xml_error(msg), mimetype='application/xml; charset=UTF-8') class IGadgetEntry(Resource): def read(self, request, user_name, igadget_id, screen_id=None): user = user_authentication(request, user_name) #TODO by default. Remove in final release if not screen_id: screen_id = 1
def commit(using=None): using = select_db("im") if using is None else using django_transaction.commit(using=using)
def upload_attachment(request, bookid, version=None): """ Uploades attachments. Used from Upload dialog. @param request: Django Request @param bookid: Book ID @param version: Book version or None """ try: book = models.Book.objects.get(url_title__iexact=bookid) except models.Book.DoesNotExist: return pages.ErrorPage(request, "errors/book_does_not_exist.html", {"book_name": bookid}) book_version = book.getVersion(version) stat = models.BookStatus.objects.filter(book = book)[0] operationResult = True # check this for transactions try: for name, fileData in request.FILES.items(): from booki.utils import log log.logBookHistory(book = book, version = book_version, args = {'filename': request.FILES[name].name}, user = request.user, kind = 'attachment_upload') att = models.Attachment(version = book_version, # must remove this reference book = book, status = stat) att.save() att.attachment.save(request.FILES[name].name, fileData, save = False) att.save() # TODO: # must write info about this to log! # maybe check file name now and save with new name transaction.commit() except IOError: operationResult = False transaction.rollback() except: oprerationResult = False transaction.rollback() if request.POST.get("attachmenttab", "") == "": return HttpResponse('<html><body><script> parent.closeAttachmentUpload(); </script></body></html>') if request.POST.get("attachmenttab", "") == "2": return HttpResponse('<html><body><script> parent.FileBrowserDialogue.loadAttachments(); parent.FileBrowserDialogue.showUpload(); parent.mcTabs.displayTab("browse_tab","browse_panel");</script></body></html>') # should not call showAttachmentsTab, but it works for now if operationResult: return HttpResponse('<html><body><script> parent.jQuery.booki.editor.showAttachmentsTab(); parent.jQuery("#tabattachments FORM")[0].reset(); </script></body></html>') else: return HttpResponse('<html><body><script> parent.jQuery.booki.editor.showAttachmentsTab(); parent.jQuery("#tabattachments FORM")[0].reset(); alert(parent.jQuery.booki._("errorupload", "Error while uploading file!"));</script></body></html>')
def handle(self, *fixture_labels, **options): """ Main method of a Django command """ 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) # 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 = connection.cursor() # Start transaction management. All fixtures are installed in a # single transaction to ensure that all references are resolved. transaction.commit_unless_managed() transaction.enter_transaction_management() transaction.managed(True) 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: 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: 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: #serializer = serializers.get_serializer(format) 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() 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_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 not class_ in objects_to_keep: objects_to_keep[class_] = set() objects_to_keep[class_].add(obj.object) models.add(class_) obj.save() self.remove_objects_not_in( objects_to_keep, verbosity) label_found = True except (SystemExit, KeyboardInterrupt): raise except Exception: import traceback fixture.close() 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, traceback.format_exc()))) return fixture.close() except: 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: 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 # If we found even one object in a fixture, we need to reset the # database sequences. if object_count > 0: sequence_sql = connection.ops.sequence_reset_sql( self.style, models) if sequence_sql: if verbosity > 1: print("Resetting sequences") for line in sequence_sql: cursor.execute(line) transaction.commit() transaction.leave_transaction_management() 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, fixture_count)) # Close the DB connection. This is required as a workaround for an # edge case in MySQL: if the same connection is used to # create tables, load data, and query, the query can return # incorrect results. See Django #7572, MySQL #37735. connection.close()
def import_groups(self): """imports askbot group profiles""" #redirects_file = self.open_unique_file('group_redirects') #1) we import auth groups for group in self.get_objects_for_model('auth.group'): #old_url = group.get_absolute_url() if group.name.startswith('_personal'): #we don't import these groups, but log #associations between old user ids and old personal #group ids, because we create the personal groups #anew and so need to have a connection #old personal group id --> old user id --> new user id # --> new pers. group id self.log_personal_group(group) continue old_group_id = group.id try: group = AuthGroup.objects.get(name=group.name) except AuthGroup.DoesNotExist: group.id = None group.save() transaction.commit() #new_url = group.get_absolute_url() #if old_url != new_url: # redirects_file.write('%s %s\n' % (old_url, new_url)) #we will later populate memberships only in these groups self.log_action_with_old_id(old_group_id, group) #redirects_file.close() #2) we import askbot group profiles only for groups for profile in self.get_objects_for_model('askbot.group'): auth_group = self.get_imported_object_by_old_id( AuthGroup, profile.group_ptr_id) if auth_group is None or auth_group.name.startswith('_personal'): continue #if profile for this group does not exist, then create new profile and save try: existing_profile = Group.objects.get( group_ptr__id=auth_group.id) self.copy_string_parameter(profile, existing_profile, 'logo_url') self.merge_words_parameter(profile, existing_profile, 'preapproved_emails') self.merge_words_parameter(profile, existing_profile, 'preapproved_email_domains') existing_profile.save() except Group.DoesNotExist: new_profile = Group.objects.create( name=auth_group.name, logo_url=profile.logo_url, preapproved_emails=profile.preapproved_emails, preapproved_email_domains=profile.preapproved_email_domains ) new_profile.save() transaction.commit()
def signin(request): """ Django View. Gets called when user wants to signin or create new account. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from booki.editor.models import BookiGroup from django.core.exceptions import ObjectDoesNotExist from django.contrib import auth if request.POST.get("ajax", "") == "1": ret = {"result": 0} if request.POST.get("method", "") == "register": def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 if _checkIfEmpty("email"): return 3 if _checkIfEmpty("password") or _checkIfEmpty("password2"): return 4 if _checkIfEmpty("fullname"): return 5 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: # if there was no errors import re def _doCheckValid(): # check if it is valid username # - from 2 to 20 characters long # - word, number, ., _, - mtch = re.match('^[\w\d\_\.\-]{2,20}$', request.POST.get("username", "").strip()) if not mtch: return 6 # check if it is valid email if not bool(email_re.match(request.POST["email"].strip())): return 7 if request.POST.get("password", "") != request.POST.get("password2", "").strip(): return 8 if len(request.POST.get("password", "").strip()) < 6: return 9 if len(request.POST.get("fullname", "").strip()) > 30: return 11 # check if this user exists try: u = auth.models.User.objects.get(username=request.POST.get("username", "")) return 10 except auth.models.User.DoesNotExist: pass return 0 ret["result"] = _doCheckValid() if ret["result"] == 0: ret["result"] = 1 try: user = auth.models.User.objects.create_user(username=request.POST["username"], email=request.POST["email"], password=request.POST["password"]) except IntegrityError: ret["result"] = 10 # this is not a good place to fire signal, but i need password for now # should create function createUser for future use import booki.account.signals booki.account.signals.account_created.send(sender = user, password = request.POST["password"]) user.first_name = request.POST["fullname"].strip() try: user.save() # groups for groupName in simplejson.loads(request.POST.get("groups")): if groupName.strip() != '': sid = transaction.savepoint() try: group = BookiGroup.objects.get(url_name=groupName) group.members.add(user) except: transaction.savepoint_rollback(sid) else: transaction.savepoint_commit(sid) user2 = auth.authenticate(username=request.POST["username"], password=request.POST["password"]) auth.login(request, user2) except: transaction.rollback() ret["result"] = 666 else: transaction.commit() if request.POST.get("method", "") == "signin": user = auth.authenticate(username=request.POST["username"], password=request.POST["password"]) if user: auth.login(request, user) ret["result"] = 1 else: try: usr = auth.models.User.objects.get(username=request.POST["username"]) # User does exist. Must be wrong password then ret["result"] = 3 except auth.models.User.DoesNotExist: # User does not exist ret["result"] = 2 transaction.commit() return HttpResponse(simplejson.dumps(ret), mimetype="text/json") redirect = request.GET.get('redirect', reverse('frontpage')) if request.GET.get('next', None): redirect = request.GET.get('next') joinGroups = [] for groupName in request.GET.getlist("group"): try: joinGroups.append(BookiGroup.objects.get(url_name=groupName)) except BookiGroup.DoesNotExist: pass try: return render_to_response('account/signin.html', {"request": request, 'redirect': redirect, 'joingroups': joinGroups}) except: transaction.rollback() finally: transaction.commit()
def forgotpassword(request): """ Django View. Gets called when user wants to change password he managed to forget. @type request: C{django.http.HttpRequest} @param request: Django Request """ from booki.utils.json_wrapper import simplejson from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.models import User if request.POST.get("ajax", "") == "1": ret = {"result": 0} usr = None if request.POST.get("method", "") == "forgot_password": def _checkIfEmpty(key): return request.POST.get(key, "").strip() == "" def _doChecksForEmpty(): if _checkIfEmpty("username"): return 2 return 0 ret["result"] = _doChecksForEmpty() if ret["result"] == 0: allOK = True try: usr = User.objects.get(username=request.POST.get("username", "")) except User.DoesNotExist: pass if not usr: try: usr = User.objects.get(email=request.POST.get("username", "")) except User.DoesNotExist: allOK = False if allOK: from booki.account import models as account_models from django.core.mail import send_mail def generateSecretCode(): import string from random import choice return ''.join([choice(string.letters + string.digits) for i in range(30)]) secretcode = generateSecretCode() account_models = account_models.UserPassword() account_models.user = usr account_models.remote_useragent = request.META.get('HTTP_USER_AGENT','') account_models.remote_addr = request.META.get('REMOTE_ADDR','') account_models.remote_host = request.META.get('REMOTE_HOST','') account_models.secretcode = secretcode try: account_models.save() except: transaction.rollback() else: transaction.commit() # body = render_to_string('account/password_reset_email.txt', dict(secretcode=secretcode)) send_mail(_('Reset password'), body, 'info@' + THIS_BOOKI_SERVER, [usr.email], fail_silently=False) else: ret["result"] = 3 return HttpResponse(simplejson.dumps(ret), mimetype="text/json") try: return render_to_response('account/forgot_password.html', {"request": request}) except: transaction.rollback() finally: transaction.commit()
def remote_join_group(request, message, groupid): group = models.BookiGroup.objects.get(url_name=groupid) group.members.add(request.user) transaction.commit() return {"result": True}
def user_settings(request, username): """ Django View. Edit user settings. Right now, this is just basics. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. @todo: Check if user exists. """ from django.contrib.auth.models import User from booki.editor import models try: user = User.objects.get(username=username) except User.DoesNotExist: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) if request.method == 'POST': settings_form = SettingsForm(request.POST, request.FILES) if settings_form.is_valid(): # this is very stupid and wrong # change the way it works # make utils for # - get image url # - get image path # - seperate storage for from django.core.files import File profile = user.get_profile() user.email = settings_form.cleaned_data['email'] user.first_name = settings_form.cleaned_data['firstname'] #user.last_name = settings_form.cleaned_data['lastname'] user.save() profile.description = settings_form.cleaned_data['description'] # this works for now, but this kind of processing must be done somewhere else! if settings_form.cleaned_data['image']: import tempfile import os # check this later fh, fname = tempfile.mkstemp(suffix='', prefix='profile') f = open(fname, 'wb') for chunk in settings_form.cleaned_data['image'].chunks(): f.write(chunk) f.close() import Image im = Image.open(fname) im.thumbnail((120, 120), Image.NEAREST) imageName = '%s.jpg' % fname im.save(imageName) profile.image.save('%s.jpg' % user.username, File(file(imageName))) os.unlink(fname) profile.save() endpoint_config = get_endpoint_or_none("@"+user.username).get_config() endpoint_config.notification_filter = settings_form.cleaned_data['notification_filter'] endpoint_config.save() transaction.commit() return HttpResponseRedirect(reverse("view_profile", args=[username])) else: settings_form = SettingsForm(initial = {'image': 'aaa', 'firstname': user.first_name, #'lastname': user.last_name, 'description': user.get_profile().description, 'email': user.email, 'notification_filter': get_endpoint_or_none("@"+user.username).notification_filter()}) try: return render_to_response('account/user_settings.html', {"request": request, "user": user, "settings_form": settings_form, }) except: transaction.rollback() finally: transaction.commit()
def execute_migrations(self, show_traceback=False): migrations = self._filter_down() if not len(migrations): sys.stdout.write("There are no migrations to apply.\n") created_models = set() try: for migration in migrations: migration_path = os.path.join(self.path, migration) fp = open(migration_path, "rb") lines = fp.readlines() fp.close() content = "".join(lines) if migration_path.endswith(".sql"): to_execute = "".join([ l for l in lines if not l.startswith("### New Model: ") ]) connection = connections[self.db] cursor = connection.cursor() sys.stdout.write("Executing %s... " % migration) try: cursor.execute(to_execute) cursor.close() except Exception: sys.stdout.write("failed\n") if show_traceback: traceback.print_exc() raise MigrationError() else: sys.stdout.write("success\n") for l in lines: if l.startswith("### New Model: "): created_models.add( get_model(*l.replace("### New Model: ", "").strip().split("."))) elif migration_path.endswith(".py"): sys.stdout.write("Executing %s... " % migration) module = {} execfile(migration_path, {}, module) if "migrate" in module and callable(module["migrate"]): try: module["migrate"]() except Exception: sys.stdout.write("failed\n") if show_traceback: traceback.print_exc() raise MigrationError() else: sys.stdout.write("success\n") Migration.objects.create( migration_label=migration, content=content, scm_version=self._get_rev(migration_path)) sys.stdout.write("Emitting post sync signal.\n") emit_post_sync_signal(created_models, self.verbosity, self.interactive, self.db) sys.stdout.write("Running loaddata for initial_data fixtures.\n") call_command("loaddata", "initial_data", verbosity=self.verbosity, database=self.db) except Exception: transaction.rollback(using=self.db) sys.stdout.write("Rolled back all migrations.\n") raise else: transaction.commit(using=self.db)
try: # Perform the SQL # execute_sql(cursor, sql) for e in do_actions: e.doAction() # Now update the evolution table version = Version(signature=current_signature) version.save(**using_args) for evolution in new_evolutions: evolution.version = version evolution.save(**using_args) transaction.commit(**using_args) except Exception, ex: transaction.rollback(**using_args) raise CommandError('Error applying evolution: %s' % str(ex)) transaction.leave_transaction_management(**using_args) if verbosity > 0: print 'Evolution successful.' else: print self.style.ERROR('Evolution cancelled.') elif not compile_sql: if verbosity > 0: if simulated: print "Trial evolution successful."
def revert_applied_patch(self, num, filename): exec_sql_modify('delete from db_patch_log where num = %s', [num]) transaction.commit()
def handle_post(self, request, user, *args, **kwargs): '''Handles GET requests get an IP4 available for vip_request by evip_id. URL: ip/availableip6/vip/id_evip/ ''' self.log.info('Get an IP4 available for vip_request') try: # User permission if not has_perm(user, AdminPermission.IPS, AdminPermission.WRITE_OPERATION): self.log.error(u'User does not have permission to perform the operation.') return self.not_authorized() # Load XML data xml_map, attrs_map = loads(request.raw_post_data) # XML data format networkapi_map = xml_map.get('networkapi') ip_map = networkapi_map.get('ip_map') # Get XML data id_evip = ip_map.get('id_evip') name = ip_map.get('name') if not is_valid_int_greater_zero_param(id_evip): self.log.error(u'Parameter id_evip is invalid. Value: %s.', id_evip) raise InvalidValueError(None, 'id_evip', id_evip) # Business Rules evip = EnvironmentVip.get_by_pk(id_evip) with distributedlock(LOCK_GET_IPV4_AVAILABLE % id_evip): ipv4 = Ip() len_network = len(evip.networkipv4_set.all()) raise_not_found_balanceamento = False if (len_network <= 0): raise NetworkNotInEvip(None, 'Não há rede no ambiente vip fornecido') cont_network = 0 cont_balanceador_not_found = 0 for net in evip.networkipv4_set.all(): balanceador_found_flag = False cont_network = cont_network + 1 list_ips_equips = list() try: ip_available = ipv4.get_available_ip(net.id) ip_new = Ip() ip_available = ip_available.exploded ip_available = ip_available.split(".") ip_new.oct1 = ip_available[0] ip_new.oct2 = ip_available[1] ip_new.oct3 = ip_available[2] ip_new.oct4 = ip_available[3] ip_new.descricao = name for env_equipment in net.vlan.ambiente.equipamentoambiente_set.all(): equipment = env_equipment.equipamento if equipment.tipo_equipamento == TipoEquipamento.get_tipo_balanceador(): if equipment.id not in list_ips_equips: list_ips_equips.append(equipment.id) if ip_new.id is None: ip_new.save_ipv4(equipment.id, user, net) else: new_ip_equip = IpEquipamento() new_ip_equip.ip = ip_new new_ip_equip.equipamento = equipment new_ip_equip.save() balanceador_found_flag = True if not balanceador_found_flag: cont_balanceador_not_found = cont_balanceador_not_found + 1 else: break if cont_balanceador_not_found == len_network: raise_not_found_balanceamento = True raise IpNotAvailableError(None, "Não há ipv4 disponivel para as redes associdas com o Ambiente " "Vip: %s - %s - %s, pois não existe equipamentos do Tipo " "Balanceador nessas redes." % (evip.finalidade_txt, evip.cliente_txt, evip.ambiente_p44_txt)) except (IpNotAvailableError, IpRangeAlreadyAssociation), e: cont_balanceador_not_found = cont_balanceador_not_found + 1 if raise_not_found_balanceamento: raise IpNotAvailableError(None, e.message) elif len_network == cont_network: raise IpNotAvailableError(None, "Não há ipv4 disponivel para as redes associdas com o Ambiente " "Vip: %s - %s - %s" % (evip.finalidade_txt, evip.cliente_txt, evip.ambiente_p44_txt)) transaction.commit() return self.response(dumps_networkapi({"ip": model_to_dict(ip_new)})) except NetworkNotInEvip, e: return self.response_error(321, 'ipv4')
def commit(using=None): using = select_db("db") if using is None else using transaction.commit(using=using)
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 manually_managed(self): r = Reporter(first_name="Dirk", last_name="Gently") r.save() transaction.commit()
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()
def _update_subtask_status(entry_id, current_task_id, new_subtask_status): """ Update the status of the subtask in the parent InstructorTask object tracking its progress. Uses select_for_update to lock the InstructorTask object while it is being updated. The operation is surrounded by a try/except/else that permit the manual transaction to be committed on completion, or rolled back on error. The InstructorTask's "task_output" field is updated. This is a JSON-serialized dict. Accumulates values for 'attempted', 'succeeded', 'failed', 'skipped' from `new_subtask_status` into the corresponding values in the InstructorTask's task_output. Also updates the 'duration_ms' value with the current interval since the original InstructorTask started. Note that this value is only approximate, since the subtask may be running on a different server than the original task, so is subject to clock skew. The InstructorTask's "subtasks" field is also updated. This is also a JSON-serialized dict. Keys include 'total', 'succeeded', 'retried', 'failed', which are counters for the number of subtasks. 'Total' is expected to have been set at the time the subtasks were created. The other three counters are incremented depending on the value of `status`. Once the counters for 'succeeded' and 'failed' match the 'total', the subtasks are done and the InstructorTask's "status" is changed to SUCCESS. The "subtasks" field also contains a 'status' key, that contains a dict that stores status information for each subtask. At the moment, the value for each subtask (keyed by its task_id) is the value of the SubtaskStatus.to_dict(), but could be expanded in future to store information about failure messages, progress made, etc. """ TASK_LOG.info( "Preparing to update status for subtask %s for instructor task %d with status %s", current_task_id, entry_id, new_subtask_status) try: entry = InstructorTask.objects.select_for_update().get(pk=entry_id) subtask_dict = json.loads(entry.subtasks) subtask_status_info = subtask_dict['status'] if current_task_id not in subtask_status_info: # unexpected error -- raise an exception format_str = "Unexpected task_id '{}': unable to update status for subtask of instructor task '{}'" msg = format_str.format(current_task_id, entry_id) TASK_LOG.warning(msg) raise ValueError(msg) # Update status: subtask_status_info[current_task_id] = new_subtask_status.to_dict() # Update the parent task progress. # Set the estimate of duration, but only if it # increases. Clock skew between time() returned by different machines # may result in non-monotonic values for duration. task_progress = json.loads(entry.task_output) start_time = task_progress['start_time'] prev_duration = task_progress['duration_ms'] new_duration = int((time() - start_time) * 1000) task_progress['duration_ms'] = max(prev_duration, new_duration) # Update counts only when subtask is done. # In future, we can make this more responsive by updating status # between retries, by comparing counts that change from previous # retry. new_state = new_subtask_status.state if new_subtask_status is not None and new_state in READY_STATES: for statname in ['attempted', 'succeeded', 'failed', 'skipped']: task_progress[statname] += getattr(new_subtask_status, statname) # Figure out if we're actually done (i.e. this is the last task to complete). # This is easier if we just maintain a counter, rather than scanning the # entire new_subtask_status dict. if new_state == SUCCESS: subtask_dict['succeeded'] += 1 elif new_state in READY_STATES: subtask_dict['failed'] += 1 num_remaining = subtask_dict['total'] - subtask_dict[ 'succeeded'] - subtask_dict['failed'] # If we're done with the last task, update the parent status to indicate that. # At present, we mark the task as having succeeded. In future, we should see # if there was a catastrophic failure that occurred, and figure out how to # report that here. if num_remaining <= 0: entry.task_state = SUCCESS entry.subtasks = json.dumps(subtask_dict) entry.task_output = InstructorTask.create_output_for_success( task_progress) TASK_LOG.debug("about to save....") entry.save() TASK_LOG.info( "Task output updated to %s for subtask %s of instructor task %d", entry.task_output, current_task_id, entry_id) except Exception: TASK_LOG.exception("Unexpected error while updating InstructorTask.") transaction.rollback() dog_stats_api.increment('instructor_task.subtask.update_exception') raise else: TASK_LOG.debug("about to commit....") transaction.commit()
def handle(self, *fixture_labels, **options): using = options.get('database') connection = connections[using] if not len(fixture_labels): raise CommandError( "No database fixture specified. Please provide the path of at " "least one fixture in the command line.") verbosity = int(options.get('verbosity')) show_traceback = options.get('traceback') # 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 loaded_object_count = 0 fixture_object_count = 0 models = set() humanize = lambda dirname: "'%s'" % dirname if dirname else '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(using=using) transaction.enter_transaction_management(using=using) transaction.managed(True, using=using) 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: open, 'gz': gzip.GzipFile, 'zip': SingleZipReader } if has_bz2: compression_types['bz2'] = bz2.BZ2File app_module_paths = [] for app in get_apps(): if hasattr(app, '__path__'): # It's a 'models/' subpackage for path in app.__path__: app_module_paths.append(path) else: # It's a models.py module app_module_paths.append(app.__file__) app_fixtures = [ os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths ] try: with connection.constraint_checks_disabled(): 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 >= 2: self.stdout.write("Loading '%s' fixtures..." % fixture_name) else: raise CommandError( "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 >= 2: self.stdout.write("Checking %s for fixtures..." % humanize(fixture_dir)) label_found = False for combo in product([using, None], formats, compression_formats): database, format, compression_format = combo file_name = '.'.join(p for p in [ fixture_name, database, format, compression_format ] if p) if verbosity >= 3: self.stdout.write("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') except IOError: if verbosity >= 2: self.stdout.write("No %s fixture '%s' in %s." % \ (format, fixture_name, humanize(fixture_dir))) else: try: if label_found: raise CommandError( "Multiple fixtures named '%s' in %s. Aborting." % (fixture_name, humanize(fixture_dir))) fixture_count += 1 objects_in_fixture = 0 loaded_objects_in_fixture = 0 if verbosity >= 2: self.stdout.write("Installing %s fixture '%s' from %s." % \ (format, fixture_name, humanize(fixture_dir))) objects = serializers.deserialize( format, fixture, using=using) for obj in objects: objects_in_fixture += 1 if router.allow_syncdb( using, obj.object.__class__): loaded_objects_in_fixture += 1 models.add(obj.object.__class__) try: obj.save(using=using) except (DatabaseError, IntegrityError) as e: e.args = ( "Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % { 'app_label': obj.object._meta. app_label, 'object_name': obj.object._meta. object_name, 'pk': obj.object.pk, 'error_msg': force_unicode(e) }, ) raise loaded_object_count += loaded_objects_in_fixture fixture_object_count += objects_in_fixture label_found = True finally: fixture.close() # If the fixture we loaded contains 0 objects, assume that an # error was encountered during fixture loading. if objects_in_fixture == 0: raise CommandError( "No fixture data found for '%s'. (File format may be invalid.)" % (fixture_name)) # Since we disabled constraint checks, we must manually check for # any invalid keys that might have been added table_names = [model._meta.db_table for model in models] connection.check_constraints(table_names=table_names) except (SystemExit, KeyboardInterrupt): raise except Exception as e: if commit: transaction.rollback(using=using) transaction.leave_transaction_management(using=using) if not isinstance(e, CommandError): e.args = ("Problem installing fixture '%s': %s" % (full_path, e), ) raise # If we found even one object in a fixture, we need to reset the # database sequences. if loaded_object_count > 0: sequence_sql = connection.ops.sequence_reset_sql( no_style(), models) if sequence_sql: if verbosity >= 2: self.stdout.write("Resetting sequences\n") for line in sequence_sql: cursor.execute(line) if commit: transaction.commit(using=using) transaction.leave_transaction_management(using=using) if verbosity >= 1: if fixture_object_count == loaded_object_count: self.stdout.write("Installed %d object(s) from %d fixture(s)" % (loaded_object_count, fixture_count)) else: self.stdout.write( "Installed %d object(s) (of %d) from %d fixture(s)" % (loaded_object_count, fixture_object_count, fixture_count)) # Close the DB connection. This is required as a workaround for an # edge case in MySQL: if the same connection is used to # create tables, load data, and query, the query can return # incorrect results. See Django #7572, MySQL #37735. if commit: connection.close()
def create_group(request, username): """ Django View. Show content for Create Group dialog and creates group. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. """ from django.contrib.auth.models import User try: user = User.objects.get(username=username) except User.DoesNotExist: try: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) except: transaction.rollback() finally: transaction.commit() if not request.user.is_authenticated(): try: return pages.ErrorPage(request, "errors/no_permissions.html") except: transaction.rollback() finally: transaction.commit() from booki.utils.book import checkGroupAvailability, createBookiGroup from booki.editor import models if request.GET.get("q", "") == "check": from booki.utils.json_wrapper import json data = {"available": checkGroupAvailability(request.GET.get('groupname', '').strip())} try: return HttpResponse(json.dumps(data), "text/plain") except: transaction.rollback() finally: transaction.commit() if request.GET.get("q", "") == "create": from booki.utils.json_wrapper import json groupName = request.GET.get('name', '').strip() groupDescription = request.GET.get('description', '').strip() groupCreated = False if checkGroupAvailability(groupName): try: group = createBookiGroup(groupName, groupDescription, request.user) group.members.add(request.user) groupCreated = True except BookiGroupExist: groupCreated = False transaction.rollback() else: transaction.commit() data = {'created': groupCreated} try: return HttpResponse(json.dumps(data), "text/plain") except: transaction.rollback() finally: transaction.commit() try: return render_to_response('account/create_group.html', {"request": request, "user": user}) except: transaction.rollback() finally: transaction.commit()
def handle(self, *args, **options): if not options['email_id']: raise CommandError(u"Vous devez specifier le ID du courriel a " u"envoyer avec l'argument --id") try: email = espace_membre.Courriel.objects.get(id=options['email_id']) except espace_membre.Courriel.DoesNotExist: raise CommandError("Le courriel (id=%s) n'existe pas." % options['email_id']) site = Site.objects.get(id=1) try: etablissements_queryset = espace_membre.Etablissement.objects\ .filter(actif=True, membre=True) if options['mode_envoi'] == 'rappel': etablissements_queryset = etablissements_queryset\ .exclude(modification__validation_etablissement=True) for etablissement in etablissements_queryset: # de préférence on envoie au PHA courriel_responsable_pha = get_courriel_responsable( etablissement, espace_membre.RESPONSABLE_ETABLISSEMENT) etablissement_courriel = etablissement.courriel courriel_responsable_com = get_courriel_responsable( etablissement, espace_membre.RESPONSABLE_COMMUNICATION) to = (courriel_responsable_pha or etablissement_courriel or courriel_responsable_com) if not to: continue # on vérifie qu'on n'a pas déjà envoyé ce courriel à # cet établissement et à cette adresse if espace_membre.CourrielLog.objects.filter( etablissement=etablissement, envoye=True, courriel=email, adresse_courriel=to).count() > 0: continue tokens = espace_membre.Acces.objects \ .filter(etablissement=etablissement, active=True) url = 'https://{}{}'.format( site.domain, reverse('espace_membre_connexion', kwargs={'token': tokens[0].token})) modele_corps = Template(email.contenu) contexte_corps = Context({ "nom_etablissement": etablissement.nom, "lien": url # '<a href="%s">%s</a>' % (url, url) }) corps = modele_corps.render(contexte_corps) courriel_relations_internationales = get_courriel_responsable( etablissement, espace_membre.RESPONSABLE_RELATIONS_INTERNATIONALES) cc = get_cc(to, courriel_responsable_com, courriel_relations_internationales) message = EmailMessage( email.sujet, corps, # adresse de retour settings.ESPACE_MEMBRE_SENDER, # adresse du destinataire [to], cc=cc, # selon les conseils de google headers={'precedence': 'bulk'}) try: # Attention en DEV, devrait simplement écrire le courriel # dans la console, cf. paramètre EMAIL_BACKEND dans conf.py # En PROD, supprimer EMAIL_BACKEND (ce qui fera retomber sur # le défaut qui est d'envoyer par SMTP). Même chose en # TEST, mais attention car les adresses qui sont dans la # base seront utilisées: modifier les données pour y mettre # des adresses de test plutôt que les vraies message.content_subtype = "html" message.send() log = espace_membre.CourrielLog() log.etablissement_id = etablissement.id log.courriel = email log.adresse_courriel = to log.envoye_le = datetime.datetime.today() log.envoye = True log.save() transaction.commit() time.sleep(2) except smtplib.SMTPException as e: err_message = u"Erreur lors de l'envoi pour etablissement "\ u"ID: {}}\n" self.stdout.write(err_message.format(etablissement.id)) self.stdout.write(e.__str__()) except: transaction.rollback() raise # nécessaire dans le cas où rien n'est envoyé, à cause du décorateur # commit_manually transaction.commit()
def delete(self, request, database_name, format=None): env = get_url_env(request) data = request.DATA LOG.debug("Request DATA {}".format(data)) response = check_database_status(database_name, env) if type(response) != Database: return response database = response unit_network = check_acl_service_and_get_unit_network(database, data) if type(unit_network) == Response: return unit_network transaction.set_autocommit(False) try: db_bind = DatabaseBind.objects.get( database=database, bind_address=unit_network ) database_bind = DatabaseBind.objects.select_for_update().filter( id=db_bind.id )[0] if database_bind.bind_status == CREATING: raise Exception( "Bind for {} has not yet been created!".format( unit_network ) ) if database_bind.bind_status != DESTROYING: if database_bind.binds_requested > 0: database_bind.binds_requested -= 1 if database_bind.binds_requested == 0: database_bind.bind_status = DESTROYING database_bind.save() except (IndexError, ObjectDoesNotExist) as e: msg = "DatabaseBind does not exist" return log_and_response( msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR ) except Exception as e: msg = "Bind for {} has not yet been created!".format(unit_network) return log_and_response( msg=msg, e=e, http_status=status.HTTP_500_INTERNAL_SERVER_ERROR ) finally: LOG.debug("Finishing transaction!") transaction.commit() transaction.set_autocommit(True) if database_bind.binds_requested == 0: unbind_address_on_database.delay( database_bind=database_bind, user=request.user ) return Response(status.HTTP_204_NO_CONTENT)
def import_book(request, username): """ Django View. Book Import dialog. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. """ from django.contrib.auth.models import User try: user = User.objects.get(username=username) except User.DoesNotExist: try: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) except: transaction.rollback() finally: transaction.commit() if not request.user.is_authenticated(): try: return pages.ErrorPage(request, "errors/no_permissions.html") except: transaction.rollback() finally: transaction.commit() from booki.utils.book import checkGroupAvailability, createBookiGroup from booki.editor import models if request.GET.get("q", "") == "check": from booki.utils.json_wrapper import json data = {"available": checkGroupAvailability(request.GET.get('groupname', '').strip())} try: return HttpResponse(json.dumps(data), "text/plain") except: transaction.rollback() finally: transaction.commit() book_visible = config.getConfiguration('CREATE_BOOK_VISIBLE') admin_import = config.getConfiguration('ADMIN_IMPORT_BOOKS') if request.user.is_superuser: admin_import = False if request.GET.get("q", "") == "import" and admin_import == False: from booki.utils.json_wrapper import json data = {} try: bookid = request.GET.get('source', '') importType = request.GET.get('importtype', '') renameTitle = request.GET.get('title', '') extraOptions = {} if renameTitle: extraOptions['book_title'] = renameTitle if request.GET.get('hidden', '') != '': extraOptions['hidden'] = True importSources = { 'flossmanuals': (TWIKI_GATEWAY_URL, "en.flossmanuals.net"), "archive": (ESPRI_URL, "archive.org"), "wikibooks": (ESPRI_URL, "wikibooks"), "epub": (ESPRI_URL, "url"), } if importType == "booki": bookid = bookid.rstrip('/') booki_url, book_url_title = bookid.rsplit("/", 1) base_url = "%s/export/%s/export" % (booki_url, book_url_title) source = "booki" else: base_url, source = importSources[importType] book = common.importBookFromUrl2(user, base_url, args=dict(source=source, book=bookid), **extraOptions ) except Exception: data['imported'] = False transaction.rollback() else: transaction.commit() data['imported'] = True from django.core.urlresolvers import reverse data['info_url'] = reverse('book_info', args=[book.url_title]) try: return HttpResponse(json.dumps(data), "text/plain") except: transaction.rollback() finally: transaction.commit() try: return render_to_response('account/import_book.html', {"request": request, "book_visible": book_visible, "admin_import": admin_import, "user": user}) except: transaction.rollback() finally: transaction.commit()
def new_user(request): org = get_organization(request) add_user_success = True manager = get_database_manager(request.user) if request.method == 'GET': profile_form = UserProfileForm() return render_to_response( "accountmanagement/account/add_new_user.html", { 'profile_form': profile_form, 'is_pro_sms': org.is_pro_sms, 'current_lang': get_language() }, context_instance=(RequestContext(request))) if request.method == 'POST': post_parameters = request.POST org = get_organization(request) form = UserProfileForm(organization=org, data=request.POST) errors = {} if form.is_valid(): username = post_parameters['username'].lower() role = post_parameters['role'] if not form.errors: with transaction.commit_manually(): sid = transaction.savepoint() try: user = User.objects.create_user( username, username, 'test123') user.first_name = post_parameters['full_name'] group = Group.objects.filter(name=role) user.groups.add(group[0]) user.save() mobile_number = post_parameters['mobile_phone'] ngo_user_profile = NGOUserProfile( user=user, title=post_parameters['title'], mobile_phone=mobile_number, org_id=org.org_id) ngo_user_profile.reporter_id = make_user_as_a_datasender( manager=manager, organization=org, current_user_name=user.get_full_name(), mobile_number=mobile_number, email=username) ngo_user_profile.save() handle_feature_toggle_impact_for_new_user( ngo_user_profile) reset_form = PasswordResetForm({"email": username}) name = post_parameters["full_name"] if role == 'Extended Users': link_user_to_all_projects.delay( ngo_user_profile.user_id) UserActivityLog().log(request, action=ADDED_USER, detail=activity_log_detail( name, friendly_name(role))) elif role in ['Project Managers', "No Delete PM"]: selected_questionnaires = post_parameters.getlist( 'selected_questionnaires[]') selected_questionnaire_names = post_parameters.getlist( 'selected_questionnaire_names[]') if selected_questionnaires is not None: link_user_to_some_projects.delay( ngo_user_profile.user_id, *tuple(selected_questionnaires)) UserActivityLog().log( request, action=ADDED_USER, detail=activity_log_detail( name, friendly_name(role), selected_questionnaire_names)) transaction.savepoint_commit(sid) except Exception as e: transaction.savepoint_rollback(sid) datawinners_logger.exception(e.message) add_user_success = False transaction.commit( ) #Mandatory for manually managed transaction blocks. Here it won't save anything if add_user_success and reset_form.is_valid(): send_email_to_data_sender(reset_form.users_cache[0], request.LANGUAGE_CODE, request=request, type="created_user", organization=org) form = UserProfileForm() else: add_user_success = False else: errors = form.errors add_user_success = False if len(request.user.groups.filter(name__in=["NGO Admins"])) < 1: current_user_type = "Administrator" else: current_user_type = "Account-Administrator" data = { "add_user_success": add_user_success, "errors": errors, "current_user": current_user_type } return HttpResponse(json.dumps(data), mimetype="application/json", status=201)
def create_book(request, username): """ Django View. Show content for Create Book dialog and creates book. @type request: C{django.http.HttpRequest} @param request: Django Request @type username: C{string} @param username: Username. """ from django.contrib.auth.models import User try: user = User.objects.get(username=username) except User.DoesNotExist: try: return pages.ErrorPage(request, "errors/user_does_not_exist.html", {"username": username}) except: transaction.rollback() finally: transaction.commit() if not request.user.is_authenticated(): try: return pages.ErrorPage(request, "errors/no_permissions.html") except: transaction.rollback() finally: transaction.commit() from booki.utils.book import checkBookAvailability, createBook from booki.editor import models book_visible = config.getConfiguration('CREATE_BOOK_VISIBLE') book_license = config.getConfiguration('CREATE_BOOK_LICENSE') admin_create = config.getConfiguration('ADMIN_CREATE_BOOKS') if request.user.is_superuser: admin_create = False if request.GET.get("q", "") == "check": from booki.utils.json_wrapper import json data = {"available": checkBookAvailability(request.GET.get('bookname', '').strip())} try: return HttpResponse(json.dumps(data), "text/plain") except: transaction.rollback() finally: transaction.commit() if request.method == 'POST' and admin_create == False: book = None try: # hidden on # description # license # title # cover book = createBook(request.user, request.POST.get('title')) lic = models.License.objects.get(abbrevation=request.POST.get('license')) book.license = lic book.description = request.POST.get('description', '') if request.POST.get("hidden", "") == "on": is_hidden = True else: is_hidden = False book.hidden = is_hidden from django.core.files import File if request.FILES.has_key('cover'): # TODO: Show some kind of error message to the user from booki.utils import misc import os try: fh, fname = misc.saveUploadedAsFile(request.FILES['cover']) book.setCover(fname) os.unlink(fname) except: pass book.save() except: transaction.rollback() else: transaction.commit() try: return render_to_response('account/create_book_redirect.html', {"request": request, "user": user, "book": book}) except: transaction.rollback() finally: transaction.commit() from booki.editor.models import License licenses = License.objects.all().order_by('name') try: return render_to_response('account/create_book.html', {"request": request, "book_visible": book_visible, "book_license": book_license, "admin_create": admin_create, "licenses": licenses, "user": user}) except: transaction.rollback() finally: transaction.commit()
def profile(request, username): user = User.objects.get(username=username) cursor.execute( "select remark_id, remark_name from remark_author where author_name = %s", [username]) remark_list = dictfetchall(cursor) cursor.execute( "select book_id, book_name from favorite_book where owner_name = %s", [username]) favorite_list = dictfetchall(cursor) cursor.execute( "select article_name, article_id from article_author where author_name = %s", [username]) article_list = dictfetchall(cursor) cursor.execute( "select group_name, group_id from join_group where member_name = %s", [username]) group_list = dictfetchall(cursor) cursor.execute( "select group_id, group_name from group_builder where builder_name = %s", [username]) manager_list = dictfetchall(cursor) if request.method == 'POST': if request.POST.has_key('logout'): logout(request) return HttpResponseRedirect('/accounts/login/') elif request.POST.has_key('change_password'): request.user.set_password(request.POST['password']) request.user.save() elif request.POST.has_key('create_group'): g = Group(GBuilder=request.user, GName=request.POST["group"], GBuildDate=time.strftime('%Y-%m-%d', time.localtime(time.time()))) g.save() cursor.execute( "select group_id, group_name from group_builder where builder_name = %s", [username]) manager_list = dictfetchall(cursor) elif request.POST.has_key('logout'): logout(request) return HttpResponseRedirect('/accounts/login/') else: for i in remark_list: if request.POST.has_key(i['remark_name']): Remark.objects.filter(RID=i['remark_id']).delete() cursor.execute( "select remark_id, remark_name from remark_author where author_name = %s", [username]) remark_list = dictfetchall(cursor) break for i in article_list: if request.POST.has_key(i['article_name']): Article.objects.filter(ATID=i['article_id']).delete() cursor.execute( "select article_name, article_id from article_author where author_name = %s", [username]) article_list = dictfetchall(cursor) break for i in favorite_list: if request.POST.has_key(i['book_id']): cursor.execute( "delete from add_book where FID_id = (select FID from favorite where UID_id = %s) and BID_id = BID", [request.user.id]) transaction.commit() cursor.execute( "select book_id, book_name from favorite_book where owner_name = %s", [username]) favorite_list = dictfetchall(cursor) break for i in group_list: if request.POST.has_key('group' + str(i['group_id'])): Join.objects.filter(UID=request.user, GID=Group.objects.get( GID=int(i['group_id']))).delete() Article.objects.filter( UID=request.user, GID=Group.objects.get( GID=int(i['group_id']))).delete() cursor.execute( "select group_name, group_id from join_group where member_name = %s", [username]) group_list = dictfetchall(cursor) cursor.execute( "select article_name, article_id from article_author where author_name = %s", [username]) article_list = dictfetchall(cursor) break for i in manager_list: if request.POST.has_key('manager' + str(i['group_id'])): Article.objects.filter(GID=Group.objects.get( GID=int(i['group_id']))).delete() Join.objects.filter(GID=Group.objects.get( GID=int(i['group_id']))).delete() Group.objects.filter(GID=int(i['group_id'])).delete() cursor.execute( "select group_id, group_name from group_builder where builder_name = %s", [username]) manager_list = dictfetchall(cursor) cursor.execute( "select article_name article_id from article_author where author_name = %s", [username]) article_list = dictfetchall(cursor) break return render_to_response('profile.html', { 'user': user, 'remark_list': remark_list, 'article_list': article_list, 'favorite_list': favorite_list, 'group_list': group_list, 'manager_list': manager_list }, context_instance=RequestContext(request))
class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + (make_option( '--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' 'Defaults to the "default" database.'), ) help = "Execute all of the SQL statments found in the migrations/sql/migrate.sql file." @transaction.commit_manually def handle_noargs(self, **options): verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') show_traceback = options.get('traceback', False) self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: import_module('.management', app_name) except ImportError, exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith( 'No module named') or 'management' not in msg: raise db = options.get('database', DEFAULT_DB_ALIAS) connection = connections[db] cursor = connection.cursor() transaction.commit_unless_managed(using=db) custom_sql = custom_migrate_sql(self.style, connection) if custom_sql: if verbosity >= 1: print "Running SQL migrations in migrations/sql/migrate.sql" try: for sql in custom_sql: cursor.execute(sql.replace("%", "%%")) except Exception, e: sys.stderr.write( "Failed to run custom SQL in migrations/sql/migrate.sql: %s" % e) if show_traceback: import traceback traceback.print_exc() transaction.rollback(using=db) else: transaction.commit(using=db)
def handle(self, **options): # Pick up the options if 'database' in options: database = options['database'] or DEFAULT_DB_ALIAS else: database = DEFAULT_DB_ALIAS if not database in settings.DATABASES: raise CommandError("No database settings known for '%s'" % database ) if 'user' in options and options['user']: try: user = User.objects.all().using(database).get(username=options['user']) except: raise CommandError("User '%s' not found" % options['user'] ) else: user = None now = datetime.now() transaction.enter_transaction_management(using=database) task = None try: # Initialize the task if 'task' in options and options['task']: try: task = Task.objects.all().using(database).get(pk=options['task']) except: raise CommandError("Task identifier not found") if task.started or task.finished or task.status != "Waiting" or task.name != 'generate plan': raise CommandError("Invalid task identifier") task.status = '0%' task.started = now else: task = Task(name='generate plan', submitted=now, started=now, status='0%', user=user) # Validate options if 'constraint' in options: constraint = int(options['constraint']) if constraint < 0 or constraint > 15: raise ValueError("Invalid constraint: %s" % options['constraint']) else: constraint = 15 if 'plantype' in options: plantype = int(options['plantype']) if plantype < 1 or plantype > 2: raise ValueError("Invalid plan type: %s" % options['plantype']) else: plantype = 1 if options['env']: task.arguments = "--constraint=%d --plantype=%d --env=%s" % (constraint, plantype, options['env']) for i in options['env'].split(','): j = i.split('=') if len(j) == 1: os.environ[j[0]] = '1' else: os.environ[j[0]] = j[1] else: task.arguments = "--constraint=%d --plantype=%d" % (constraint, plantype) # Log task task.save(using=database) transaction.commit(using=database) # Locate commands.py cmd = None for app in settings.INSTALLED_APPS: mod = import_module(app) if os.path.exists(os.path.join(os.path.dirname(mod.__file__),'commands.py')): cmd = os.path.join(os.path.dirname(mod.__file__),'commands.py') break if not cmd: raise Exception("Can't locate commands.py") # Execute os.environ['FREPPLE_PLANTYPE'] = str(plantype) os.environ['FREPPLE_CONSTRAINT'] = str(constraint) os.environ['FREPPLE_TASKID'] = str(task.id) os.environ['FREPPLE_DATABASE'] = database os.environ['PATH'] = settings.FREPPLE_HOME + os.pathsep + os.environ['PATH'] + os.pathsep + settings.FREPPLE_APP if os.path.isfile(os.path.join(settings.FREPPLE_HOME,'libfrepple.so')): os.environ['LD_LIBRARY_PATH'] = settings.FREPPLE_HOME if 'DJANGO_SETTINGS_MODULE' not in os.environ: os.environ['DJANGO_SETTINGS_MODULE'] = 'freppledb.settings' if os.path.exists(os.path.join(settings.FREPPLE_HOME,'python27.zip')): # For the py2exe executable os.environ['PYTHONPATH'] = os.path.join(settings.FREPPLE_HOME,'python27.zip') + os.pathsep + os.path.normpath(settings.FREPPLE_APP) else: # Other executables os.environ['PYTHONPATH'] = os.path.normpath(settings.FREPPLE_APP) ret = os.system('frepple "%s"' % cmd.replace('\\','\\\\')) if ret != 0 and ret != 2: # Return code 0 is a successful run # Return code is 2 is a run cancelled by a user. That's shown in the status field. raise Exception('Failed with exit code %d' % ret) # Task update task.status = 'Done' task.finished = datetime.now() except Exception as e: if task: task.status = 'Failed' task.message = '%s' % e task.finished = datetime.now() raise e finally: if task: task.save(using=database) try: transaction.commit(using=database) except: pass transaction.leave_transaction_management(using=database)
def import_season_scorecards(season, csv_file, user, logger): from django.db import transaction transaction.commit() scorecards = [] rows = [] reader = csv.reader(open(csv_file, 'rU')) for row in reader: rows.append(row) try: # build our closures closures = [] for col in rows[0]: closures.append(get_closure(season, col)) # add each row for row in rows[1:]: context = dict(entries=dict()) for (index, value) in enumerate(row): closure = closures[index] if closure: closure(season, context, value) # shortcut if there's no wetmill if not context['wetmill']: break # only create a scorecard if there is a wetmill name if context['wetmill']: logger.write("Processing %s scorecard.\n" % context['wetmill']) # delete any previous scorecard Scorecard.objects.filter(season=season, wetmill=context['wetmill']).delete() scorecard = Scorecard.objects.create( season=season, wetmill=context['wetmill'], auditor=context['auditor'], client_id=context['client_id'], audit_date=context['audit_date'], created_by=user, modified_by=user) # add each entry entries = context['entries'] for standard in entries.keys(): value = entries[standard] scorecard.add_entry(standard, value, user) # try to finalize the scorecard scorecard.finalize() scorecard.save() scorecards.append(scorecard) except Exception as e: import sys, traceback exc_type, exc_value, exc_traceback = sys.exc_info() if logger: logger.write("\nError Details:\n\n") logger.write("".join( traceback.format_exception(exc_type, exc_value, exc_traceback))) # if an errors occurs, roll back any part of our import transaction.rollback() raise e return scorecards