aboutsummaryrefslogtreecommitdiff
path: root/wqflask/maintenance/set_resource_defaults.py
blob: abd5416c715a8b9db0a0d06596029313e589856a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""

Script that sets default resource access masks for use with the DB proxy

Defaults will be:
Owner - omni_gn
Mask  - Public/non-confidential: { data: "view",
                                   metadata: "view",
                                   admin: "not-admin" }
        Private/confidentia:     { data: "no-access",
                                   metadata: "no-access",
                                   admin: "not-admin" }

To run:
./bin/genenetwork2 ~/my_settings.py -c ./wqflask/maintenance/gen_select_dataset.py

"""

from __future__ import print_function, division

import sys
import json

# NEW: Note we prepend the current path - otherwise a guix instance of GN2 may be used instead
sys.path.insert(0, './')

# NEW: import app to avoid a circular dependency on utility.tools
from wqflask import app

from utility import hmac
from utility.tools import SQL_URI
from utility.redis_tools import get_redis_conn, get_user_id, add_resource, get_resources, get_resource_info
Redis = get_redis_conn()

import MySQLdb

import urllib.parse

from utility.logger import getLogger
logger = getLogger(__name__)

def parse_db_uri():
    """Converts a database URI to the db name, host name, user name, and password"""

    parsed_uri = urllib.parse.urlparse(SQL_URI)

    db_conn_info = dict(
                        db = parsed_uri.path[1:],
                        host = parsed_uri.hostname,
                        user = parsed_uri.username,
                        passwd = parsed_uri.password)

    print(db_conn_info)
    return db_conn_info

def insert_probeset_resources(default_owner_id):
    current_resources = Redis.hgetall("resources")
    Cursor.execute("""  SELECT
                            ProbeSetFreeze.Id, ProbeSetFreeze.Name, ProbeSetFreeze.confidentiality, ProbeSetFreeze.public
                        FROM
                            ProbeSetFreeze""")

    resource_results = Cursor.fetchall()
    for i, resource in enumerate(resource_results):
        resource_ob = {}
        resource_ob['name'] = resource[1]
        resource_ob['owner_id'] = default_owner_id
        resource_ob['data'] = { "dataset" : str(resource[0])}
        resource_ob['type'] = "dataset-probeset"
        if resource[2] < 1 and resource[3] > 0:
            resource_ob['default_mask'] = { "data": "view",
                                            "metadata": "view",
                                            "admin": "not-admin"}
        else:
            resource_ob['default_mask'] = { "data": "no-access",
                                            "metadata": "no-access",
                                            "admin": "not-admin"}
        resource_ob['group_masks'] = {}

        add_resource(resource_ob, update=False)

def insert_publish_resources(default_owner_id):
    current_resources = Redis.hgetall("resources")
    Cursor.execute("""  SELECT 
                            PublishXRef.Id, PublishFreeze.Id, InbredSet.InbredSetCode
                        FROM
                            PublishXRef, PublishFreeze, InbredSet, Publication
                        WHERE
                            PublishFreeze.InbredSetId = PublishXRef.InbredSetId AND
                            InbredSet.Id = PublishXRef.InbredSetId AND
                            Publication.Id = PublishXRef.PublicationId""")

    resource_results = Cursor.fetchall()
    for resource in resource_results:
        if resource[2]:
            resource_ob = {}
            if resource[2]:
                resource_ob['name'] = resource[2] + "_" + str(resource[0])
            else:
                resource_ob['name'] = str(resource[0])
            resource_ob['owner_id'] = default_owner_id
            resource_ob['data'] = { "dataset" : str(resource[1]) ,
                                    "trait"   : str(resource[0])}
            resource_ob['type'] = "dataset-publish"
            resource_ob['default_mask'] = { "data": "view",
                                            "metadata": "view",
                                            "admin": "not-admin"}

            resource_ob['group_masks'] = {}

            add_resource(resource_ob, update=False)
        else:
            continue

def insert_geno_resources(default_owner_id):
    current_resources = Redis.hgetall("resources")
    Cursor.execute("""  SELECT
                            GenoFreeze.Id, GenoFreeze.ShortName, GenoFreeze.confidentiality
                        FROM
                            GenoFreeze""")

    resource_results = Cursor.fetchall()
    for i, resource in enumerate(resource_results):
        resource_ob = {}
        resource_ob['name'] = resource[1]
        if resource[1] == "HET3-ITPGeno":
            resource_ob['owner_id'] = "c5ce8c56-78a6-474f-bcaf-7129d97f56ae"
        else:
            resource_ob['owner_id'] = default_owner_id
        resource_ob['data'] = { "dataset" : str(resource[0]) }
        resource_ob['type'] = "dataset-geno"
        if resource[2] < 1:
            resource_ob['default_mask'] = { "data": "view",
                                            "metadata": "view",
                                            "admin": "not-admin"}
        else:
            resource_ob['default_mask'] = { "data": "no-access",
                                            "metadata": "no-access",
                                            "admin": "not-admin"}
        resource_ob['group_masks'] = {}

        add_resource(resource_ob, update=False)

def insert_resources(default_owner_id):
    current_resources = get_resources()
    print("START")
    insert_publish_resources(default_owner_id)
    print("AFTER PUBLISH")
    insert_geno_resources(default_owner_id)
    print("AFTER GENO")
    insert_probeset_resources(default_owner_id)
    print("AFTER PROBESET")

def main():
    """Generates and outputs (as json file) the data for the main dropdown menus on the home page"""

    Redis.delete("resources")

    owner_id = "c5ce8c56-78a6-474f-bcaf-7129d97f56ae"

    insert_resources(owner_id)

if __name__ == '__main__':
    Conn = MySQLdb.Connect(**parse_db_uri())
    Cursor = Conn.cursor()
    main()