Skip to content

Commit

Permalink
Merge pull request #1007 from onaio/cache
Browse files Browse the repository at this point in the history
upgrade to django 1.11
  • Loading branch information
ukanga authored Apr 26, 2017
2 parents 0e4c82d + e603da2 commit 48f3532
Show file tree
Hide file tree
Showing 68 changed files with 635 additions and 548 deletions.
51 changes: 25 additions & 26 deletions onadata/apps/api/management/commands/migrate_group_permissions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.core.management.base import BaseCommand
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.db.models import Count
from django.db.models.loading import get_model
from django.db.utils import IntegrityError
from django.utils.translation import gettext as _
from optparse import make_option
from django.db.models.loading import get_model
from guardian.models import GroupObjectPermissionBase

from onadata.apps.api.models import Team
Expand All @@ -13,20 +12,24 @@

class Command(BaseCommand):
help = _(u"Migrate group permissions")
option_list = BaseCommand.option_list + (
make_option('--model', '-m',
action='store_true',
dest='app_model',
default=False,
help='The model the permission belong too.'
' (app.model format)'),
make_option('--perm-table', '-p',
action='store_true',
dest='perms_tbl',
default=False,
help='The new model permission are stored in'
' (app.model format)'),
)

def add_arguments(self, parser):
parser.add_argument(
'--model',
'-m',
action='store_true',
dest='app_model',
default=False,
help='The model the permission belong too.'
' (app.model format)')
parser.add_argument(
'--perm-table',
'-p',
action='store_true',
dest='perms_tbl',
default=False,
help='The new model permission are stored in'
' (app.model format)')

def handle(self, *args, **options):
self.stdout.write("Migrate group permissions started", ending='\n')
Expand Down Expand Up @@ -58,24 +61,20 @@ def handle(self, *args, **options):
"a subclass of GroupObjectPermissionBase")
exit()

ct = ContentType.objects.get(model=model.__name__.lower(),
app_label=model._meta.app_label)
ct = ContentType.objects.get(
model=model.__name__.lower(), app_label=model._meta.app_label)
teams = Team.objects.filter().annotate(
c=Count('groupobjectpermission')
).filter(c__gt=0)
c=Count('groupobjectpermission')).filter(c__gt=0)
for team in queryset_iterator(teams):
self.stdout.write(
"Processing: {} - {}".format(team.pk, team.name)
)
self.stdout.write("Processing: {} - {}".format(team.pk, team.name))
for gop in team.groupobjectpermission_set.filter(content_type=ct)\
.select_related('permission', 'content_type')\
.prefetch_related('permission', 'content_type'):
try:
perms_model(
content_object=gop.content_object,
group=team,
permission=gop.permission
).save()
permission=gop.permission).save()
except IntegrityError:
continue
except ValueError:
Expand Down
53 changes: 27 additions & 26 deletions onadata/apps/api/management/commands/migrate_permissions.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.db.models.loading import get_model
from django.db.utils import IntegrityError
from django.utils.translation import gettext as _
from django.conf import settings
from optparse import make_option
from django.db.models.loading import get_model
from guardian.models import UserObjectPermissionBase

from onadata.libs.utils.model_tools import queryset_iterator


class Command(BaseCommand):
help = _(u"Migrate permissions")
option_list = BaseCommand.option_list + (
make_option('--model', '-m',
action='store_true',
dest='app_model',
default=False,
help='The model the permission belong too.'
' (app.model format)'),
make_option('--perm-table', '-p',
action='store_true',
dest='perms_tbl',
default=False,
help='The new model permission are stored in'
' (app.model format)'),
)

def add_arguments(self, parser):
parser.add_argument(
'--model',
'-m',
action='store_true',
dest='app_model',
default=False,
help='The model the permission belong too.'
' (app.model format)')
parser.add_argument(
'--perm-table',
'-p',
action='store_true',
dest='perms_tbl',
default=False,
help='The new model permission are stored in'
' (app.model format)')

def handle(self, *args, **options):
self.stdout.write("Migrate permissions started", ending='\n')
Expand Down Expand Up @@ -58,27 +61,25 @@ def handle(self, *args, **options):
"a subclass of UserObjectPermissionBase")
exit()

ct = ContentType.objects.get(model=model.__name__.lower(),
app_label=model._meta.app_label)
ct = ContentType.objects.get(
model=model.__name__.lower(), app_label=model._meta.app_label)

# Get all the users
users = User.objects.exclude(
username__iexact=settings.ANONYMOUS_DEFAULT_USERNAME
).order_by('username')
username__iexact=settings.ANONYMOUS_DEFAULT_USERNAME).order_by(
'username')

for user in queryset_iterator(users):
self.stdout.write(
"Processing: {} - {}".format(user.pk, user.username)
)
"Processing: {} - {}".format(user.pk, user.username))
for uop in user.userobjectpermission_set.filter(content_type=ct)\
.select_related('permission', 'content_type')\
.prefetch_related('permission', 'content_type'):
try:
perms_model(
content_object=uop.content_object,
user=user,
permission=uop.permission
).save()
permission=uop.permission).save()
except IntegrityError:
continue
except ValueError:
Expand Down
19 changes: 10 additions & 9 deletions onadata/apps/api/management/commands/regenerate_auth_tokens.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.utils.translation import gettext as _
from optparse import make_option
from rest_framework.authtoken.models import Token


class Command(BaseCommand):
help = _(u"Regenerate Authentication Tokens")
option_list = BaseCommand.option_list + (
make_option('--users', '-u',
action='store_true',
dest='users',
default=False,
help='Users to Regenerate Tokens'),
)

def add_arguments(self, parser):
parser.add_argument(
'--users',
'-u',
action='store_true',
dest='users',
default=False,
help='Users to Regenerate Tokens')

def handle(self, *args, **options):
ALL = "all"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_user_profile_list_with_and_without_users_param(self):
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 2)
self.assertEqual(
response.data,
[dict(i) for i in response.data],
[self.user_profile_data(), deno_profile_data]
)

Expand Down
43 changes: 37 additions & 6 deletions onadata/apps/api/tests/viewsets/test_xform_list_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ def test_get_xform_list_other_user_with_dataentry_role(self):
'text/xml; charset=utf-8')

def test_retrieve_xform_xml(self):
self.view = XFormListViewSet.as_view({"get": "retrieve"})
self.view = XFormListViewSet.as_view(
{
"get": "retrieve",
"head": "retrieve"
}
)
request = self.factory.head('/')
response = self.view(request, pk=self.xform.pk)
auth = DigestAuth('bob', 'bobbob')
Expand Down Expand Up @@ -404,8 +409,14 @@ def _load_metadata(self, xform=None):

def test_retrieve_xform_manifest(self):
self._load_metadata(self.xform)
self.view = XFormListViewSet.as_view({"get": "manifest"})
self.view = XFormListViewSet.as_view(
{
"get": "manifest",
"head": "manifest"
}
)
request = self.factory.head('/')

response = self.view(request, pk=self.xform.pk)
auth = DigestAuth('bob', 'bobbob')
request = self.factory.get('/')
Expand Down Expand Up @@ -467,7 +478,12 @@ def test_retrieve_xform_manifest_anonymous_user_require_auth(self):

def test_retrieve_xform_media(self):
self._load_metadata(self.xform)
self.view = XFormListViewSet.as_view({"get": "media"})
self.view = XFormListViewSet.as_view(
{
"get": "media",
"head": "media"
}
)
request = self.factory.head('/')
response = self.view(
request, pk=self.xform.pk, metadata=self.metadata.pk, format='png')
Expand Down Expand Up @@ -511,7 +527,12 @@ def test_retrieve_xform_media_linked_xform(self):
self._make_submissions()
self.xform.reload()

self.view = XFormListViewSet.as_view({"get": "manifest"})
self.view = XFormListViewSet.as_view(
{
"get": "manifest",
"head": "manifest"
}
)
request = self.factory.head('/')
response = self.view(request, pk=self.xform.pk)
auth = DigestAuth('bob', 'bobbob')
Expand All @@ -524,7 +545,12 @@ def test_retrieve_xform_media_linked_xform(self):
response.data[0]['hash'], 'md5:%s' %
md5(self.xform.last_submission_time.isoformat()).hexdigest())

self.view = XFormListViewSet.as_view({"get": "media"})
self.view = XFormListViewSet.as_view(
{
"get": "media",
"head": "media"
}
)
request = self.factory.get('/')
response = self.view(
request, pk=self.xform.pk, metadata=self.metadata.pk, format='csv')
Expand All @@ -549,7 +575,12 @@ def test_retrieve_xform_manifest_linked_form(self):
data_value = 'xform {} transportation'.format(self.xform.pk)
media = self._add_form_metadata(self.xform, data_type, data_value)

self.view = XFormListViewSet.as_view({"get": "manifest"})
self.view = XFormListViewSet.as_view(
{
"get": "manifest",
"head": "manifest"
}
)

# sign in bob
request = self.factory.head('/')
Expand Down
9 changes: 5 additions & 4 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from django.core.files.storage import default_storage
from django.test.utils import override_settings
from django.utils.dateparse import parse_datetime
from django.utils.timezone import utc
from django_digest.test import DigestAuth
from httmock import urlmatch, HTTMock
from mock import patch, Mock
Expand Down Expand Up @@ -3599,7 +3600,7 @@ def _get_date_filtered_export(self, query_str):

def test_csv_export_filtered_by_date(self):
with HTTMock(enketo_mock):
start_date = datetime(2015, 12, 2)
start_date = datetime(2015, 12, 2, tzinfo=utc)
self._make_submission_over_date_range(start_date)

first_datetime = start_date.strftime(MONGO_STRFTIME)
Expand Down Expand Up @@ -3628,7 +3629,7 @@ def test_csv_export_filtered_by_date(self):

def test_previous_export_with_date_filter_is_returned(self):
with HTTMock(enketo_mock):
start_date = datetime(2015, 12, 2)
start_date = datetime(2015, 12, 2, tzinfo=utc)
self._make_submission_over_date_range(start_date)

first_datetime = start_date.strftime(MONGO_STRFTIME)
Expand Down Expand Up @@ -3701,7 +3702,7 @@ def test_download_export_with_invalid_export_id(self):

def test_normal_export_after_export_with_date_filter(self):
with HTTMock(enketo_mock):
start_date = datetime(2015, 12, 2)
start_date = datetime(2015, 12, 2, tzinfo=utc)
self._make_submission_over_date_range(start_date)

first_datetime = start_date.strftime(MONGO_STRFTIME)
Expand Down Expand Up @@ -3854,7 +3855,7 @@ def test_csv_exports_w_images_link(self):
"fixtures", "tutorial", "instances",
"uuid1", 'submission.xml'),
media_file=f,
forced_submission_time=datetime(2015, 12, 2))
forced_submission_time=datetime(2015, 12, 2, tzinfo=utc))

attachment_id = Attachment.objects.all().last().pk

Expand Down
14 changes: 8 additions & 6 deletions onadata/apps/api/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,22 +470,24 @@ def check_inherit_permission_from_project(xform_id, user):
return

# get the project_xform
xforms = XForm.objects.filter(pk=xform_id).select_related('project')
xform = XForm.objects.filter(pk=xform_id).select_related('project').only(
'project_id', 'id'
).first()

if not xforms:
if not xform:
return

# ignore if forms has meta perms set
if xforms[0].metadata_set.filter(data_type='xform_meta_perms'):
if xform.metadata_set.filter(data_type='xform_meta_perms'):
return

# get and compare the project role to the xform role
project_role = get_role_in_org(user, xforms[0].project)
xform_role = get_role_in_org(user, xforms[0])
project_role = get_role_in_org(user, xform.project)
xform_role = get_role_in_org(user, xform)

# if diff set the project role to the xform
if xform_role != project_role:
_set_xform_permission(project_role, user, xforms[0])
_set_xform_permission(project_role, user, xform)


def _set_xform_permission(role, user, xform):
Expand Down
Loading

0 comments on commit 48f3532

Please sign in to comment.