From nobody Tue May 13 19:48:59 2025 Delivered-To: importer2@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1555604530; cv=none; d=zoho.com; s=zohoarc; b=DagOegnB0Jssdx2sRm+P7cFGbLKG1PbuGyBxheVVxgUOqdRd13VZzc4N8Oj7KFrXskd/WSykgeiNkDk3n7aQlCKxD/FjzYMwMGuwS4ArBlM9qPvJtyaFGfDPfO81GcYdN7T6KySmeMFQKoqB3yi+RymffIuJvqHI7a1AeVNp2bc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555604530; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=RtHNiCKp/WdJgB1sbhcGS/ExtRMB0bpAvojhQYXLHqI=; b=ad20zLIoIZW0Yi0hnJpjhbWpAllhD8s+B0t8ce35ZsWXTV1K9qPc99dje7Z4/jfVJg06oHKn1O/OszYx/znVjjoJLxJc/VzVEB+K0jtaf0/M0JDakL2YaMo+nhevEUMP3ZaCyrXUmdmTooNMUnL6OdsV4zdt1GIVGIDHNfSOe6w= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1555604530304349.3880554081602; Thu, 18 Apr 2019 09:22:10 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 24F88308A111; Thu, 18 Apr 2019 16:22:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 191AE5D9CC; Thu, 18 Apr 2019 16:22:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0C7044EA71; Thu, 18 Apr 2019 16:22:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x3IGM8on009234 for ; Thu, 18 Apr 2019 12:22:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 421DC60BF7; Thu, 18 Apr 2019 16:22:08 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-46.ams2.redhat.com [10.36.112.46]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8DD5B60C61 for ; Thu, 18 Apr 2019 16:22:02 +0000 (UTC) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Thu, 18 Apr 2019 18:21:45 +0200 Message-Id: <20190418162146.5695-10-pbonzini@redhat.com> In-Reply-To: <20190418162146.5695-1-pbonzini@redhat.com> References: <20190418162146.5695-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH 09/10] models: switch from property tables to field X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 18 Apr 2019 16:22:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Fortunately, most accesses to the property tables were hidden behind accessors. Therefore, apart from the usual ugly migration (which in this case is more or less a copy of the config migration) it is almost enough to change the accessors to look into the JSON dictionary. We can also use the new API to access a whole subset of the properties, which simplifies the access to testing.check_in.*. --- .../0052_populate_property_fields.py | 67 +++++++++++ api/models.py | 109 ++++++++++-------- mods/testing.py | 6 +- 3 files changed, 128 insertions(+), 54 deletions(-) create mode 100644 api/migrations/0052_populate_property_fields.py diff --git a/api/migrations/0052_populate_property_fields.py b/api/migratio= ns/0052_populate_property_fields.py new file mode 100644 index 0000000..432e809 --- /dev/null +++ b/api/migrations/0052_populate_property_fields.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations +from django.db.models import Q + +def do_properties_to_field(obj, propset): + properties =3D {} + props =3D propset.all() + for p in props: + *path, last =3D p.name.split('.') + parent =3D properties + for item in path: + parent =3D parent.setdefault(item, {}) + parent[last] =3D p.value + obj.properties =3D properties + #print(obj, properties) + obj.save() + props.delete() + +def properties_to_field(apps, schema_editor): + Project =3D apps.get_model('api', 'Project') + for po in Project.objects.all(): + do_properties_to_field(po, po.projectproperty_set) + Message =3D apps.get_model('api', 'Message') + for po in Message.objects.all(): + do_properties_to_field(po, po.messageproperty_set) + +def flatten_properties(source, prefix, result=3DNone): + if result is None: + result =3D {} + for k, v in source.items(): + if isinstance(v, dict): + flatten_properties(v, prefix + k + '.', result) + else: + result[prefix + k] =3D v + return result + +def do_field_to_properties(source, propclass, **kwargs): + props =3D flatten_properties(source, '') + for k, v in props.items(): + #print(k, v) + new_prop =3D propclass(name=3Dk, value=3Dv, **kwargs) + new_prop.save() + +def field_to_properties(apps, schema_editor): + Project =3D apps.get_model('api', 'Project') + ProjectProperty =3D apps.get_model('api', 'ProjectProperty') + for po in Project.objects.all(): + do_field_to_properties(po.properties, ProjectProperty, project=3Dp= o) + Message =3D apps.get_model('api', 'Message') + MessageProperty =3D apps.get_model('api', 'MessageProperty') + for m in Message.objects.all(): + do_field_to_properties(m.properties, MessageProperty, message=3Dm) + + +class Migration(migrations.Migration): + + dependencies =3D [ + ('api', '0051_auto_20190418_1346'), + ] + + operations =3D [ + migrations.RunPython(properties_to_field, + reverse_code=3Dfield_to_properties), + ] diff --git a/api/models.py b/api/models.py index 7c3aefb..2a6c09b 100644 --- a/api/models.py +++ b/api/models.py @@ -182,30 +182,39 @@ class Project(models.Model): return self.objects.filter(name=3Dproject).exists() =20 def get_property(self, prop, default=3DNone): - a =3D ProjectProperty.objects.filter(project=3Dself, name=3Dprop).= first() - if a: - return a.value - else: - return default - - def get_properties(self): - r =3D {} - for m in ProjectProperty.objects.filter(project=3Dself): - r[m.name] =3D m.value - return r - - def _do_set_property(self, prop, value): - if value is None: - ProjectProperty.objects.filter(project=3Dself, name=3Dprop).de= lete() + x =3D self.properties + *path, last =3D prop.split('.') + for item in path: + if not item in x: + return default + x =3D x[item] + return x.get(last, default) + + def delete_property(self, prop): + x =3D self.properties + *path, last =3D prop.split('.') + for item in path: + if not item in x: + return + x =3D x[item] + if not last in x: return - pp, created =3D ProjectProperty.objects.get_or_create(project=3Dse= lf, - name=3Dprop) - pp.value =3D value - pp.save() + old_val =3D x[last] + del x[last] + self.save() + emit_event("SetProperty", obj=3Dself, name=3Dprop, value=3DNone, + old_value=3Dold_val) =20 def set_property(self, prop, value): - old_val =3D self.get_property(prop) - self._do_set_property(prop, value) + if value is None: + self.delete_property(prop) + x =3D self.properties + *path, last =3D prop.split('.') + for item in path: + x =3D x.setdefault(item, {}) + old_val =3D x.get(last) + x[last] =3D value + self.save() emit_event("SetProperty", obj=3Dself, name=3Dprop, value=3Dvalue, old_value=3Dold_val) =20 @@ -613,37 +622,39 @@ class Message(models.Model): self.save() =20 def get_property(self, prop, default=3DNone): - return self.get_properties().get(prop, default) - - def get_properties(self): - if hasattr(self, '_properties'): - if self._properties is not None: - return self._properties - else: - # The prefetch cache is invalidated, query again - all_props =3D MessageProperty.objects.filter(message=3Dsel= f) - else: - all_props =3D self.messageproperty_set.all() - r =3D {} - for m in all_props: - r[m.name] =3D m.value - self._properties =3D r - return r - - def _do_set_property(self, prop, value): - if value is None: - MessageProperty.objects.filter(message=3Dself, name=3Dprop).de= lete() + x =3D self.properties + *path, last =3D prop.split('.') + for item in path: + if not item in x: + return default + x =3D x[item] + return x.get(last, default) + + def delete_property(self, prop): + x =3D self.properties + *path, last =3D prop.split('.') + for item in path: + if not item in x: + return + x =3D x[item] + if not last in x: return - mp, created =3D MessageProperty.objects.get_or_create(message=3Dse= lf, - name=3Dprop) - mp.value =3D value - mp.save() - # Invalidate cache - self._properties =3D None + old_val =3D x[last] + del x[last] + self.save() + emit_event("SetProperty", obj=3Dself, name=3Dprop, value=3DNone, + old_value=3Dold_val) =20 def set_property(self, prop, value): - old_val =3D self.get_property(prop) - self._do_set_property(prop, value) + if value is None: + self.delete_property(prop) + x =3D self.properties + *path, last =3D prop.split('.') + for item in path: + x =3D x.setdefault(item, {}) + old_val =3D x.get(last) + x[last] =3D value + self.save() emit_event("SetProperty", obj=3Dself, name=3Dprop, value=3Dvalue, old_value=3Dold_val) =20 diff --git a/mods/testing.py b/mods/testing.py index 969ce84..e5562ba 100644 --- a/mods/testing.py +++ b/mods/testing.py @@ -369,14 +369,10 @@ class TestingModule(PatchewModule): =20 def check_active_testers(self, project): at =3D [] - for k, v in project.get_properties().items(): - prefix =3D "testing.check_in." - if not k.startswith(prefix): - continue + for tn, v in project.get_property('testing.check_in', {}).items(): age =3D time.time() - v if age > 10 * 60: continue - tn =3D k[len(prefix):] at.append("%s (%dmin)" % (tn, math.ceil(age / 60))) if not at: return --=20 2.21.0 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel