Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3586b58

Browse files
aaldaberaaldaber
aaldaber
authored and
aaldaber
committed
Fixes to db sharing mechanism
Added logic to avoid adding data to existing db when sharing the database
1 parent 8b90951 commit 3586b58

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

‎scrapyproject/views.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def main_page(request):
9797
singleproject['name'] = project.project_name
9898
userprojects.append(singleproject)
9999
for dataset in datasets:
100-
databases += dataset['database']
100+
databases += dataset.database
101101
return render(request, template_name="mainpage.html",
102102
context={'username': request.user.username, 'projects': userprojects, 'databases': databases})
103103

@@ -1497,6 +1497,18 @@ def sharing_db(dbname, target_user, projectname, username):
14971497
mongouri = "mongodb://" + settings.MONGODB_USER + ":" + quote(
14981498
settings.MONGODB_PASSWORD) + "@" + settings.MONGODB_URI + "/admin"
14991499
connection = MongoClient(mongouri)
1500+
existing_dbs = connection.database_names()
1501+
checked_all_database_names = 0
1502+
db_version = 1
1503+
1504+
while not checked_all_database_names:
1505+
checked_all_database_names = 1
1506+
for dbname in existing_dbs:
1507+
if str(dbname) == target_db_name:
1508+
target_db_name += str(db_version)
1509+
db_version += 1
1510+
checked_all_database_names = 0
1511+
existing_dbs = connection.database_names()
15001512

15011513
database = connection[dbname]
15021514

@@ -1519,13 +1531,18 @@ def sharing_db(dbname, target_user, projectname, username):
15191531
insertcol = connection[target_db_name][collection_name]
15201532
skip = 0
15211533
collection = col.find(filter={}, projection={'_id': False}, limit=100, skip=skip*100)
1522-
while collection.count() > 0:
1534+
1535+
items = []
1536+
for item in collection:
1537+
items.append(item)
1538+
1539+
while len(items) > 0:
15231540
skip += 1
1541+
insertcol.insert_many(items)
1542+
collection = col.find(filter={}, projection={'_id': False}, limit=100, skip=skip * 100)
15241543
items = []
15251544
for item in collection:
15261545
items.append(item)
1527-
insertcol.insert_many(items)
1528-
collection = col.find(filter={}, projection={'_id': False}, limit=100, skip=skip * 100)
15291546

15301547
connection.admin.command('grantRolesToUser', target_user,
15311548
roles=[{'role': 'dbOwner', 'db': target_db_name}])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /