def test_acl(self): """ Test for ACL handling. """ # Create user to verify ACL user = User.objects.create_user('testuser', '*****@*****.**', 'testpassword') # Create project project = self.create_project() # Enable ACL project.enable_acl = True project.save() # Check user does not have access self.assertFalse(can_access_project(user, project)) # Add to ACL group user.groups.add(Group.objects.get(name='Test@Translate')) # Need to fetch user again to clear permission cache user = User.objects.get(username='******') # We now should have access self.assertTrue(can_access_project(user, project))
def test_acl(self): """Test for ACL handling.""" # Create user to verify ACL user = User.objects.create_user( 'testuser', '*****@*****.**', 'testpassword' ) # Create project project = self.create_project() # Enable ACL project.enable_acl = True project.save() # Check user does not have access self.assertFalse(can_access_project(user, project)) # Add to ACL group user.groups.add(Group.objects.get(name='Test@Translate')) # Need to fetch user again to clear permission cache user = User.objects.get(username='******') # We now should have access self.assertTrue(can_access_project(user, project))
def notify_user(self, notification, subproject, display_obj, context=None, headers=None, user=None): ''' Wrapper for sending notifications to user. ''' from weblate.permissions.helpers import can_access_project if context is None: context = {} if headers is None: headers = {} # Check whether user is still allowed to access this project if can_access_project(self.user, subproject.project): # Generate notification return get_notification_email(self.language, self.user.email, notification, display_obj, context, headers, user=user)
def auto_translate(user, translation, source, inconsistent, overwrite, check_acl=True): """Perform automatic translation based on other components.""" updated = 0 if inconsistent: units = translation.unit_set.filter_type( 'check:inconsistent', translation.subproject.project, translation.language, ) elif overwrite: units = translation.unit_set.all() else: units = translation.unit_set.filter(state__lt=STATE_TRANSLATED, ) sources = Unit.objects.filter( translation__language=translation.language, state__gte=STATE_TRANSLATED, ) if source: subprj = SubProject.objects.get(id=source) if check_acl and not can_access_project(user, subprj.project): raise PermissionDenied() sources = sources.filter(translation__subproject=subprj) else: sources = sources.filter(translation__subproject__project=translation. subproject.project).exclude( translation=translation) # Filter by strings units = units.filter(source__in=sources.values('source')) translation.commit_pending(None) for unit in units.iterator(): with transaction.atomic(): # Get first matching entry update = sources.filter(source=unit.source)[0] # No save if translation is same if unit.state == update.state and unit.target == update.target: continue # Copy translation unit.state = update.state unit.target = update.target # Create signle change object for whole merge Change.objects.create(action=Change.ACTION_AUTO, unit=unit, user=user, author=user) # Save unit to backend unit.save_backend(None, False, False, user=user) updated += 1 return updated
def process_others(self, source, check_acl=True): """Perform automatic translation based on other components.""" sources = Unit.objects.filter( translation__language=self.translation.language, state__gte=STATE_TRANSLATED, ) if source: subprj = SubProject.objects.get(id=source) if check_acl and not can_access_project(self.user, subprj.project): raise PermissionDenied() sources = sources.filter(translation__subproject=subprj) else: project = self.translation.subproject.project sources = sources.filter( translation__subproject__project=project).exclude( translation=self.translation) # Filter by strings units = self.get_units().filter(source__in=sources.values('source')) self.translation.commit_pending(None) for unit in units.select_for_update().iterator(): # Get first matching entry update = sources.filter(source=unit.source)[0] # No save if translation is same if unit.state == update.state and unit.target == update.target: continue # Copy translation self.update(unit, update.state, update.target)
def auto_translate(user, translation, source, inconsistent, overwrite, check_acl=True): """Perform automatic translation based on other components.""" updated = 0 if inconsistent: units = translation.unit_set.filter_type( 'check:inconsistent', translation ) elif overwrite: units = translation.unit_set.all() else: units = translation.unit_set.filter(translated=False) sources = Unit.objects.filter( translation__language=translation.language, translated=True ) if source: subprj = SubProject.objects.get(id=source) if check_acl and not can_access_project(user, subprj.project): raise PermissionDenied() sources = sources.filter(translation__subproject=subprj) else: sources = sources.filter( translation__subproject__project=translation.subproject.project ).exclude( translation=translation ) # Filter by strings units = units.filter( source__in=sources.values('source') ) translation.commit_pending(None) for unit in units.iterator(): with transaction.atomic(): # Get first matching entry update = sources.filter(source=unit.source)[0] # No save if translation is same if unit.fuzzy == update.fuzzy and unit.target == update.target: continue # Copy translation unit.fuzzy = update.fuzzy unit.target = update.target # Create signle change object for whole merge Change.objects.create( action=Change.ACTION_AUTO, unit=unit, user=user, author=user ) # Save unit to backend unit.save_backend(None, False, False, user=user) updated += 1 return updated
def download_translations(self, source, language, text, unit, user): """Download list of possible translations from a service.""" matching_units = Unit.objects.more_like_this(unit) return [ self.format_unit_match(munit, 50) for munit in matching_units if can_access_project(user, munit.translation.subproject.project) ]
def download_translations(self, source, language, text, unit, user): ''' Downloads list of possible translations from a service. ''' matching_units = Unit.objects.same_source(unit) return [ format_unit_match(munit, 100) for munit in matching_units if can_access_project(user, munit.translation.subproject.project) ]
def test_acl(self): """Test for ACL handling.""" # Create user to verify ACL user = create_test_user() # Create project project = self.create_project() # Enable ACL project.access_control = Project.ACCESS_PRIVATE project.save() # Check user does not have access self.assertFalse(can_access_project(user, project)) # Add to ACL group user.groups.add(Group.objects.get(name='Test@Translate')) # Need to fetch user again to clear permission cache user = User.objects.get(username='******') # We now should have access self.assertTrue(can_access_project(user, project))
def send_user(profile, notification, subproject, display_obj, context=None, headers=None, user=None): """Wrapper for sending notifications to user.""" if context is None: context = {} if headers is None: headers = {} # Check whether user is still allowed to access this project if can_access_project(profile.user, subproject.project): # Generate notification return get_notification_email( profile.language, profile.user.email, notification, display_obj, context, headers, user=user )
def send_user(profile, notification, subproject, display_obj, context=None, headers=None, user=None): """Wrapper for sending notifications to user.""" if context is None: context = {} if headers is None: headers = {} # Check whether user is still allowed to access this project if can_access_project(profile.user, subproject.project): # Generate notification return get_notification_email( profile.language, profile.user.email, notification, display_obj, context, headers, user=user ) return None