def invite(self, request, **kwargs): Role = get_model('user', 'Role') try: role = Role.objects.get(slug=request.data.get('role', None), company=self._objects['company']) except Role.DoesNotExist: return response.BadRequest(["role doesn't exist!"]) return response.Ok(data=[])
def cached_notify_policy_for_user(self, user): """ Get notification level for specified company and user. """ policy = self.cached_notify_policies.get(user.id, None) if policy is None: model_cls = get_model("notifications", "NotifyPolicy") policy = model_cls.objects.create( company=self, user=user, notify_level=NotifyLevel.involved) del self.cached_notify_policies return policy
def process_company_save(sender, instance, created, **kwargs): """ Populate new project dependent default data """ # update es entity # registry.update(instance) if not created: return if instance._importing: return template = getattr(instance, "creation_template", None) if template is None: CompanyTemplate = get_model("company", "CompanyTemplate") template = CompanyTemplate.objects.get( slug=settings.DEFAULT_COMPANY_TEMPLATE) template.apply_to_company(instance) instance.save() Role = get_model("user", "Role") try: owner_role = instance.roles.get( slug=settings.DEFAULT_COMPANY_OWNER_ROLE) except Role.DoesNotExist: owner_role = instance.roles.first() if owner_role: Membership = get_model("company", "Membership") Membership.objects.create(user=instance.owner, company=instance, role=owner_role, is_admin=True, email=instance.owner.email)
def apply_to_company(self, company): Role = get_model("user", "Role") if company.id is None: raise Exception("company need an id (must be a saved company)") company.creation_template = self for role in self.roles: Role.objects.create(name=role["name"], slug=role["slug"], order=role["order"], company=company, permissions=role['permissions']) return company
from tb.core.api.pagination import ESDrfPagination from tb.core.loading import get_class, get_model from tb.core import filters from tb.core.api import response from tb.core.api.viewsets import TBModelViewSet from tb.core.api.mixins import object_required from tb.core.api.utils import tb_get_object_or_404 from ..documents import CompanyDocument from ..serializers import CompanySerializer, CompanyDocumentSerializer from .. import permissions as CompanyPermissions from .mixins import WithCompanyHeaderMixin from .. import signals Role = get_model('user', 'Role') Company = get_model('company', 'Company') Membership = get_model('company', 'Membership') WithJobHeaderMixin = get_class('job.views.mixins', 'WithJobHeaderMixin') class CompanyViewset(WithCompanyHeaderMixin, TBModelViewSet): """ Requirements: - be able to put data only if the user is it's creator or it's the owner """ serializer_class = CompanySerializer queryset = Company.objects.all() permission_classes = (CompanyPermissions.CompanyPermission, )
from collections import OrderedDict as SortedDict except ImportError: from django.utils.datastructures import SortedDict from cities_light.models import City from tb.core.loading import get_model from tb.core.compat import AUTH_USER_MODEL from tb.core.utils.slug import slugify_uniquely from tb.core.utils.time import timestamp_ms from tb.models.fields.json import JSONField from .managers import MembershipManager from tb.apps.notifications.choices import NotifyLevel # from tb.apps.user.permissions.utils import assign_role, remove_role, clear_roles Role = get_model('user', 'Role') def unique_company_avatar_filename(instance, filename): base_path = Company.AVATAR_ROOT file_type = os.path.splitext(filename)[1] filename = "{}{}".format(instance.slug, file_type) return os.path.join(base_path, filename) class Membership(models.Model): # This model stores all company memberships. Also # stores invitations to memberships that does not have # assigned user. MALE = 'male' FEMALE = 'female'
class Meta: model = get_model('company', 'Membership') fields = ('company', 'role_name', 'abilities', 'full_name', 'is_user_active', 'invited_by', 'is_admin')
class Meta: model = get_model('company', 'Membership') fields = ('role_slug', 'role_name', 'full_name', 'is_user_active', 'invited_by')
import serpy from django.contrib.auth import get_user_model from rest_framework import serializers from django_elasticsearch_dsl_drf.serializers import DocumentSerializer from tb.core.loading import get_class, get_model from tb.core.api.serializers import TBLightSerializer from tb.core.user import get_user_photo_url, get_user_big_photo_url, get_user_gravatar_id from tb.core.permissions.choices import MEMBERS_PERMISSIONS from .documents import CompanyDocument UserModel = get_user_model() Company = get_model('company', 'Company') Membership = get_model('company', 'Membership') UserBasicInfoSerializer = get_class('user.serializers', 'UserBasicInfoSerializer') MEMBERS_PERMISSIONS_DICT = dict(MEMBERS_PERMISSIONS) class CompanySerializer(serializers.ModelSerializer): class Meta: model = Company exclude = ('owner', 'creator') class CompanyRolesSerializer(TBLightSerializer): slug = serpy.Field() bundle = serpy.Field()
from django.conf import settings from django_elasticsearch_dsl import DocType, Index, fields from tb.core.loading import get_model from cities_light.models import City from libs.es import html_strip_analyzer, edgengram_analyzer, RawObjectField, simple_text_analyzer Company = get_model('company', 'Company') INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__]) INDEX.settings( number_of_shards=1, number_of_replicas=1 ) @INDEX.doc_type class CompanyDocument(DocType): id = fields.IntegerField(attr='id') name = fields.TextField( analyzer=edgengram_analyzer, fields={ 'raw': fields.KeywordField(), } ) geo = fields.GeoPointField(attr='geo_es_format') description = fields.TextField() avatar = fields.FileField() city = fields.ObjectField( properties={
from django.db.models.signals import post_save, post_delete from django.dispatch import receiver from django.conf import settings from tb.core.loading import get_model from . import signals @receiver(post_save, sender=get_model("company", "Company"), dispatch_uid='company_post_save') def process_company_save(sender, instance, created, **kwargs): """ Populate new project dependent default data """ # update es entity # registry.update(instance) if not created: return if instance._importing: return template = getattr(instance, "creation_template", None) if template is None: CompanyTemplate = get_model("company", "CompanyTemplate") template = CompanyTemplate.objects.get( slug=settings.DEFAULT_COMPANY_TEMPLATE) template.apply_to_company(instance)