def with_rates(self, event): from django.db import connection cursor = connection.cursor() cursor.execute( """ SELECT n.id, n.film_id, n.person_id, n.type, n.oscar_type, coalesce(avg(r.rating),0) as avg,count(r.rating) as count FROM event_nominated n LEFT JOIN core_rating r ON r.rating is not null and n.type = r.type and n.film_id = r.film_id and (r.actor_id is null and n.person_id is null or r.actor_id=n.person_id) WHERE n.event_id = %d GROUP BY n.id, n.film_id, n.person_id, n.type, n.oscar_type ORDER BY n.oscar_type,avg desc """ % event.pk ) result_list = [] for row in cursor.fetchall(): p = self.model(id=row[0], film_id=row[1], person_id=row[2], type=row[3], oscar_type=row[4]) p.avg_rating = "%3.1f" % (row[5] or 0) p.count = row[6] result_list.append(p) # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() return result_list
def scrape(self, save=True, verbose=True): counts = { 'saves': 0, 'error': 0, } data = [] for title in self: try: data = self.api.search(title) if data: for item in data: if save: movie = self.movie_with_json(item) counts['saves'] += 1 if verbose: print movie else: data.append(item) except Exception as e: connection._rollback() # PostgreSQL rollback mechanism counts['error'] += 1 if verbose: print "Exception searching for \"%s\": %s" % (title, str(e)) continue if not save: with open('fixtures/title_list.json', 'w') as out: json.dump(data, out) print "%(saves)i movies saved, %(error)i titles skipped." % counts
def save(self, force_insert=False, force_update=False, *args, **kwargs): if not self.guid: self.guid = uuid.uuid1() if not self.slug: self.slug = slugify(self.name) if self.name and not self.group: try: group = AuthGroup.objects.create(name=self.name) except IntegrityError: connection._rollback() id = AuthGroup.objects.count() group = AuthGroup.objects.create(name=" ".join([self.name, str(id)])) group.save() self.group = group elif self.name and self.group: self.group.name = self.name try: self.group.save() except IntegrityError: connection._rollback() super(Group, self).save(force_insert, force_update, *args, **kwargs)
def scrape(self, force=False, verbose=True): counts = { 'saves': 0, 'error': 0, 'noimg': 0, 'skip': 0, } for movie in Movie.objects.all(): try: if movie.poster and not force: counts['skip'] += 1 continue if self.fetch(movie): counts['saves'] += 1 else: counts['noimg'] += 1 except Exception as e: connection._rollback() counts['error'] += 1 if verbose: print "Exception fetching image for \"%s\": %s" % (movie.title, str(e)) continue print "%(saves)i posters fetched, %(error)i posters errors, and %(skip)i skipped, %(noimg)i movies had no poster links." % counts
def form_valid(self, form): form.instance.writer = self.request.user try: return super(CreateReport, self).form_valid(form) except IntegrityError, error: connection._rollback() return self.render_to_response(self.get_context_data(form=form,error_messages=[u'既に日報が存在します']))
def get_average_rating_for_film(film, type): # qset_average = ( # Q(film=film) & # Q(type=type) & # Q(rating__isnull=False) # ) # avg = Rating.objects.filter(qset_month).avg("rating") query = """ SELECT avg(rating), count(*) FROM core_rating WHERE film_id=%(film_id)s AND type=%(type)s AND rating IS NOT NULL """ % { "film_id": film.id, "type": type, } cursor = connection.cursor() cursor.execute(query) overall_film_rating = OverallFilmRating() for row in cursor.fetchall(): overall_film_rating.avg = row[0] overall_film_rating.count = row[1] cursor.close() # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() return overall_film_rating
def get_comment(cid): try: comment = Comment.objects.get(pk = int(cid)) return (True,comment_to_dict(comment)) except: connection._rollback() return (False,str(traceback.format_exc()))
def channel_startmaster(self, data): """ value={'name': cpwd, 'time': int, 'show' : {'name': 'CR001 Titel der Sendung'}} data={"name": "event", "metadata": {"url": "", "genre": "various", "description": "Unspecified description", "name": "event139 Stream mit Uml\u00e4ut \u00fc\u00f6\u00e4\u00dc\u00d6\u00c4\u00df\u00f8\u00d8"}} FIXME: need show """ channel = Channel.objects.get(cluster=data["name"]) available_methods = get_episode_finder() episode = None methods = copy.copy(channel.mapping_method) methods.append("make-live") for method in methods: # print "trying method", method if method not in available_methods: error_handler("Mapping method %s not found" % method, channel) continue finder = available_methods[method]() try: episode = finder.get_episode(channel, data["metadata"]) except Exception, e: print "method", method, "failed" print traceback.format_exc() error_handler("Mapping method %s failed: %s" % (method, e), channel) connection._rollback() if episode: break
def post(self, request, *args, **kwargs): topic = models.Topic.objects.get(pk=request.POST.get("topic")) content = request.POST.get("message") user = self.request.user # soup = BeautifulSoup(content) # if soup.quote: # import re # content = '<quote msgid="t,1,3599@1">sdfsdf</quote>' # soup = BeautifulSoup(content) # msgid = soup.quote['msgid'] # msgid = re.sub(r'\@', ',', msgid) # u = auth.User.objects.get(pk=msgid.split(',')[1]) # m = models.Message.objects.get(pk=msgid.split(',')[2]) # content = re.sub(r"\<quote ", "<div class=\"quoted-message\" ", content) # content = re.sub(r"\<\/quote>", "</div>", content) # soup = BeautifulSoup(content) # temp = soup.div.string # soup.div.string = '' # content = soup # content = """{0}<div class="message-header">From: <a href="/user/{1}">{2}</a> | Posted: {3}</div>{4}<br /><br /></div>""".format(content, u.id, u.username, m.created, temp) m = models.Message.objects.create(user=user, topic=topic, content=content) m.save() topic.updated = datetime.datetime.now() topic.save() try: models.TopicHistory.objects.create(user=user, topic=topic, message=m) except: from django.db import connection connection._rollback() h = models.TopicHistory.objects.get(user=user, topic__id=topic.id) h.message = m h.save() self.success_url = "/topic/{0}".format(topic.id) return HttpResponseRedirect(self.success_url)
def register(request): template_variables = {} if request.method == 'POST': name = request.POST['name'] mail = request.POST['mail'] passw = request.POST['pass'] passb = request.POST['passb'] accept = request.POST.get('accept', False) if accept == False: template_variables['creation_status'] = 'Failure' template_variables['error_message'] = 'Debes de aceptar los terminos y condiciones' if passw != passb or passw == "": template_variables['creation_status'] = 'Failure' template_variables['error_message'] = 'Las contraseñas no coinciden' if is_valid_email(mail) == False: template_variables['creation_status'] = 'Failure' template_variables['error_message'] = 'El mail es inválido' if name == "" or validate_string(name) == False: template_variables['creation_status'] = 'Failure' template_variables['error_message'] = 'Nombre de usuario invalido' aux = template_variables.get('creation_status', '') if aux != 'Failure': try: pay_day = datetime.datetime.today() + datetime.timedelta(days=15) domain = name.lower() + "." + BASE_DOMAIN s_name = name.lower() new_tenant, created = \ Customer_info.objects.get_or_create(domain_url=domain, \ schema_name=s_name, defaults={'name': name, 'role': 'owner', \ 'paid_until': pay_day, 'status': False, 'mail': mail} ) except DatabaseError: connection._rollback() template_variables['creation_status'] = 'Failure' template_variables['error_message'] = 'El nombre de usuario solicitado ' \ 'no esta disponible, intente de nuevo' else: connection.set_tenant(new_tenant) user = User(username=name, is_superuser=True) user.save() user.set_password(passw) user.save() #saving xindex user x_user = Xindex_User() x_user.user = user x_user.first_name = name x_user.save() template_variables['creation_status'] = 'Success' template_variables['name'] = name template_variables['mail'] = mail template_variables['domain'] = domain content = "Bienvenido a xindex! sus datos son: \n" content += "Nombre de usuario: " + name + "\n" content += "Dominio: " + domain + "\n" mail_to(mail, content) template = loader.get_template('tenantCustomer/register.html') context = RequestContext(request, template_variables) return HttpResponse(template.render(context))
def delete(self, *args, **kwargs): # Related objects # Import related objects here to prevent circular references from tendenci.apps.pages.models import Page from tendenci.addons.events.models import Event from tendenci.apps.stories.models import Story pages = Page.objects.filter(header_image=self.pk) events = Event.objects.filter(image=self.pk) stories = Story.objects.filter(image=self.pk) # Set foreign key of related objects to None for page in pages: page.header_image = None page.save() for event in events: event.image = None event.save() for story in stories: story.image = None story.save() # roll back the transaction to fix the error for postgresql # "current transaction is aborted, commands ignored until # end of transaction block" connection._rollback() # delete actual file; do not save() self.instance self.file.delete(save=False) # delete database record super(File, self).delete(*args, **kwargs)
def update_profile(self, data): """Updates the UserProfile and corresponding User object based on the data that was passed in. Note: do NOT attempt a password change this way.""" user_changed = False profile_changed = False for key, value in data.items(): if hasattr(self.user, key): setattr(self.user, key, value) user_changed = True elif hasattr(self, key): setattr(self, key, value) profile_changed = True else: logger.error('neither profile or profile.user has key "%s"', key) try: if user_changed: self.user.save() except IntegrityError: connection._rollback() raise UsernameError('username %s already existed' % data['username']) if profile_changed: self.save()
def get_average_rating_for_film(film, type): # qset_average = ( # Q(film=film) & # Q(type=type) & # Q(rating__isnull=False) # ) # avg = Rating.objects.filter(qset_month).avg("rating") query = """ SELECT avg(rating), count(*) FROM core_rating WHERE film_id=%(film_id)s AND type=%(type)s AND rating IS NOT NULL """ % { 'film_id': film.id, 'type': type } cursor = connection.cursor() cursor.execute(query) overall_film_rating = OverallFilmRating() for row in cursor.fetchall(): overall_film_rating.avg = row[0] overall_film_rating.count = row[1] cursor.close() # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() return overall_film_rating
def save(request): if request.method == "POST": if request.POST["id"].strip(): admin = get_object_or_404(Admin, pk=request.POST["id"]) else: admin = Admin() admin.fullname = request.POST["fullname"] admin.login = request.POST["login"] if request.POST.get("can_write", "0") == "1": admin.can_write = True else: admin.can_write = False # user.role = request.POST['role'] admin.passwd_hash = md5.new(request.POST["pass1"]).hexdigest() try: admin.save() except (IntegrityError, ProgrammingError), e: from django.db import connection connection._rollback() vars = {"basepath": BASEPATH} vars["error_msg"] = integrity_get_message(str(e)) return render_edit(request, admin, vars) return HttpResponseRedirect(BASEPATH + "/%s" % admin.id)
def rollback(): """ This function does the rollback itself and resets the dirty flag. """ connection._rollback() set_clean() _clean_post_commit_callbacks()
def create_object(self, v): """ Create object with attributes. Override to save complex data structures """ for k, nv in six.iteritems(v): if k == "tags": # Merge tags nv = sorted("%s:%s" % (self.system.name, x) for x in nv) v[k] = nv o = self.model(**v) try: o.save() except self.integrity_exception as e: self.logger.warning("Integrity error: %s", e) assert self.unique_field if not self.is_document: from django.db import connection connection._rollback() # Fallback to change object o = self.model.objects.get( **{self.unique_field: v[self.unique_field]}) for k, nv in six.iteritems(v): setattr(o, k, nv) o.save() return o
def delete(self, *args, **kwargs): # Related objects # Import related objects here to prevent circular references from tendenci.apps.pages.models import Page from tendenci.addons.events.models import Event from tendenci.apps.stories.models import Story pages = Page.objects.filter(header_image=self.pk) events = Event.objects.filter(image=self.pk) stories = Story.objects.filter(image=self.pk) # Set foreign key of related objects to None for page in pages: page.header_image = None page.save() for event in events: event.image = None event.save() for story in stories: story.image = None story.save() # roll back the transaction to fix the error for postgresql #"current transaction is aborted, commands ignored until # end of transaction block" connection._rollback() # delete actual file; do not save() self.instance self.file.delete(save=False) # delete database record super(File, self).delete(*args, **kwargs)
def add_user(self, user, **kwargs): """ add a user to the group; check for duplicates return (user, created) """ from django.db import IntegrityError from django.db import transaction, connection try: GroupMembership.objects.create(**{ 'group': self, 'member': user, 'creator_id': kwargs.get('creator_id') or user.pk, 'creator_username': kwargs.get('creator_username') or user.username, 'owner_id': kwargs.get('owner_id') or user.pk, 'owner_username': kwargs.get('owner_username') or user.username, 'status': kwargs.get('status') or True, 'status_detail': kwargs.get('status_detail') or 'active', }) return user, True # created except IntegrityError: connection._rollback() return user, False except Exception: transaction.rollback() return user, False
def tags(request, id_category, tags =''): response = {} category = Category.objects.filter(id=id_category) if len(category) == 1: category = category[0] response['status'] = 200 else: return HttpResponse(json.dumps({"status":404})) if request.method == 'POST': is_super_tag = False if 'is_super_tag' in request.POST: is_super_tag = (request.POST['is_super_tag'] == 'true') old_tags = list(category.tags.all()) # important to convert to list ohterwise it is a generator and if the tags are removed, old tags become empty category.tags.clear() # Removing exsisting relationships if tags: tags_string = tags.split(',') else: tags_string = [] for tag in tags_string: if tag not in [' ', '', '\t', '\r', '\n']: stemmed_name = Stemmer(tag).stem_text() tag_db, created = Tag.objects.get_or_create(name = tag, is_super_tag = is_super_tag, defaults = {'stemmed_name' : stemmed_name}) if created: tag_db.stemmed_name = stemmed_name tag_db.save() try: category.tags.add(tag_db) except Exception, e: connection._rollback() new_tags = list(category.tags.all())
def with_rates(self, event): from django.db import connection cursor = connection.cursor() cursor.execute(""" SELECT n.id, n.film_id, n.person_id, n.type, n.oscar_type, r.average_score as avg, r.number_of_votes as count FROM event_nominated n LEFT JOIN core_filmranking r ON n.type = r.type and n.film_id = r.film_id and (r.actor_id is null and n.person_id is null or r.actor_id=n.person_id) WHERE n.event_id = %d ORDER BY n.oscar_type,avg desc """ % event.pk) result_list = [] for row in cursor.fetchall(): p = self.model(id=row[0], film_id=row[1], person_id=row[2], type=row[3], oscar_type=row[4]) p.avg_rating = "%3.1f" % (row[5] or 0) p.count = row[6] result_list.append(p) # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() return result_list
def save(self): def_slug = slugify(self.title) cur_int = 2 # # Publishing this for the first time, add publish date # (notice that the super.save() routine is called when we work on the slug below) if self.status == 'PUB' and self.published is None : self.published = datetime.now() # # Save the slug and make sure that it is unique if not self.slug or self.slug == '': self.slug = def_slug while True: try: super(Article, self).save() except IntegrityError: self.slug = '%s-%s' % (def_slug, str(cur_int)) cur_int += 1 except DatabaseError: from django.db import connection connection._rollback() else: break
def show_startmaster(self, data): """ value={'name': cpwd, 'time': int, 'show' : {'name': 'CR001 Titel der Sendung'}} FIXME: need show """ channel = Channel.objects.get(cluster=data['name']) available_methods = get_episode_finder() episode = None methods = copy.copy(channel.mapping_method) methods.append("make-live") for method in methods: # print "trying method", method if method not in available_methods: error_handler("Mapping method %s not found" % method, channel) continue finder = available_methods[method]() try: episode = finder.get_episode(channel, data['show']) except Exception, e: print "method", method, "failed" print traceback.format_exc() error_handler("Mapping method %s failed: %s" % (method, e), channel) connection._rollback() if episode: break
def form_valid(self, form): form.instance.subtask.update_when_status_change(form.instance.status, form.instance.report.date) try: return super(UpdateTaskReport, self).form_valid(form) except IntegrityError, error: connection._rollback() return self.render_to_response(self.get_context_data(form=form ,error_messages=[u'既に同じタスク報告が日報に存在します']))
def save(request): if request.method == 'POST': if request.POST['id'].strip(): conf = get_object_or_404(Conference, pk=request.POST['id']) else: conf = Conference() conf.name = request.POST['name'] conf.description = request.POST['desc'] conf.cost = float(request.POST['cost']) conf.contractor_id = int(request.POST['contractor']) conf.hotel_id = int(request.POST['hotel']) conf.start_date = datetime(int(request.POST['start_date_year']), int(request.POST['start_date_month']), int(request.POST['start_date_day'])) conf.end_date = datetime(int(request.POST['end_date_year']), int(request.POST['end_date_month']), int(request.POST['end_date_day'])) conf.reg_deadline = datetime(int(request.POST['reg_deadline_year']), int(request.POST['reg_deadline_month']), int(request.POST['reg_deadline_day'])) try: conf.save() except (IntegrityError, ProgrammingError), e: from django.db import connection connection._rollback() vars = { 'basepath': BASEPATH } vars['error_msg'] = integrity_get_message(str(e)) return render_edit(request,conf,vars) return HttpResponseRedirect(BASEPATH+"/%s" % conf.id)
def geoserver_post_save(instance, sender, created, **kwargs): from geonode.messaging import producer # this is attached to various models, (ResourceBase, Document) # so we should select what will be handled here if isinstance(instance, Layer): instance_dict = model_to_dict(instance) payload = json_serializer_producer(instance_dict) producer.geoserver_upload_layer(payload) if getattr(settings, 'DELAYED_SECURITY_SIGNALS', False): instance.set_dirty_state() if instance.storeType != 'remoteStore' and created: logger.info("... Creating Default Resource Links for Layer [%s]" % (instance.alternate)) try: set_resource_default_links(instance, sender, prune=True) except BaseException: from django.db import connection connection._rollback() logger.warn( "Failure Creating Default Resource Links for Layer [%s]" % (instance.alternate)) logger.info("... Creating Thumbnail for Layer [%s]" % (instance.alternate)) try: create_gs_thumbnail(instance, overwrite=True, check_bbox=True) except BaseException: logger.warn("Failure Creating Thumbnail for Layer [%s]" % (instance.alternate))
def add_comment(uid,tid,text,parent=None): try: id = generateId() creator = LcUser.objects.get(pk = int(uid)) thread = Thread.objects.get(pk = int(tid)) if (parent != None): parent = Comment.objects.get(pk = int(parent)) timestamp = int(time()) c = Comment(id = id, creator=creator, thread = thread, parent = parent, text=text, time_created = timestamp) c.save() notified = [creator.id] if (parent != None): if parent.creator.id != creator.id: u.notify(parent.creator.id,c.id,True) notified.append(parent.creator.id) followers = u.get_followers(int(tid)) for f in followers: if f not in notified: u.notify(f,c.id,False) p = -1 if parent: p = parent.id return (True,id) except: connection._rollback() return (False,str(traceback.format_exc()))
def _value(self): global is_setting_initializing use_db, overrides = get_overrides() if not use_db: try: val = overrides[self.group.key][self.key] except KeyError: if self.use_default: val = self.default else: raise SettingNotSet('%s.%s is not in your LIVESETTINGS_OPTIONS' % (self.group.key, self.key)) else: try: val = self.setting.value except SettingNotSet as sns: is_setting_initializing = False if self.use_default: val = self.default if overrides: # maybe override the default grp = overrides.get(self.group.key, {}) if self.key in grp: val = grp[self.key] else: val = NOTSET except AttributeError as ae: is_setting_initializing = False log.error("Attribute error: %s", ae) log.error("%s: Could not get _value of %s", self.key, self.setting) raise (ae) except Exception as e: global _WARN if is_setting_initializing and isinstance(e, DatabaseError) and str(e).find( "livesettings_setting") > -1: if 'livesettings_setting' not in _WARN: log.warn(str(e).strip()) _WARN['livesettings_setting'] = True log.warn('Error loading livesettings from table, OK if you are in syncdb or before it. ROLLBACK') connection._rollback() if self.use_default: val = self.default else: raise ImproperlyConfigured("All settings used in startup must have defaults, %s.%s does not", self.group.key, self.key) else: is_setting_initializing = False import traceback traceback.print_exc() log.error("Problem finding settings %s.%s, %s", self.group.key, self.key, e) raise SettingNotSet("Startup error, couldn't load %s.%s" % (self.group.key, self.key)) else: is_setting_initializing = False return val
def get_list(query_string, conn=None): connection = get_connection(conn) cursor = connection.cursor() try: cursor.execute(query_string) except Exception, e: connection._rollback() raise e
def _pre_delete(sender, instance, **args): #remove the view for this application try: cursor = connection.cursor() cursor.execute("DROP VIEW {} CASCADE".format(instance.records_view)) except: #drop failed, maybe the view does not exist, ignore the exception connection._rollback()
def insere_noticia(doc): try: Noticia.objects.create(**doc) except IntegrityError: from django.db import connection connection._rollback() urls = Url.objects.filter(url=doc.get('link')) urls.update(status_processamento=True)
def rollback_unless_managed(): """ Rolls back changes if the system is not in managed transaction mode. """ if not is_managed(): connection._rollback() else: set_dirty()
def _value(self): global is_setting_initializing use_db, overrides = get_overrides() if not use_db: try: val = overrides[self.group.key][self.key] except KeyError: if self.use_default: val = self.default else: raise SettingNotSet('%s.%s is not in your LIVESETTINGS_OPTIONS' % (self.group.key, self.key)) else: try: val = self.setting.value except SettingNotSet: is_setting_initializing = False if self.use_default: val = self.default if overrides: # maybe override the default grp = overrides.get(self.group.key, {}) if self.key in grp: val = grp[self.key] else: val = NOTSET except AttributeError as ae: is_setting_initializing = False log.error("Attribute error: %s", ae) log.error("%s: Could not get _value of %s", self.key, self.setting) raise(ae) except Exception as e: global _WARN if is_setting_initializing and isinstance(e, DatabaseError) and str(e).find("livesettings_setting") > -1: if not 'livesettings_setting' in _WARN: log.warn(str(e).strip()) _WARN['livesettings_setting'] = True log.warn('Error loading livesettings from table, OK if you are in syncdb or before it. ROLLBACK') connection._rollback() if self.use_default: val = self.default else: raise ImproperlyConfigured("All settings used in startup must have defaults, %s.%s does not", self.group.key, self.key) else: is_setting_initializing = False import traceback traceback.print_exc() log.error("Problem finding settings %s.%s, %s", self.group.key, self.key, e) raise SettingNotSet("Startup error, couldn't load %s.%s" % (self.group.key, self.key)) else: is_setting_initializing = False return val
def _pre_save(sender, instance, **args): #create a view for this application try: cursor = connection.cursor() cursor.execute("CREATE OR REPLACE VIEW {} AS SELECT r.* FROM catalogue_application a join catalogue_applicationlayer l on a.id = l.application_id join catalogue_record r on l.layer_id = r.id WHERE a.name = '{}' and r.active order by l.order, r.identifier".format(instance.records_view, instance.name)) except Exception as e: #create view failed connection._rollback() raise ValidationError(e)
def getUserByKey(key): try: user = ApiKey.objects.get(key=key) return user.user except ApiKey.DoesNotExist: from django.db import connection connection._rollback() return None
def _get_usage(self, model, counts=False, min_count=None, extra_joins=None, extra_criteria=None, params=None): """ Perform the custom SQL query for ``usage_for_model`` and ``usage_for_queryset``. """ if min_count is not None: counts = True model_table = qn(model._meta.db_table) model_pk = '%s.%s' % (model_table, qn(model._meta.pk.column)) query = """ SELECT DISTINCT %(tag)s.id, %(tag)s.name%(count_sql)s FROM %(tag)s INNER JOIN %(tagged_item)s ON %(tag)s.id = %(tagged_item)s.tag_id INNER JOIN %(model)s ON %(tagged_item)s.object_id = %(model_pk)s %%s WHERE %(tagged_item)s.content_type_id = %(content_type_id)s %%s GROUP BY %(tag)s.id, %(tag)s.name %%s ORDER BY %(tag)s.name ASC""" % { 'tag': qn(self.model._meta.db_table), 'count_sql': counts and (', COUNT(%s)' % model_pk) or '', 'tagged_item': qn(TaggedItem._meta.db_table), 'model': model_table, 'model_pk': model_pk, 'content_type_id': ContentType.objects.get_for_model(model).pk, } min_count_sql = '' if min_count is not None: min_count_sql = 'HAVING COUNT(%s) >= %%s' % model_pk params.append(min_count) cursor = connection.cursor() cursor.execute(query % (extra_joins, extra_criteria, min_count_sql), params) tags = [] for row in cursor.fetchall(): t = self.model(*row[:2]) if counts: t.count = row[2] tags.append(t) cursor.close() # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() return tags
def log_create(sender, instance, created, **kwargs): """ Log the creation of a new user """ ## make sure it has a wheel profile try: WheelProfile.objects.get_or_create(user=instance) except DatabaseError: logging.error("Failed to create profile for %s, perhaps migrations haven't run yet?" % instance) from django.db import connection connection._rollback()
def _pre_delete(sender, instance, **args): # remove the view for this application try: cursor = connection.cursor() cursor.execute("DROP VIEW {} CASCADE".format( instance.records_view)) except BaseException: # drop failed, maybe the view does not exist, ignore the exception connection._rollback()
def get_related_ids(self, obj, queryset_or_model, num=None): """ Retrieve a list of instances of the specified model which share tags with the model instance ``obj``, ordered by the number of shared tags in descending order. If ``num`` is given, a maximum of ``num`` instances will be returned. """ queryset, model = get_queryset_and_model(queryset_or_model) model_table = qn(model._meta.db_table) content_type = ContentType.objects.get_for_model(obj) related_content_type = ContentType.objects.get_for_model(model) query = """ SELECT %(model_pk)s, SUM(%(tag)s.weight) AS %(count)s FROM %(model)s, %(tagged_item)s, %(tag)s, %(tagged_item)s related_tagged_item WHERE %(tagged_item)s.object_id = %%s AND %(tagged_item)s.content_type_id = %(content_type_id)s AND %(tag)s.id = %(tagged_item)s.tag_id AND related_tagged_item.content_type_id = %(related_content_type_id)s AND related_tagged_item.tag_id = %(tagged_item)s.tag_id AND %(model_pk)s = related_tagged_item.object_id""" if content_type.pk == related_content_type.pk: # Exclude the given instance itself if determining related # instances for the same model. query += """ AND related_tagged_item.object_id != %(tagged_item)s.object_id""" query += """ GROUP BY %(model_pk)s ORDER BY %(count)s DESC %(limit_offset)s""" query = query % { 'model_pk': '%s.%s' % (model_table, qn(model._meta.pk.column)), 'count': qn('count'), 'model': model_table, 'tagged_item': qn(self.model._meta.db_table), 'tag': qn(self.model._meta.get_field('tag').rel.to._meta.db_table), 'content_type_id': content_type.pk, 'related_content_type_id': related_content_type.pk, # Hardcoding this for now just to get tests working again - this # should now be handled by the query object. 'limit_offset': num is not None and 'LIMIT %s' or '', } cursor = connection.cursor() params = [obj.pk] if num is not None: params.append(num) cursor.execute(query, params) result_set = [row for row in cursor.fetchall()] cursor.close() # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() return result_set
def forwards(self, orm): try: cursor = connection.cursor() cursor.execute("select * from information_schema.tables where table_name='mig_auth_user_t4_to_t5'") row = cursor.fetchone() if row: cursor.execute("ALTER TABLE mig_auth_user_t4_to_t5 DROP CONSTRAINT mig_auth_user_t4_to_t5_t5_id_fkey") transaction.commit_unless_managed() except DatabaseError: connection._rollback()
def forwards(self, orm): try: cursor = connection.cursor() cursor.execute("select * from information_schema.tables where table_name='auth_message'") row = cursor.fetchone() if row: cursor.execute("ALTER TABLE auth_message DROP CONSTRAINT auth_message_user_id_fkey") transaction.commit_unless_managed() except DatabaseError: connection._rollback()
def _db_accessible(): """Discover whether we have a working connection to the database""" from psycopg2 import OperationalError from django.db import connection try: connection.introspection.table_names() return True except OperationalError: connection._rollback() return False
def _pre_save(sender, instance, **args): # create a view for this application try: cursor = connection.cursor() cursor.execute( "CREATE OR REPLACE VIEW {} AS SELECT r.* FROM catalogue_application a join catalogue_applicationlayer l on a.id = l.application_id join catalogue_record r on l.layer_id = r.id WHERE a.name = '{}' and r.active order by l.order, r.identifier" .format(instance.records_view, instance.name)) except Exception as e: # create view failed connection._rollback() raise ValidationError(e)
def bot_save_handler(sender, instance, created, **kwargs): if created: try: u = User.objects.create_user(instance.name, '*****@*****.**', '') u.save() except IntegrityError: connection._rollback() return instance.user = u instance.save()
def create_or_update(cls, unique_fields, other_fields): q = cls.objects.filter(**unique_fields) if q.count() == 0: obj = cls(**dict(unique_fields, **other_fields)) else: obj = q[0] for k, v in other_fields.iteritems(): obj.__setattr__(k, v) try: obj.save() except Exception, e: connection._rollback() raise e
def _db_populated(self): """Discover whether the database has this application's tables""" from django.db.utils import DatabaseError if not self._db_accessible(): return False try: from south.models import MigrationHistory MigrationHistory.objects.count() return True except DatabaseError: from django.db import connection connection._rollback() return False
def handle_label(self, path, **options): with open(path, 'r') as data: for row in csv.reader(data, delimiter='\t'): try: user, password = self.create_user(*row) if self.notify: self.notify_user(user, password) except Exception as e: print "Could not create user with values:\n\t%s\n\tError: %s" % ("\t".join(row), str(e)) self.errors += 1 connection._rollback() continue
def saveUrl(self): try: safeUrl = SiteConfig() safeUrl.url = self._dataDict.get('url') safeUrl.name = self._dataDict.get('name') safeUrl.save() except Exception, e: connection._rollback() if e.args[0] == 1062: message = 'URL: ' + str(self._dataDict) + ' - уже есть в базе' else: message = 'Не удалось сохранить ' + str(self._dataDict) return False, message
def catchall(*args, **kwargs): """Decorator to catch all possible Database Errors and return a default value""" try: return method(*args, **kwargs) except (AppRegistryNotReady, ObjectDoesNotExist, OperationalError, InternalError, ProgrammingError): # Handle Postgres transaction revert if 'postgresql' in settings.DATABASES['default']['ENGINE']: from django.db import connection # pylint: disable=protected-access connection._rollback() return default else: return default
def transfer_users_from_allauth_to_pysocial(apps, schema_editor): """ Transfer user UIDs from Allauth to Python Social Auth. Has to be done in raw SQL since Django does not recognize allauth after removing it from the project. """ all_tables = connection.introspection.table_names() if "socialaccount_socialaccount" in all_tables: try: with connection.cursor() as cursor: cursor.execute( "INSERT INTO social_auth_usersocialauth (user_id, provider, uid, extra_data, created, modified)" "SELECT user_id, 'tunnistamo', uid, extra_data, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP FROM socialaccount_socialaccount;" ) except IntegrityError: connection._rollback()
def get_or_create_django_object(self, object_class, params): try: self.django_object, created = object_class.objects.select_for_update().get_or_create( **params ) transaction.commit() except Exception: connection._rollback() self.django_object = object_class.objects.get(pk = params['pk']) created = False if not created: self.django_object = self.update(params['defaults']) return self.django_object, created
def run_bots(): count_added = 0 count_duplicates = 0 b = Bot.objects.order_by('last_run_at')[0] links = b.parse_url() for l in reversed(links): p = Post(user=b.user, gif_url=l) try: p.save() count_added += 1 except IntegrityError: connection._rollback() count_duplicates += 1 b.last_run_at = datetime.datetime.now() b.save() return "added: {0}, duplicates: {1}".format(count_added, count_duplicates)
def set_categories_to_product(product, dalliz_categories, osm, set_match = True): """ Setting dalliz categories to a product """ if product is not None: old_categories = product.dalliz_category.all() new, common, removed = diff(old_categories, dalliz_categories) for c in new: try: product.dalliz_category.add(c) except Exception, e: connection._rollback() for c in removed: try: product.dalliz_category.remove(c) except Exception, e: connection._rollback()
def _db_populated(self): """Discover whether the database has this application's tables""" from django.db.utils import DatabaseError if not self._db_accessible(): return False try: from django.db import connection from django.db.migrations.loader import MigrationLoader loader = MigrationLoader(connection, ignore_no_migrations=True) loader.build_graph() return len(loader.applied_migrations) > 0 except DatabaseError: from django.db import connection connection._rollback() return False
def _safe_get_siteid(site): global is_site_initializing, is_first_warn if not site: try: site = Site.objects.get_current() siteid = site.id except Exception, e: if is_site_initializing and isinstance(e, DatabaseError) and str(e).find('django_site') > -1: if is_first_warn: log.warn(str(e).strip()) is_first_warn = False log.warn('Can not get siteid; probably before syncdb; ROLLBACK') connection._rollback() else: is_site_initializing = False siteid = settings.SITE_ID else: is_site_initializing = False
def update(self): """ Updates the Schema.last_updated fields after scraping is done. """ self.num_added = 0 self.num_changed = 0 update_start = datetime.datetime.now() # We use a try/finally here so that the DataUpdate object is created # regardless of whether the scraper raised an exception. try: got_error = True super(NewsItemListDetailScraper, self).update() got_error = False finally: # Rollback, in case the database is in an aborted # transaction. This avoids the "psycopg2.ProgrammingError: # current transaction is aborted, commands ignored until # end of transaction block" error. from django.db import connection connection._rollback() update_finish = datetime.datetime.now() # Clear the Schema cache, in case the schemas have been # updated in the database since we started the scrape. self._schemas_cache = self._schema_cache = None for s in self.schemas.values(): s.last_updated = datetime.date.today() s.save() DataUpdate.objects.create( schema=s, update_start=update_start, update_finish=update_finish, num_added=self.num_added, num_changed=self.num_changed, # None of our scrapers delete records yet, but we have the # plumbing in place here in case future scrapers need to do # that. num_deleted=0, num_skipped=self.num_skipped, got_error=got_error, )
def get_intersection_by_model(self, queryset_or_model, tags): """ Create a ``QuerySet`` containing instances of the specified model associated with *all* of the given list of tags. """ tags = get_tag_list(tags) tag_count = len(tags) queryset, model = get_queryset_and_model(queryset_or_model) if not tag_count: return model._default_manager.none() model_table = qn(model._meta.db_table) # This query selects the ids of all objects which have all the # given tags. query = """ SELECT %(model_pk)s FROM %(model)s, %(tagged_item)s WHERE %(tagged_item)s.content_type_id = %(content_type_id)s AND %(tagged_item)s.tag_id IN (%(tag_id_placeholders)s) AND %(model_pk)s = %(tagged_item)s.object_id GROUP BY %(model_pk)s HAVING COUNT(%(model_pk)s) = %(tag_count)s""" % { 'model_pk': '%s.%s' % (model_table, qn(model._meta.pk.column)), 'model': model_table, 'tagged_item': qn(self.model._meta.db_table), 'content_type_id': ContentType.objects.get_for_model(model).pk, 'tag_id_placeholders': ','.join(['%s'] * tag_count), 'tag_count': tag_count, } cursor = connection.cursor() cursor.execute(query, [tag.pk for tag in tags]) object_ids = [row[0] for row in cursor.fetchall()] cursor.close() # http://www.mail-archive.com/[email protected]/msg92582.html connection._rollback() if len(object_ids) > 0: return queryset.filter(pk__in=object_ids) else: return model._default_manager.none()
def new(request, rec_id): if not check_owner(rec_id, request.user.id): return redirect(reverse('dynamic.views.domains.index')) cuenta = Accounts.objects.get(user=request.user.id) usados = RedirectDynamic.objects.filter(dynamic__record__id=rec_id).count() dynamic = Dynamics.objects.filter(record__id=rec_id)[0] redir = RedirectDynamic() # Cuento cuantos usuarios nulos existe, para comprar si ya ha # marcado el checkbox de "Redireccionar todo" if RedirectDynamic.objects.filter(dynamic__record__id=rec_id).filter( username='').count() == 1: exist_all_redirect = True else: exist_all_redirect = False redir.dynamic_id = dynamic.id if request.method == 'POST': form = RedirectDynamicForm(request.POST, instance=redir, auto_id=False) if form.is_valid(): try: form.save() return redirect( reverse('mail.views.redirects_dynamic.index', args=[rec_id])) except IntegrityError: connection._rollback() form._errors['username'] = ["El usuario ya existe."] del form.cleaned_data['username'] else: form = RedirectDynamicForm(instance=redir, auto_id=False) return render_to_response('mail_redirects_dynamic_edit.html', { 'form': form, 'cuenta': cuenta, 'usados': usados, 'disponibles': cuenta.limit_email_redirect - usados, 'total': cuenta.limit_email_redirect, 'rec_id': rec_id, 'dom': dynamic.getName(), 'tipo': 'Redirecciones', 'nuevo': True, 'exist_all_redirect': exist_all_redirect }, context_instance=RequestContext(request))
def set_tags_to_product(product, tags, osm, set_match = True, is_super_tag = False): """ Setting tags to a product """ # Clearing tags of products if product is not None: old_tags = product.tag.filter(is_super_tag = is_super_tag) new, common, removed = diff(old_tags, tags) for t in new: try: product.tag.add(t) except Exception, e: connection._rollback() for t in removed: try: product.tag.remove(t) except Exception, e: connection._rollback()