def post(self, request): data = json.loads(request.body) blogpost = self._get_post(data.get('id'), request.user) blogpost.title = data.get('title', '') blogpost.body = data.get('body', '') blogpost.published = data.get('published') posted_to = data.get('posted_to') if posted_to == 'organization': content_object = Organization.get_single_by_user(request.user) else: posted_to_id = int(data.get('posted_to_id')) content_object = Team.objects.get(pk=posted_to_id) # TODO: validate team blogpost.content_object = content_object blogpost.save() blogpost.tags.clear() tags = data.get('tags', []) for _tag in tags: slug = slugify(_tag.get('text', '')) try: tag = Tag.objects.get(slug=slug) except Tag.DoesNotExist: tag = Tag() tag.text =_tag.get('text', '') tag.save() blogpost.tags.add(tag) return json_response(blogpost.to_dict())
def get_logs(request): level = request.GET.get('level', 'user') page = int(request.GET.get('page', 0)) if page < 1: page = 1 per_page = 20 # TODO: move to settings entries = [] total = 0 organization = Organization.get_single_by_user(request.user) logs = [] blogposts = [] object_ids = [] if level == 'user': object = request.user object_type = ContentType.objects.get_for_model(object) object_ids = [request.user.id,] elif level == 'organization': teams = Team.objects.filter(organization=organization) if len(teams) == 0: return empty_set() object_type = ContentType.objects.get_for_model(teams[0]) object_ids = [team.id for team in teams] blogposts = Post.objects.filter(deleted=False, published=True, content_type__pk=ContentType.objects.get_for_model(organization).id, object_id=organization.pk) | Post.objects.filter(deleted=False, published=True, content_type__pk=object_type.id, object_id__in=object_ids) elif level == 'my-teams': teams = [membership.team for membership in Member.objects.filter(user=request.user)] if len(teams) == 0: return empty_set() object_type = ContentType.objects.get_for_model(teams[0]) object_ids = [team.id for team in teams] blogposts = Post.objects.filter(deleted=False, published=True, content_type__pk=object_type.id, object_id__in=object_ids) else: return json_response(status=400) logs = LoggedAction.objects.filter(content_type__pk=object_type.id, object_id__in=object_ids) | LoggedAction.objects.filter(content_type__pk=ContentType.objects.get_for_model(request.user).id, object_id__in=[request.user.id,]) items = sorted( chain(logs, blogposts), key=attrgetter('created_at'), reverse=True) paginator = Paginator(items, per_page) page_contents = paginator.page(page) total = paginator.count entries = [entry.to_dict() for entry in page_contents.object_list] return json_response({'entries': entries, 'total': total})
def update_organization(request): if request.method == 'POST': organization = Organization.get_single_by_user(request.user) logo = request.FILES.get('orglogo') menu_color = request.POST.get('menu_color') if logo is not None: organization.logo = logo if menu_color is not None: organization.menu_color = menu_color organization.save() messages.add_message(request, messages.SUCCESS, '%s organization was updated successfully' % (organization.name,)) return redirect(reverse('index')) raise Http404
def get(self, request, team_id=None): if team_id is not None: return self.get_single(request, team_id) organization = Organization.get_single_by_user(request.user) teams = Team.objects.filter(organization=organization) if request.GET.get('myteams') is not None: entries = [team.to_dict() for team in teams if request.user in team.members] else: entries = [team.to_dict() for team in teams] response = { 'total': len(teams), 'entries': entries, } return json_response(response)
def get(self, request, entry_id=None): if entry_id is not None: return self.get_single(request, entry_id) page = int(request.GET.get('page', 0)) if page < 1: page = 1 per_page = 20 # TODO: move to settings entries = [] total = 0 if request.GET.get('own') is not None: posts = Post.objects.filter(user=request.user, deleted=False) else: organization = Organization.get_single_by_user(request.user) teams = Team.objects.filter(organization=organization) if len(teams) == 0: return empty_set() object_type = ContentType.objects.get_for_model(teams[0]) object_ids = [team.id for team in teams] posts = Post.objects.filter(deleted=False, published=True, content_type__pk=ContentType.objects.get_for_model(organization).id, object_id=organization.pk) | Post.objects.filter(deleted=False, published=True, content_type__pk=object_type.id, object_id__in=object_ids) _tag = request.GET.get('tag') if _tag is not None: try: tag = Tag.objects.get(slug=_tag) except Tag.DoesNotExist: tag = None if tag is not None: posts = posts.filter(tags__in=[tag,]) posts = posts.order_by('-created_at') paginator = Paginator(posts, per_page) page_contents = paginator.page(page) total = paginator.count pages = paginator.num_pages entries = [blogpost.to_dict() for blogpost in page_contents.object_list] return json_response({'entries': entries, 'total': total, 'pages': pages})
def search(request): response = { 'entries': [], 'total': 0 } organization = Organization.get_single_by_user(request.user) users = [ms.user.to_dict() for ms in OrganizationMember.objects.filter(organization=organization)] for entry in users: entry['type'] = 'user' response['entries'] += users teams = [team.to_dict() for team in Team.objects.filter(organization=organization)] for entry in teams: entry['type'] = 'team' response['entries'] += teams roles = [role.to_dict() for role in Role.objects.filter(end_date__gt=datetime.datetime.now(), team_id__in=[team['id'] for team in teams])] for entry in roles: entry['type'] = 'role' response['entries'] += roles return json_response(response)
def get_my_organization(request): try: organization = Organization.get_single_by_user(request.user) except TypeError: return json_response(organization.to_dict()) return json_response(organization.to_dict())
def searchpeople(request): organization = Organization.get_single_by_user(request.user) peoples = [membership.user for membership in organization.members] teams = organization.team_set.all() return render(request, 'app/people.html', locals())