def check_xpi_info(xpi_info, addon=None): from 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): @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 build_filters(self, filters=None): """ If `addon__exact` is a filter and its value cannot be coerced into an int, assume that it's a slug lookup. Run the query necessary to determine the app, and substitute the slug with the PK in the filter so tastypie will continue doing its thing. """ built = super(RatingResource, self).build_filters(filters) if 'addon__exact' in built: try: int(built['addon__exact']) except ValueError: app = self.get_app(built['addon__exact']) if app: built['addon__exact'] = str(app.pk) if built.get('user__exact', None) == 'mine': # This is a cheat. Would prefer /mine/ in the URL. user = get_user() if not user: # You must be logged in to use "mine". raise ImmediateHttpResponse(response=http.HttpUnauthorized()) built['user__exact'] = user.pk return built
def save(self, addon, commit=True): if self.cleaned_data: self.instance.addon = addon if self.cleaned_data.get('DELETE'): # Existing preview. if self.instance.id: self.instance.delete() # User has no desire to save this preview. return super(PreviewForm, self).save(commit=commit) if self.cleaned_data['upload_hash']: upload_hash = self.cleaned_data['upload_hash'] upload_path = os.path.join(settings.TMP_PATH, 'preview', upload_hash) filetype = (os.path.splitext(upload_hash)[1][1:] .replace('-', '/')) if filetype in amo.VIDEO_TYPES: self.instance.update(filetype=filetype) vtasks.resize_video.delay(upload_path, self.instance, user=amo.get_user(), set_modified_on=[self.instance]) else: self.instance.update(filetype='image/png') tasks.resize_preview.delay(upload_path, self.instance, set_modified_on=[self.instance])
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from addons.models import Addon from amo import get_user, logger_log from devhub.models import (ActivityLog, AddonLog, AppLog, UserLog, CommentLog, VersionLog) from mkt.webapps.models import Webapp from users.models import UserProfile from 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] == Webapp: AppLog(addon_id=arg[1], activity_log=al).save() elif 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() # Webapp first since Webapp subclasses Addon. if isinstance(arg, Webapp): AppLog(addon=arg, 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() # Index by every user UserLog(activity_log=al, user=user).save() return al
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 save(self, addon, commit=False): tags_new = self.cleaned_data['tags'] tags_old = [slugify(t.tag_text, spaces=True) for t in addon.tags.all()] # Add new tags. for t in set(tags_new) - set(tags_old): Tag(tag_text=t).save_tag(addon, amo.get_user()) # Remove old tags. for t in set(tags_old) - set(tags_new): Tag(tag_text=t).remove_tag(addon, amo.get_user()) # We ignore `commit`, since we need it to be `False` so we can save # the ManyToMany fields on our own. addonform = super(AddonFormBasic, self).save(commit=False) addonform.save() return addonform
def get_user(self, ident): pk = ident if pk == 'mine': user = amo.get_user() if not user: # You must be logged in to use "mine". raise NotAuthenticated() pk = user.pk return pk
def obj_create(self, bundle, request=None, **kwargs): form = NewManifestForm(bundle.data) if not form.is_valid(): raise self.form_errors(form) bundle.obj = FileUpload.objects.create(user=amo.get_user()) tasks.fetch_manifest(form.cleaned_data['manifest'], bundle.obj.pk) log.info('Validation created: %s' % bundle.obj.pk) return bundle
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from access.models import Group from addons.models import Addon from amo import get_user, logger_log from devhub.models import ActivityLog, AddonLog, CommentLog, GroupLog, UserLog, VersionLog from users.models import UserProfile from 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 obj_create(self, bundle, request=None, **kwargs): packaged = "upload" in bundle.data form = NewPackagedForm(bundle.data) if packaged else NewManifestForm(bundle.data) if not form.is_valid(): raise self.form_errors(form) if not packaged: upload = FileUpload.objects.create(user=amo.get_user()) # The hosted app validator is pretty fast. tasks.fetch_manifest(form.cleaned_data["manifest"], upload.pk) else: upload = form.file_upload # The packaged app validator is much heavier. tasks.validator.delay(upload.pk) # This is a reget of the object, we do this to get the refreshed # results if not celery delayed. bundle.obj = FileUpload.uncached.get(pk=upload.pk) log.info("Validation created: %s" % bundle.obj.pk) return bundle
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from devhub.models import ActivityLog, AddonLog, UserLog from addons.models import Addon from users.models import UserProfile from amo import get_user, logger_log 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 al.save() 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] == UserProfile: AddonLog(user_id=arg[1], activity_log=al).save() if isinstance(arg, Addon): AddonLog(addon=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() # Index by every user UserLog(activity_log=al, user=user).save()
def __init__(self, *args, **kwargs): self.max_size = kwargs.pop('max_size', MAX_PACKAGED_APP_SIZE) self.user = kwargs.pop('user', get_user()) self.file_upload = None super(NewPackagedAppForm, self).__init__(*args, **kwargs)
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from access.models import Group from addons.models import Addon from amo import get_user, logger_log from devhub.models import (ActivityLog, ActivityLogAttachment, AddonLog, AppLog, CommentLog, GroupLog, UserLog, VersionLog) from mkt.webapps.models import Webapp from users.models import UserProfile from 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() if 'attachments' in kw: formset = kw['attachments'] storage = get_storage_class()() for form in formset: data = form.cleaned_data if 'attachment' in data: attachment = data['attachment'] storage.save( '%s/%s' % (settings.REVIEWER_ATTACHMENTS_PATH, attachment.name), attachment) ActivityLogAttachment(activity_log=al, description=data['description'], mimetype=attachment.content_type, filepath=attachment.name).save() for arg in args: if isinstance(arg, tuple): if arg[0] == Webapp: AppLog(addon_id=arg[1], activity_log=al).save() elif 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() # Webapp first since Webapp subclasses Addon. if isinstance(arg, Webapp): AppLog(addon=arg, 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 __init__(self, *args, **kwargs): self.max_size = kwargs.pop('max_size', MAX_PACKAGED_APP_SIZE) self.user = kwargs.pop('user', get_user()) self.addon = kwargs.pop('addon', None) self.file_upload = None super(NewPackagedAppForm, self).__init__(*args, **kwargs)
def log(action, *args, **kw): """ e.g. amo.log(amo.LOG.CREATE_ADDON, []), amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version) """ from addons.models import Addon from amo import get_user, logger_log from devhub.models import (ActivityLog, ActivityLogAttachment, AddonLog, AppLog, CommentLog, UserLog, VersionLog) from mkt.webapps.models import Webapp from users.models import UserProfile from 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() if 'attachments' in kw: formset = kw['attachments'] storage = get_storage_class()() for form in formset: data = form.cleaned_data if 'attachment' in data: attachment = data['attachment'] storage.save('%s/%s' % (settings.REVIEWER_ATTACHMENTS_PATH, attachment.name), attachment) ActivityLogAttachment(activity_log=al, description=data['description'], mimetype=attachment.content_type, filepath=attachment.name).save() for arg in args: if isinstance(arg, tuple): if arg[0] == Webapp: AppLog(addon_id=arg[1], activity_log=al).save() elif 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() # Webapp first since Webapp subclasses Addon. if isinstance(arg, Webapp): AppLog(addon=arg, 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() # Index by every user UserLog(activity_log=al, user=user).save() return al
def __init__(self, *args, **kwargs): self.max_size = kwargs.pop("max_size", MAX_PACKAGED_APP_SIZE) self.user = kwargs.pop("user", get_user()) self.addon = kwargs.pop("addon", None) self.file_upload = None super(NewPackagedAppForm, self).__init__(*args, **kwargs)
def some_func(): return get_user()