def test_delete(self): """Test, if a gtranslate entry is deleted, when the corresponding project is delete. """ p = Project(slug="rm") p.name = "RM me" p.save() Gtranslate.objects.create(use_gtranslate=False, project=p) delete_gtranslate(p)
def test_slug_validation(self): """Test that validation for slugs works.""" slug = 'abc' p = Project( slug=slug, name='a', source_language=self.language_en, description='d' ) p.clean_fields() slug = 'abc+' p = Project( slug=slug, name='a', source_language=self.language_en, description='d' ) self.assertRaises(ValidationError, p.clean_fields)
def test_project_slug_integrity(self): """ Check duplication of project slug.""" p, created = Project.objects.get_or_create( slug="foo", name="Foo Project", source_language=self.language_en) new_p = Project(slug="foo", name="Foo Project", source_language=self.language_en) self.assertRaises(IntegrityError, new_p.save)
def test_delete(self): """Test, if a gtranslate entry is deleted, when the corresponding project is delete. """ p = Project(slug="rm") p.name = "RM me" p.source_language = self.language_en p.save() Gtranslate.objects.create(project=p) p.delete() self.assertEquals(Gtranslate.objects.all().count(), 0)
def test_slug_validation(self): """Test that validation for slugs works.""" slug = 'abc' p = Project(slug=slug, name='a', source_language=self.language_en, description='d') p.clean_fields() slug = 'abc+' p = Project(slug=slug, name='a', source_language=self.language_en, description='d') self.assertRaises(ValidationError, p.clean_fields)
def myprojects(request): user = request.user maintain = Project.objects.maintained_by(user) submit_projects = Project.objects.translated_by(user) watched_projects = Project.get_watched(user) watched_resource_translations = TranslationWatch.get_watched(user) locks = Lock.objects.valid().filter(owner=user) context_var = { 'maintain_projects': maintain, 'submit_project_permissions': submit_projects, 'watched_projects': watched_projects, 'watched_resource_translations': watched_resource_translations, 'locks': locks, 'coordinator_teams': user.team_coordinators.all(), 'member_teams': user.team_members.all(), } return render_to_response("projects/project_myprojects.html", context_var, context_instance=RequestContext(request))
def myprojects(request): user = request.user maintain = Project.objects.maintained_by(user) submit_projects = Project.objects.translated_by(user) watched_projects = Project.get_watched(user) watched_resource_translations = TranslationWatch.get_watched(user) locks = Lock.objects.valid().filter(owner=user) context_var = { 'maintain_projects': maintain, 'submit_project_permissions': submit_projects, 'watched_projects': watched_projects, 'watched_resource_translations': watched_resource_translations, 'locks': locks, 'coordinator_teams': user.team_coordinators.all(), 'member_teams': user.team_members.all(), } return render_to_response("projects/project_myprojects.html", context_var, context_instance = RequestContext(request))
if isinstance(user, AnonymousUser): return False ctype = ContentType.objects.get_for_model(self) observed_items = ObservedItem.objects.filter(content_type=ctype, object_id=self.id, user=user) if observed_items: return True else: return False def get_watched(cls, user): """ Return list of 'cls' objects watched by 'user'. cls - is a class model, not an instance. user - is a user object. """ return cls.objects.filter( id__in=user.observeditem_set.filter(content_type=ContentType.objects.get_for_model(cls)) .values_list("object_id", flat=True) .query ) Project.add_to_class("is_watched", is_watched) TranslationWatch.add_to_class("is_watched", is_watched) Project.add_to_class("get_watched", classmethod(get_watched)) TranslationWatch.add_to_class("get_watched", classmethod(get_watched))
class ProjectHandler(BaseHandler): """ API handler for model Project. """ allowed_methods = ('GET', 'POST', 'PUT', 'DELETE') details_fields = ( 'slug', 'name', 'description', 'long_description', 'homepage', 'feed', 'created', 'anyone_submit', 'bug_tracker', 'trans_instructions', 'tags', 'outsource', ('maintainers', ('username', )), ('owner', ('username', )), ('resources', ( 'slug', 'name', )), 'teams', 'source_language_code', ) default_fields = ( 'slug', 'name', 'description', 'source_language_code', ) fields = default_fields allowed_fields = ( 'name', 'slug', 'description', 'long_description', 'private', 'homepage', 'feed', 'anyone_submit', 'hidden', 'bug_tracker', 'trans_instructions', 'tags', 'maintainers', 'outsource', 'source_language_code', ) exclude = () @classmethod def source_language_code(cls, p): """Add the source language as a field.""" return p.source_language.code @classmethod def teams(cls, p): """Show the language codes for which there are teams as list. Return an empty list in case there are no teams defined. """ team_set = get_project_teams(p) return team_set.values_list('language__code', flat=True) def read(self, request, project_slug=None, api_version=1): """ Get project details in json format """ # Reset fields to default value ProjectHandler.fields = ProjectHandler.default_fields if "details" in request.GET.iterkeys(): if project_slug is None: return rc.NOT_IMPLEMENTED ProjectHandler.fields = ProjectHandler.details_fields return self._read(request, project_slug) @require_mime('json') @method_decorator(one_perm_required_or_403(pr_project_add)) def create(self, request, project_slug=None, api_version=1): """ API call to create new projects via POST. """ data = getattr(request, 'data', None) if project_slug is not None: return BAD_REQUEST("POSTing to this url is not allowed.") if data is None: return BAD_REQUEST("At least parameters 'slug', 'name' and " "'source_language' are needed.") return self._create(request, data) @require_mime('json') @method_decorator( one_perm_required_or_403(pr_project_add_change, (Project, 'slug__exact', 'project_slug'))) def update(self, request, project_slug, api_version=1): """ API call to update project details via PUT. """ if project_slug is None: return BAD_REQUEST("Project slug not specified.") data = request.data if data is None: return BAD_REQUEST("Empty request.") return self._update(request, project_slug, data) @method_decorator( one_perm_required_or_403(pr_project_delete, (Project, 'slug__exact', 'project_slug'))) def delete(self, request, project_slug=None, api_version=1): """ API call to delete projects via DELETE. """ if project_slug is None: return BAD_REQUEST("Project slug not specified.") return self._delete(request, project_slug) def _read(self, request, project_slug): """ Return a list of projects or the details for a specific project. """ if project_slug is None: # Use pagination p = Project.objects.for_user(request.user) res, msg = paginate(p, request.GET.get('start'), request.GET.get('end')) if res is None: return BAD_REQUEST(msg) return res else: try: p = Project.objects.get(slug=project_slug) perm = ProjectPermission(request.user) if not perm.private(p): return rc.FORBIDDEN except Project.DoesNotExist: return rc.NOT_FOUND return p def _create(self, request, data): """ Create a new project. """ mandatory_fields = ( 'slug', 'name', 'source_language_code', ) msg = "Field '%s' is required to create a project." for field in mandatory_fields: if field not in data: return BAD_REQUEST(msg % field) if 'owner' in data: return BAD_REQUEST("Owner cannot be set explicitly.") try: self._check_fields(data.iterkeys()) except AttributeError, e: return BAD_REQUEST("Field '%s' is not available." % e.message) # outsource and maintainers are ForeignKey outsource = data.pop('outsource', {}) maintainers = data.pop('maintainers', {}) lang = data.pop('source_language_code') try: source_language = Language.objects.by_code_or_alias(lang) except Language.DoesNotExist: return BAD_REQUEST("Language %s does not exist." % lang) try: p = Project(**data) p.source_language = source_language except Exception: return BAD_REQUEST("Invalid arguments given.") try: p.full_clean() except ValidationError, e: return BAD_REQUEST("%s" % e)
if isinstance(user, AnonymousUser): return False ctype = ContentType.objects.get_for_model(self) observed_items = ObservedItem.objects.filter(content_type=ctype, object_id=self.id, user=user) if observed_items: return True else: return False def get_watched(cls, user): """ Return list of 'cls' objects watched by 'user'. cls - is a class model, not an instance. user - is a user object. """ return cls.objects.filter(id__in=user.observeditem_set.filter( content_type=ContentType.objects.get_for_model(cls) ).values_list('object_id', flat=True).query) Project.add_to_class("is_watched", is_watched) TranslationWatch.add_to_class("is_watched", is_watched) Project.add_to_class("get_watched", classmethod(get_watched)) TranslationWatch.add_to_class("get_watched", classmethod(get_watched))