public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/grumpy:master commit in: backend/lib/, /, backend/
@ 2016-09-06 21:21 Mart Raudsepp
  0 siblings, 0 replies; only message in thread
From: Mart Raudsepp @ 2016-09-06 21:21 UTC (permalink / raw
  To: gentoo-commits

commit:     6d5b0a5ba688677a127d1df1439080482c9709d1
Author:     Mart Raudsepp <leio <AT> gentoo <DOT> org>
AuthorDate: Tue Sep  6 21:19:29 2016 +0000
Commit:     Mart Raudsepp <leio <AT> gentoo <DOT> org>
CommitDate: Tue Sep  6 21:20:02 2016 +0000
URL:        https://gitweb.gentoo.org/proj/grumpy.git/commit/?id=6d5b0a5b

Add initial code to sync categories from packages.g.o with associated plumbing

Now this should make http://localhost:5000 show the available categories:
./manage.py init
./manage.py sync_gentoo
./manage.py runserver

 backend/__init__.py   | 15 ++++++++++++++-
 backend/lib/models.py | 28 ++++++++++++++++++++++++++++
 backend/lib/sync.py   | 21 +++++++++++++++++++++
 manage.py             | 15 +++++++++++++--
 requirements.txt      |  2 ++
 5 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/backend/__init__.py b/backend/__init__.py
index 81ca7eb..b03432b 100644
--- a/backend/__init__.py
+++ b/backend/__init__.py
@@ -1,7 +1,20 @@
 from flask import Flask
+from flask_sqlalchemy import SQLAlchemy
 
 app = Flask(__name__)
+app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///grumpy.db" # FIXME: configuration support
+db = SQLAlchemy(app)
+
+from .lib import models
+
 
 @app.route("/")
 def hello_world():
-    return "Hello World!"
+    categories = models.Category.query.all()
+    text = ""
+    for cat in categories:
+        text += "<b>%s</b>: %s<br>" % (cat.name, cat.description)
+    return "Hello World! These are the package categories I know about:<br>%s" % text
+
+
+__all__ = ["app", "db"]

diff --git a/backend/lib/models.py b/backend/lib/models.py
new file mode 100644
index 0000000..5088e3e
--- /dev/null
+++ b/backend/lib/models.py
@@ -0,0 +1,28 @@
+from .. import db
+
+
+class Category(db.Model):
+    id = db.Column(db.Integer, primary_key=True)
+    name = db.Column(db.String(30), unique=True, nullable=False)
+    description = db.Column(db.String(500))
+
+    def __repr__(self):
+        return "<Category %r>" % self.name
+
+class Package(db.Model):
+    id = db.Column(db.Integer, primary_key=True)
+    name = db.Column(db.String(128), nullable=False)
+    category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)
+    category = db.relationship('Category', backref=db.backref('packages', lazy='dynamic'))
+
+    def __repr__(self):
+        return "<Package %r/%r>" % (self.category.name, self.name)
+
+class PackageVersion(db.Model):
+    id = db.Column(db.Integer, primary_key=True)
+    version = db.Column(db.String(128), nullable=False)
+    package_id = db.Column(db.Integer, db.ForeignKey('package.id'), nullable=False)
+    package = db.relationship('Package', backref=db.backref('versions', lazy='dynamic'))
+
+    def __repr__(self):
+        return "<PackageVersion %s/%r-%r>" % (self.package.category.name, self.package.name, self.version)

diff --git a/backend/lib/sync.py b/backend/lib/sync.py
new file mode 100644
index 0000000..3cfb746
--- /dev/null
+++ b/backend/lib/sync.py
@@ -0,0 +1,21 @@
+from flask import json
+import requests
+from .. import app, db
+from .models import Category
+
+http_session = requests.session()
+
+def sync_categories():
+    url = "https://packages.gentoo.org/categories.json"
+    data = http_session.get(url)
+    categories = json.loads(data.text)
+    existing_categories = {}
+    for cat in Category.query.all():
+        existing_categories[cat.name] = cat
+    for category in categories:
+        if category['name'] in existing_categories:
+            existing_categories[category['name']].description = category['description']
+        else:
+            new_cat = Category(name=category['name'], description=category['description'])
+            db.session.add(new_cat)
+    db.session.commit()

diff --git a/manage.py b/manage.py
index b28d93a..4f123aa 100755
--- a/manage.py
+++ b/manage.py
@@ -3,15 +3,26 @@
 
 from flask_script import Manager, Shell
 
-from backend import app
+from backend import app, db
+from backend.lib.sync import sync_categories
 
 
 manager = Manager(app)
 
 def shell_context():
-    return dict(app=manager.app)
+    return dict(app=manager.app, db=db)
 
 manager.add_command('shell', Shell(make_context=shell_context))
 
+@manager.command
+def init():
+    """Initialize empty database with tables"""
+    db.create_all()
+
+@manager.command
+def sync_gentoo():
+    """Syncronize Gentoo data from packages.gentoo.org API"""
+    sync_categories()
+
 if __name__ == '__main__':
     manager.run()

diff --git a/requirements.txt b/requirements.txt
index eaf59ef..78e4b2b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,2 +1,4 @@
 Flask
+Flask-SQLAlchemy
 Flask-Script  #manage.py
+requests


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-09-06 21:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-06 21:21 [gentoo-commits] proj/grumpy:master commit in: backend/lib/, /, backend/ Mart Raudsepp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox