def create_template(self, request, **kwargs): template_name = request.DATA.get('template_name', None) template_description = request.DATA.get('template_description', None) if not template_name: raise response.BadRequest(_("Not valid template name")) if not template_description: raise response.BadRequest(_("Not valid template description")) with advisory_lock("create-project-template"): template_slug = slugify_uniquely(template_name, models.ProjectTemplate) project = self.get_object() self.check_permissions(request, 'create_template', project) template = models.ProjectTemplate( name=template_name, slug=template_slug, description=template_description, ) template.load_data_from_project(project) template.save() return response.Created( serializers.ProjectTemplateSerializer(template).data)
def duplicate(self, request, pk=None): project = self.get_object() self.check_permissions(request, "duplicate", project) if project.blocked_code is not None: raise exc.Blocked(_("Blocked element")) validator = validators.DuplicateProjectValidator(data=request.DATA) if not validator.is_valid(): return response.BadRequest(validator.errors) data = validator.data # Validate if the project can be imported is_private = data.get('is_private', False) total_memberships = len(data.get("users", [])) + 1 (enough_slots, error_message) = users_services.has_available_slot_for_new_project( self.request.user, is_private, total_memberships) if not enough_slots: raise exc.NotEnoughSlotsForProject(is_private, total_memberships, error_message) new_project = services.duplicate_project( project=project, owner=request.user, name=data["name"], description=data["description"], is_private=data["is_private"], users=data["users"]) new_project = get_object_or_404(self.get_queryset(), id=new_project.id) serializer = self.get_serializer(new_project) return response.Created(serializer.data)
def _public_register(self, request): if not settings.PUBLIC_REGISTER_ENABLED: raise exc.BadRequest(_("Public register is disabled.")) try: data = parse_public_register_data(request.DATA) user = public_register(**data) except exc.IntegrityError as e: raise exc.BadRequest(e.detail) data = make_auth_response_data(user) return response.Created(data)
def us(self, request, *args, **kwargs): project = self.get_object_or_none() self.check_permissions(request, 'import_item', project) us = services.store.store_user_story(project, request.DATA.copy()) errors = services.store.get_errors() if errors: raise exc.BadRequest(errors) data = serializers.UserStoryExportSerializer(us.object).data headers = self.get_success_headers(data) return response.Created(data, headers=headers)
def issue(self, request, *args, **kwargs): project = self.get_object_or_none() self.check_permissions(request, 'import_item', project) signals.pre_save.disconnect( sender=Issue, dispatch_uid="set_finished_date_when_edit_issue") issue = services.store.store_issue(project, request.DATA.copy()) errors = services.store.get_errors() if errors: raise exc.BadRequest(errors) data = serializers.IssueExportSerializer(issue.object).data headers = self.get_success_headers(data) return response.Created(data, headers=headers)
def create(self, request, *args, **kwargs): validator = self.get_validator(data=request.DATA, files=request.FILES) if validator.is_valid(): self.check_permissions(request, 'create', validator.object) self.pre_save(validator.object) self.pre_conditions_on_save(validator.object) self.object = validator.save(force_insert=True) self.post_save(self.object, created=True) instance = self.get_queryset().get(id=self.object.id) serializer = self.get_serializer(instance) headers = self.get_success_headers(serializer.data) return response.Created(serializer.data, headers=headers) return response.BadRequest(validator.errors)
def create(self, request, *args, **kwargs): self.check_permissions(request, 'import_project', None) data = request.DATA.copy() data['owner'] = data.get('owner', request.user.email) # Validate if the project can be imported is_private = data.get('is_private', False) total_memberships = len([ m for m in data.get("memberships", []) if m.get("email", None) != data["owner"] ]) total_memberships = total_memberships + 1 # 1 is the owner (enough_slots, error_message) = users_services.has_available_slot_for_new_project( self.request.user, is_private, total_memberships) if not enough_slots: raise exc.NotEnoughSlotsForProject(is_private, total_memberships, error_message) # Create Project project_serialized = services.store.store_project(data) if not project_serialized: raise exc.BadRequest(services.store.get_errors()) # Create roles roles_serialized = None if "roles" in data: roles_serialized = services.store.store_roles( project_serialized.object, data) if not roles_serialized: raise exc.BadRequest(_("We needed at least one role")) # Create memberships if "memberships" in data: services.store.store_memberships(project_serialized.object, data) try: owner_membership = project_serialized.object.memberships.get( user=project_serialized.object.owner) owner_membership.is_admin = True owner_membership.save() except Membership.DoesNotExist: Membership.objects.create( project=project_serialized.object, email=project_serialized.object.owner.email, user=project_serialized.object.owner, role=project_serialized.object.roles.all().first(), is_admin=True) # Create project values choicess if "points" in data: services.store.store_project_attributes_values( project_serialized.object, data, "points", validators.PointsExportValidator) if "issue_types" in data: services.store.store_project_attributes_values( project_serialized.object, data, "issue_types", validators.IssueTypeExportValidator) if "issue_statuses" in data: services.store.store_project_attributes_values( project_serialized.object, data, "issue_statuses", validators.IssueStatusExportValidator, ) if "issue_duedates" in data: services.store.store_project_attributes_values( project_serialized.object, data, "issue_duedates", validators.IssueDueDateExportValidator, ) if "us_statuses" in data: services.store.store_project_attributes_values( project_serialized.object, data, "us_statuses", validators.UserStoryStatusExportValidator, ) if "us_duedates" in data: services.store.store_project_attributes_values( project_serialized.object, data, "us_duedates", validators.UserStoryDueDateExportValidator, ) if "task_statuses" in data: services.store.store_project_attributes_values( project_serialized.object, data, "task_statuses", validators.TaskStatusExportValidator) if "task_duedates" in data: services.store.store_project_attributes_values( project_serialized.object, data, "task_duedates", validators.TaskDueDateExportValidator) if "priorities" in data: services.store.store_project_attributes_values( project_serialized.object, data, "priorities", validators.PriorityExportValidator) if "severities" in data: services.store.store_project_attributes_values( project_serialized.object, data, "severities", validators.SeverityExportValidator) if ("points" in data or "issues_types" in data or "issues_statuses" in data or "us_statuses" in data or "task_statuses" in data or "priorities" in data or "severities" in data): services.store.store_default_project_attributes_values( project_serialized.object, data) # Created custom attributes if "userstorycustomattributes" in data: services.store.store_custom_attributes( project_serialized.object, data, "userstorycustomattributes", validators.UserStoryCustomAttributeExportValidator) if "taskcustomattributes" in data: services.store.store_custom_attributes( project_serialized.object, data, "taskcustomattributes", validators.TaskCustomAttributeExportValidator) if "issuecustomattributes" in data: services.store.store_custom_attributes( project_serialized.object, data, "issuecustomattributes", validators.IssueCustomAttributeExportValidator) # Is there any error? errors = services.store.get_errors() if errors: raise exc.BadRequest(errors) # Importer process is OK response_data = serializers.ProjectExportSerializer( project_serialized.object).data response_data['id'] = project_serialized.object.id headers = self.get_success_headers(response_data) return response.Created(response_data, headers=headers)
def load_dump(self, request): throttle = throttling.ImportDumpModeRateThrottle() if not throttle.allow_request(request, self): self.throttled(request, throttle.wait()) self.check_permissions(request, "load_dump", None) dump = request.FILES.get('dump', None) if not dump: raise exc.WrongArguments(_("Needed dump file")) if dump.content_type == "application/gzip": dump = gzip.GzipFile(fileobj=dump) reader = codecs.getreader("utf-8") try: dump = json.load(reader(dump)) except Exception: raise exc.WrongArguments(_("Invalid dump format")) slug = dump.get('slug', None) if slug is not None and Project.objects.filter(slug=slug).exists(): del dump['slug'] user = request.user dump['owner'] = user.email # Validate if the project can be imported is_private = dump.get("is_private", False) total_memberships = len([ m for m in dump.get("memberships", []) if m.get("email", None) != dump["owner"] ]) total_memberships = total_memberships + 1 # 1 is the owner (enough_slots, error_message) = users_services.has_available_slot_for_new_project( user, is_private, total_memberships) if not enough_slots: raise exc.NotEnoughSlotsForProject(is_private, total_memberships, error_message) # Async mode if settings.CELERY_ENABLED: task = tasks.load_project_dump.delay(user, dump) return response.Accepted({"import_id": task.id}) # Sync mode try: project = services.store_project_from_dict(dump, request.user) except err.TinaImportError as e: # On Error ## remove project if e.project: e.project.delete_related_content() e.project.delete() return response.BadRequest({ "error": e.message, "details": e.errors }) else: # On Success project_from_qs = project_utils.attach_extra_info( Project.objects.all()).get(id=project.id) response_data = ProjectSerializer(project_from_qs).data return response.Created(response_data)
def _private_register(self, request): data = parse_private_register_data(request.DATA) user = private_register_for_new_user(**data) data = make_auth_response_data(user) return response.Created(data)