diff options
author | 2016-09-07 00:19:29 +0300 | |
---|---|---|
committer | 2016-09-07 00:20:02 +0300 | |
commit | 6d5b0a5ba688677a127d1df1439080482c9709d1 (patch) | |
tree | 52722eb53bd8d53f440ffedad08c2722e6bcf702 /backend | |
parent | Initial start of new Project Grumpy code; Hello World! (diff) | |
download | grumpy-6d5b0a5ba688677a127d1df1439080482c9709d1.tar.gz grumpy-6d5b0a5ba688677a127d1df1439080482c9709d1.tar.bz2 grumpy-6d5b0a5ba688677a127d1df1439080482c9709d1.zip |
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
Diffstat (limited to 'backend')
-rw-r--r-- | backend/__init__.py | 15 | ||||
-rw-r--r-- | backend/lib/models.py | 28 | ||||
-rw-r--r-- | backend/lib/sync.py | 21 |
3 files changed, 63 insertions, 1 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() |