aboutsummaryrefslogtreecommitdiff
path: root/gn2/maintenance/set_resource_defaults.py
blob: f9e5494a1274db77fd39d1a94a1dabd6c08d075a (about) (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
"""

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

"""

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 gn2.wqflask import app

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

import urllib.parse

from gn2.wqflask.database import database_connection


def insert_probeset_resources(cursor, 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(cursor, 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(cursor, 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(cursor, default_owner_id)
    print("AFTER PUBLISH")
    insert_geno_resources(cursor, default_owner_id)
    print("AFTER GENO")
    insert_probeset_resources(cursor, default_owner_id)
    print("AFTER PROBESET")


def main(cursor):
    """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__':
    with database_connection(get_setting("SQL_URI")) as conn:
        with conn.cursor() as cursor:
            main(cursor)