Skip to content

Instantly share code, notes, and snippets.

@mmulich
Created March 27, 2014 14:56
Show Gist options
  • Save mmulich/9809378 to your computer and use it in GitHub Desktop.
Save mmulich/9809378 to your computer and use it in GitHub Desktop.
diff --git a/cnxpublishing/db.py b/cnxpublishing/db.py
index d702f41..eeb1c77 100644
--- a/cnxpublishing/db.py
+++ b/cnxpublishing/db.py
@@ -103,6 +103,17 @@ update pending_documents set license_accepted = 't'
where id = %s""", (document_id,))
+def add_pending_resource(cursor, resource):
+ """Adds a pending resource to the database."""
+ args = (psycopg2.Binary(resource.data), resource.media_type,)
+ cursor.execute("""\
+INSERT INTO pending_resources ("data", "media_type")
+VALUES (%s, %s) RETURNING "hash"
+""", args)
+ hash = cursor.fetchone()[0]
+ return hash
+
+
def add_pending_document(cursor, publication_id, document):
"""Adds a document that is awaiting publication to the database."""
uri = document.get_uri('cnx-archive')
diff --git a/cnxpublishing/publish.py b/cnxpublishing/publish.py
index 1a6b24d..197c6dc 100644
--- a/cnxpublishing/publish.py
+++ b/cnxpublishing/publish.py
@@ -185,7 +185,30 @@ def _insert_files(cursor, ident_hash, files):
This returns a list of filenames and hashes.
"""
- raise NotImplementedError()
+ module_file_values = []
+ file_values = []
+ hashes = {}
+ params = {'ident_hash': ident_hash}
+ for i, file_ in enumerate(files):
+ file_values.append('(%(document{})s)'.format(i))
+ params['document{}'.format(i)] = memoryview(file_['data'])
+
+ module_file_values.append("""\
+SELECT {row} AS row_number,
+%(filename{i})s AS filename,
+%(mimetype{i})s AS mimetype"""
+ .format(row=i+1, i=i))
+ params['filename{}'.format(i)] = file_['filename']
+ params['mimetype{}'.format(i)] = file_['mimetype']
+
+ if file_values:
+ stmt = MODULE_FILES_INSERTION_TEMPLATE.format(**{
+ 'file_values': ','.join(file_values),
+ 'module_file_values_sql': ' UNION ALL '.join(module_file_values),
+ })
+ cursor.execute(stmt, params)
+ hashes = dict(list(cursor.fetchall()))
+ return hashes
def _insert_tree(cursor, tree, parent_id=None, index=0):
@@ -202,8 +225,8 @@ def publish_model(cursor, model, publisher, message):
.format(len(publishers), publishers))
ident_hash = _insert_metadata(cursor, model, publisher, message)
if isinstance(model, Document):
- files = [] # TODO
- # file_hashes = _insert_files(cursor, ident_hash, files)
+ files = model.resources
+ file_hashes = _insert_files(cursor, ident_hash, files)
elif isinstance(model, Binder):
tree = {} # TODO
# tree = _insert_tree(cursor, tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment