Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1270: Signbank empty database. #1316

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion signbank/dictionary/adminviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -4170,6 +4170,7 @@ def render_to_add_user_response(self, context):

if 'add_view_perm' in self.request.GET:
manage_identifier += '_manage_view'

if dataset_object in datasets_user_can_view:
if user_object.is_staff or user_object.is_superuser:
messages.add_message(self.request, messages.INFO,
Expand Down Expand Up @@ -4257,7 +4258,6 @@ def render_to_add_user_response(self, context):

if 'delete_view_perm' in self.request.GET:
manage_identifier += '_manage_view'

if dataset_object in datasets_user_can_view:
if user_object.is_staff or user_object.is_superuser:
messages.add_message(self.request, messages.ERROR,
Expand Down
71 changes: 71 additions & 0 deletions signbank/dictionary/management/commands/create_empty_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings

from os.path import isfile
from sqlite3 import connect
from shutil import copyfile, move

def make_db_small(filename):
conn = connect(filename,isolation_level=None)
c = conn.cursor()

# Select all tables in dataset
sql_query = """SELECT name FROM sqlite_master WHERE type='table';"""
c.execute(sql_query)

# Set which tables should not be emptied
keep_tables = ['auth_group', 'auth_permissions',
'dictionary_corpus', 'dictionary_dataset', 'dictionary_dataset_translation_languages',
'dictionary_derivationhistory', 'dictionary_derivationhistorytranslation', 'dictionary_dialect',
'dictionary_fieldchoice', 'dictionary_handshape', 'dictionary_language',
'dictionary_semanticfield', 'dictionary_semanticfieldtranslation',
'dictionary_signlanguage', 'dictionary_content_type',
'django_migrations',
'pages_page', 'south_migrationhistory', 'tagging_tag',
'auth_group_permissions',
'auth_user_groups',
'auth_user_user_permissions',
'auth_permission',
'auth_user',
'registration_registrationprofile',
'registration_userprofile',
'pages_page_group_required',
'django_site',
'guardian_groupobjectpermission',
'guardian_userobjectpermission',
'dictionary_userprofile',
'django_content_type',
'sqlite_sequence'
]

# Empty all other tables
for table in c.fetchall():
if table[0] not in keep_tables:
print('Delete contents from table: ', table[0])
c.execute('DELETE FROM ' + table[0] + ';')

c.execute('VACUUM')
conn.commit()
conn.close()


class Command(BaseCommand):
help = 'Creates a smaller faster database for unit tests'

def handle(self, *args, **options):
# find databases
source_db = settings.DATABASES['default']['NAME']
test_db_filename = settings.DATABASES['default']['TEST']['NAME']

SMALL = True

if isfile(test_db_filename):
self.stdout.write('Making backup of old test database')
move(test_db_filename, test_db_filename + '_save')

self.stdout.write('Copying database file')
copyfile(source_db, test_db_filename)

if SMALL:
self.stdout.write('Emptying tables, for faster tests')
make_db_small(test_db_filename)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.core.management.base import BaseCommand
from signbank.dictionary.models import Dataset, Gloss, LemmaIdgloss


class Command(BaseCommand):

help = 'remove users except for development'
args = ''

def handle(self, *args, **options):

all_datasets = Dataset.objects.all().distinct()

for dataset in all_datasets:

if dataset.acronym not in ['tstMH']:
dataset.delete()

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.core.management.base import BaseCommand
from signbank.dictionary.models import User, UserProfile

class Command(BaseCommand):

help = 'remove users except for development'
args = ''

def handle(self, *args, **options):

all_users = User.objects.all().distinct()
if not all_users.count():
return

for user in all_users:

if user.username not in ['susanodd', 'wessel', 'AnonymousUser',
'DivyaKanekal', 'jetske',
'micha']:

userprofile = UserProfile.objects.filter(user=user)
if userprofile:
userprofile.delete()
user.delete()
12 changes: 7 additions & 5 deletions signbank/dictionary/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db.models import CharField, TextField
#from django.forms import TextInput, Textarea, CharField
from signbank.dictionary.adminviews import *
from signbank.dictionary.forms import GlossCreateForm, FieldChoiceForm
from signbank.dictionary.models import *
Expand All @@ -18,7 +17,7 @@
from pathlib import Path
from os import path

from guardian.shortcuts import assign_perm
from guardian.shortcuts import assign_perm, get_user_perms

from signbank.video.models import GlossVideo
from signbank.dictionary.views import gloss_api_get_sign_name_and_media_info
Expand Down Expand Up @@ -165,7 +164,6 @@ def test_createGloss(self):
# Get the test dataset
dataset_name = settings.DEFAULT_DATASET
test_dataset = Dataset.objects.get(name=dataset_name)

# Construct the Create Gloss form data
create_gloss_form_data = {'dataset': test_dataset.id, 'select_or_new_lemma': "new"}
for language in test_dataset.translation_languages.all():
Expand Down Expand Up @@ -1371,19 +1369,20 @@ def test_User_is_dataset_manager(self):
response = self.client.get(reverse('admin_dataset_manager'), form_data, follow=True)
self.assertContains(response, 'Change permission for user successfully granted.'
.format(self.user2.username))
self.assertEqual(response.status_code, 200)

# Revoke change permission
form_data = {'dataset_acronym': self.test_dataset.acronym, 'username': self.user2.username,
'delete_change_perm': 'Revoke'}
response = self.client.get(reverse('admin_dataset_manager'), form_data, follow=True)
self.assertContains(response, 'Change permission for user successfully revoked.'
.format(self.user2.username))

self.assertEqual(response.status_code, 200)

def test_Set_default_language(self):
"""
Tests
:return:
:return:
"""
logged_in = self.client.login(username='test-user', password='test-user')
self.assertTrue(logged_in)
Expand All @@ -1401,6 +1400,7 @@ def test_Set_default_language(self):
assign_perm('change_dataset', self.user, self.test_dataset)
response = self.client.get(reverse('admin_dataset_manager'), form_data, follow=True)
self.assertContains(response, 'The default language of')
self.assertEqual(response.status_code, 200)

# Try to add a language that is not in the translation language set of the test dataset
language = Language(name="nonexistingtestlanguage", language_code_2char="ts", language_code_3char='tst')
Expand All @@ -1409,6 +1409,8 @@ def test_Set_default_language(self):
response = self.client.get(reverse('admin_dataset_manager'), form_data, follow=True)
self.assertContains(response, '{} is not in the set of languages of dataset {}.'.format(
language.name, self.test_dataset.acronym))
self.assertEqual(response.status_code, 200)


class LemmaTests(TestCase):

Expand Down
4 changes: 1 addition & 3 deletions signbank/dictionary/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def add_gloss(request):
selected_datasets = Dataset.objects.filter(pk=request.POST['dataset'])
else:
selected_datasets = get_selected_datasets_for_user(request.user)

dataset_languages = Language.objects.filter(dataset__in=selected_datasets).distinct()

if dataset:
Expand Down Expand Up @@ -155,7 +156,6 @@ def add_gloss(request):
for ua in user_affiliations:
new_affiliation, created = AffiliatedGloss.objects.get_or_create(affiliation=ua.affiliation,
gloss=gloss)

except ValidationError as ve:
return show_error(request, ve.message, form, dataset_languages)

Expand Down Expand Up @@ -839,7 +839,6 @@ def update_gloss(request, glossid):

return HttpResponse(str(newvalue), {'content-type': 'text/plain'})

import guardian
if ds in guardian.shortcuts.get_objects_for_user(request.user, ['view_dataset'],
Dataset, any_perm=True):
newvalue = value
Expand Down Expand Up @@ -2493,7 +2492,6 @@ def update_morpheme(request, morphemeid):

return HttpResponse(str(newvalue), {'content-type': 'text/plain'})

import guardian
if ds in guardian.shortcuts.get_objects_for_user(request.user, ['view_dataset'],
Dataset, any_perm=True):
newvalue = value
Expand Down
1 change: 1 addition & 0 deletions signbank/registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def users_without_dataset(request):
continue

user = User.objects.get(pk=int(user.split('_')[-1]))

assign_perm('view_dataset', user, main_dataset)

users_with_access.append(user.first_name + ' ' + user.last_name)
Expand Down