def check_xpi_info(xpi_info, addon=None): from olympia.addons.models import Addon, BlacklistedGuid guid = xpi_info['guid'] if amo.get_user(): deleted_guid_clashes = Addon.unfiltered.exclude( authors__id=amo.get_user().id).filter(guid=guid) else: deleted_guid_clashes = Addon.unfiltered.filter(guid=guid) if not guid: raise forms.ValidationError(_("Could not find an add-on ID.")) if not waffle.switch_is_active('allow-long-addon-guid') and len(guid) > 64: raise forms.ValidationError( _("Add-on ID must be 64 characters or less.")) if addon and addon.guid != guid: raise forms.ValidationError(_("Add-on ID doesn't match add-on.")) if (not addon and # Non-deleted add-ons. ( Addon.with_unlisted.filter(guid=guid).exists() or # BlacklistedGuid objects for legacy deletions. BlacklistedGuid.objects.filter(guid=guid).exists() or # Deleted add-ons that don't belong to the uploader. deleted_guid_clashes.exists())): raise forms.ValidationError(_('Duplicate add-on ID found.')) if len(xpi_info['version']) > 32: raise forms.ValidationError( _('Version numbers should have fewer than 32 characters.')) if not VERSION_RE.match(xpi_info['version']): raise forms.ValidationError( _('Version numbers should only contain letters, numbers, ' 'and these punctuation characters: +*.-_.')) return xpi_info
def check_xpi_info(xpi_info, addon=None): from olympia.addons.models import Addon, BlacklistedGuid guid = xpi_info['guid'] if amo.get_user(): deleted_guid_clashes = Addon.unfiltered.exclude( authors__id=amo.get_user().id).filter(guid=guid) else: deleted_guid_clashes = Addon.unfiltered.filter(guid=guid) if not guid: raise forms.ValidationError(_("Could not find an add-on ID.")) if not waffle.switch_is_active('allow-long-addon-guid') and len(guid) > 64: raise forms.ValidationError( _("Add-on ID must be 64 characters or less.")) if addon and addon.guid != guid: raise forms.ValidationError(_("Add-on ID doesn't match add-on.")) if (not addon and # Non-deleted add-ons. (Addon.with_unlisted.filter(guid=guid).exists() or # BlacklistedGuid objects for legacy deletions. BlacklistedGuid.objects.filter(guid=guid).exists() or # Deleted add-ons that don't belong to the uploader. deleted_guid_clashes.exists())): raise forms.ValidationError(_('Duplicate add-on ID found.')) if len(xpi_info['version']) > 32: raise forms.ValidationError( _('Version numbers should have fewer than 32 characters.')) if not VERSION_RE.match(xpi_info['version']): raise forms.ValidationError( _('Version numbers should only contain letters, numbers, ' 'and these punctuation characters: +*.-_.')) return xpi_info
def test_set_task_user(self): @decorators.set_task_user def some_func(): return get_user() set_user(UserProfile.objects.get(username='******')) assert get_user().pk == 999 assert some_func().pk == int(settings.TASK_USER_ID) assert get_user().pk == 999
def test_set_task_user(self): @decorators.set_task_user def some_func(): return get_user() set_user(UserProfile.objects.get(username='******')) eq_(get_user().pk, 999) eq_(some_func().pk, int(settings.TASK_USER_ID)) eq_(get_user().pk, 999)
def check_xpi_info(xpi_info, addon=None): from olympia.addons.models import Addon, BlacklistedGuid guid = xpi_info['guid'] is_webextension = xpi_info.get('is_webextension', False) # If we allow the guid to be omitted we assume that one was generated # or existed before and use that one. # An example are WebExtensions that don't require a guid but we generate # one once they're uploaded. Now, if you update that WebExtension we # just use the original guid. if addon and not guid and is_webextension: xpi_info['guid'] = guid = addon.guid if not guid and not is_webextension: raise forms.ValidationError(_("Could not find an add-on ID.")) if guid: if amo.get_user(): deleted_guid_clashes = Addon.unfiltered.exclude( authors__id=amo.get_user().id).filter(guid=guid) else: deleted_guid_clashes = Addon.unfiltered.filter(guid=guid) guid_too_long = ( not waffle.switch_is_active('allow-long-addon-guid') and len(guid) > 64 ) if guid_too_long: raise forms.ValidationError( _("Add-on ID must be 64 characters or less.")) if addon and addon.guid != guid: msg = _( "The add-on ID in your manifest.json or install.rdf (%s) " "does not match the ID of your add-on on AMO (%s)") raise forms.ValidationError(msg % (guid, addon.guid)) if (not addon and # Non-deleted add-ons. (Addon.with_unlisted.filter(guid=guid).exists() or # BlacklistedGuid objects for legacy deletions. BlacklistedGuid.objects.filter(guid=guid).exists() or # Deleted add-ons that don't belong to the uploader. deleted_guid_clashes.exists())): raise forms.ValidationError(_('Duplicate add-on ID found.')) if len(xpi_info['version']) > 32: raise forms.ValidationError( _('Version numbers should have fewer than 32 characters.')) if not VERSION_RE.match(xpi_info['version']): raise forms.ValidationError( _('Version numbers should only contain letters, numbers, ' 'and these punctuation characters: +*.-_.')) return xpi_info
def wrapper(*args, **kw): old_user = get_user() set_user(get_task_user()) try: result = f(*args, **kw) finally: set_user(old_user) return result
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from olympia.access.models import Group from olympia.addons.models import Addon from olympia.amo import get_user, logger_log from olympia.devhub.models import (ActivityLog, AddonLog, CommentLog, GroupLog, UserLog, VersionLog) from olympia.users.models import UserProfile from olympia.versions.models import Version user = kw.get('user', get_user()) if not user: logger_log.warning('Activity log called with no user: %s' % action.id) return al = ActivityLog(user=user, action=action.id) al.arguments = args if 'details' in kw: al.details = kw['details'] al.save() if 'details' in kw and 'comments' in al.details: CommentLog(comments=al.details['comments'], activity_log=al).save() # TODO(davedash): post-remora this may not be necessary. if 'created' in kw: al.created = kw['created'] # Double save necessary since django resets the created date on save. al.save() for arg in args: if isinstance(arg, tuple): if arg[0] == Addon: AddonLog(addon_id=arg[1], activity_log=al).save() elif arg[0] == Version: VersionLog(version_id=arg[1], activity_log=al).save() elif arg[0] == UserProfile: UserLog(user_id=arg[1], activity_log=al).save() elif arg[0] == Group: GroupLog(group_id=arg[1], activity_log=al).save() elif isinstance(arg, Addon): AddonLog(addon=arg, activity_log=al).save() elif isinstance(arg, Version): VersionLog(version=arg, activity_log=al).save() elif isinstance(arg, UserProfile): # Index by any user who is mentioned as an argument. UserLog(activity_log=al, user=arg).save() elif isinstance(arg, Group): GroupLog(group=arg, activity_log=al).save() # Index by every user UserLog(activity_log=al, user=user).save() return al
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from olympia.access.models import Group from olympia.addons.models import Addon from olympia.amo import get_user, logger_log from olympia.devhub.models import ( ActivityLog, AddonLog, CommentLog, GroupLog, UserLog, VersionLog) from olympia.users.models import UserProfile from olympia.versions.models import Version user = kw.get('user', get_user()) if not user: logger_log.warning('Activity log called with no user: %s' % action.id) return al = ActivityLog(user=user, action=action.id) al.arguments = args if 'details' in kw: al.details = kw['details'] al.save() if 'details' in kw and 'comments' in al.details: CommentLog(comments=al.details['comments'], activity_log=al).save() # TODO(davedash): post-remora this may not be necessary. if 'created' in kw: al.created = kw['created'] # Double save necessary since django resets the created date on save. al.save() for arg in args: if isinstance(arg, tuple): if arg[0] == Addon: AddonLog(addon_id=arg[1], activity_log=al).save() elif arg[0] == Version: VersionLog(version_id=arg[1], activity_log=al).save() elif arg[0] == UserProfile: UserLog(user_id=arg[1], activity_log=al).save() elif arg[0] == Group: GroupLog(group_id=arg[1], activity_log=al).save() elif isinstance(arg, Addon): AddonLog(addon=arg, activity_log=al).save() elif isinstance(arg, Version): VersionLog(version=arg, activity_log=al).save() elif isinstance(arg, UserProfile): # Index by any user who is mentioned as an argument. UserLog(activity_log=al, user=arg).save() elif isinstance(arg, Group): GroupLog(group=arg, activity_log=al).save() # Index by every user UserLog(activity_log=al, user=user).save() return al
def start_validation(request): form = BulkValidationForm(request.POST) if form.is_valid(): job = form.save(commit=False) job.creator = get_user() job.save() find_files(job) return redirect(reverse('zadmin.validation')) else: return validation(request, form=form)
def start_validation(request): # FIXME: `@transaction.non_atomic_requests` is a workaround for an issue # that might exist elsewhere too. The view is wrapped in a transaction # by default and because of that tasks being started in this view # won't see the `ValidationJob` object created. form = BulkValidationForm(request.POST) if form.is_valid(): job = form.save(commit=False) job.creator = get_user() job.save() find_files(job) return redirect(reverse('zadmin.validation')) else: return validation(request, form=form)
def some_func(): return get_user()