Skip to content

Commit

Permalink
Remove extraneous spreadsheet class
Browse files Browse the repository at this point in the history
Related to issue #23. This commit also removes permission checking
(done in in TorqueDataConnect extension).
  • Loading branch information
YaxelPerez committed Aug 4, 2020
1 parent 68a43e3 commit 5d184fb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 68 deletions.
64 changes: 1 addition & 63 deletions torquedata/torquedata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,12 @@
import os
import pickle
import json
from dataclasses import dataclass
from collections import UserDict
from whoosh import index
from whoosh.index import create_in
from whoosh.fields import *

app = Flask(__name__)

# explanation: since this class inherits from UserDict, it behaves pretty much
# exactly like a dict which means old code doesn't break but new code can use
# the methods attached to this class
class Spreadsheet(UserDict):
@classmethod
def read(cls, name):
"""Load an existing spreadsheet"""
raise NotImplementedError

@classmethod
def write(cls):
"""Save spreadsheet to filesystem"""
raise NotImplementedError

def __init__(self, name, object_name, key_column, columns=[]):
self.name = name
self.object_name = object_name
self.key_column = key_column
self.columns = columns

super().__init__()

def apply_permissions(self, permissions):
"""
Return a new spreadsheet with only data that is allowed by a given
permissions object
"""
new_data = {}
# cull invalid rows, columns
for key, entry in self.data.items():
if 'valid_ids' in permissions and key not in permissions['valid_ids']:
continue
if 'columns' in permissions:
new_entry = {}
for column, value in entry.items():
if column not in permissions['columns']:
continue
new_entry[column] = value
new_data[key] = new_entry
else:
new_data[key] = entry

new_sheet = Spreadsheet(
name=self.name,
object_name=self.object_name,
key_column=self.key_column,
columns=permissions.get('columns', None) or self.columns
)
new_sheet.data = new_data
return new_sheet

def json(self):
return json.dumps({self.name: self.data.values()})

try:
app.config.from_object('config')
except:
Expand Down Expand Up @@ -152,12 +96,7 @@ def index_search(group, sheet_name, wiki_key):


def load_sheet(sheet_name):
data[sheet_name] = Spreadsheet(
name=sheet_name,
object_name=sheet_config[sheet_name]['object_name'],
key_column=sheet_config[sheet_name]['key_column'],
columns=[]
)
data[sheet_name] = {}
reader = csv.reader(
open(os.path.join(app.config.get("SPREADSHEET_FOLDER"), sheet_name, sheet_name + ".csv"), encoding='utf-8'),
delimiter=',',
Expand All @@ -175,7 +114,6 @@ def load_sheet(sheet_name):
for row in reader:
o = {}
for (field, column_type, cell) in zip(header, column_types, row):
data[sheet_name].columns.append(field)
if column_type == 'list':
# This may be reversed as a decision at some point, but the empty cell
# from the csv comes through as the empty string, meaning that the user
Expand Down
10 changes: 5 additions & 5 deletions torquedata/torquedata/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ def search(sheet_name):

@app.route('/api/sheets')
def sheets():
""" Return a summary of all the sheets """
"""Return a summary of all the sheets"""
group = request.args.get("group")
wiki_key = request.args.get("wiki_key")

response = []
for sheet_name, sheet in data.items():
sheet = sheet.apply_permissions(permissions[sheet_name][wiki_key][group])
response.append({
"name": sheet_name,
"object_name": sheet.object_name,
"key_column": sheet.key_column,
"columns": sheet.columns
"object_name": sheet_config[sheet_name]["object_name"],
"key_column": sheet_config[sheet_name]["key_column"],
"columns": list(next(iter(sheet.values())))
})
return json.dumps(response)


@app.route('/api/<sheet_name>.<fmt>')
def sheet(sheet_name, fmt):
group = request.args.get("group")
Expand Down

0 comments on commit 5d184fb

Please sign in to comment.