def install_plugins(): for plugin in settings.CORM_PLUGINS: plugin_module, plugin_name = plugin.rsplit(".", maxsplit=1) module = import_module(plugin_module) plugin_class = getattr(module, plugin_name, None) if plugin_class is not None: print("Loaded plugin: %s" % plugin) ConnectionManager.add_plugin(plugin_module, plugin_class()) else: print("Failed to load plugin: %s" % plugin)
def channels_chart(self): channel_names = dict() if not self._channelsChart: channels = list() counts = dict() from_colors = ['4e73df', '1cc88a', '36b9cc', '7dc5fe', 'cceecc'] next_color = 0 channels = Channel.objects.filter(source__community=self.member.community) convo_filter = Q(conversation__speaker=self.member, conversation__timestamp__gte=datetime.datetime.now() - datetime.timedelta(days=180)) if self.tag: convo_filter = convo_filter & Q(conversation__tags=self.tag) if self.role: convo_filter = convo_filter & Q(conversation__speaker__role=self.role) channels = channels.annotate(conversation_count=Count('conversation', filter=convo_filter)) channels = channels.annotate(source_icon=F('source__icon_name'), source_connector=F('source__connector'), color=F('tag__color')) for c in channels: if c.conversation_count == 0: continue counts[c] = c.conversation_count self._channelsChart = PieChart("channelsChart", title="Conversations by Channel", limit=8) for channel, count in sorted(counts.items(), key=operator.itemgetter(1), reverse=True): self._channelsChart.add("%s (%s)" % (channel.name, ConnectionManager.display_name(channel.source_connector)), count, channel.color) self.charts.add(self._channelsChart) return self._channelsChart
def getChannelsChart(self): channel_names = dict() if not self._channelsChart: counts = dict() total = 0 conversations = Conversation.objects.filter(channel__source__community=self.community, timestamp__gte=datetime.datetime.now() - datetime.timedelta(days=self.timespan)) if self.tag: conversations = conversations.filter(tags=self.tag) if self.member_tag: conversations = conversations.filter(speaker__tags=self.member_tag) if self.role: conversations = conversations.filter(speaker__role=self.role) conversations = conversations.annotate(source_name=F('channel__source__name'), source_connector=F('channel__source__connector')).order_by("timestamp") for c in conversations: source_name = "%s (%s)" % (c.source_name, ConnectionManager.display_name(c.source_connector)) if source_name not in counts: counts[source_name] = 1 else: counts[source_name] += 1 self._channelsChart = [(channel, count) for channel, count in sorted(counts.items(), key=operator.itemgetter(1), reverse=True)] if len(self._channelsChart) > 8: other_count = sum([count for tag, count in self._channelsChart[7:]]) self._channelsChart = self._channelsChart[:7] self._channelsChart.append(("Other", other_count, "#efefef")) return self._channelsChart
def sources_chart(self): if not self._sourcesChart: counts = dict() other_count = 0 identity_filter = Q( contact__member__first_seen__gte=self.rangestart, contact__member__last_seen__lte=self.rangeend) if self.member_tag: identity_filter = identity_filter & Q( contact__member__tags=self.member_tag) if self.role: identity_filter = identity_filter & Q( contact__member__role=self.role) sources = Source.objects.filter(community=self.community).annotate( identity_count=Count('contact', filter=identity_filter)) for source in sources: if source.identity_count == 0: continue counts[source] = source.identity_count self._sourcesChart = PieChart("sourcesChart", title="Member Sources", limit=8) for source, count in sorted(counts.items(), key=operator.itemgetter(1), reverse=True): self._sourcesChart.add( "%s (%s)" % (source.name, ConnectionManager.display_name(source.connector)), count) self.charts.add(self._sourcesChart) return self._sourcesChart
def link_url(self): if hasattr(self, '_identity_url'): return self._identity_url else: self._identity_url = ConnectionManager.get_identity_url(self) return self._identity_url
def connector_name(self): return ConnectionManager.display_name(self.source.connector)
def __str__(self): return "%s on %s" % (self.user, ConnectionManager.display_name(self.connector))
def as_json(request, community_id): view = Sources(request, community_id, json=True) nodes = list() links = list() member_map = dict() connection_counts = dict() connected = set() contact_filter = Q( contact__member__last_seen__gte=datetime.datetime.now() - datetime.timedelta(days=30)) sources = Source.objects.filter(community=view.community).annotate( contact_count=Count( 'contact', filter=contact_filter, distinct=True)) source_node_color = "1cc88a" for source in sources: if source.contact_count > 0: link = reverse('channels', kwargs={ 'community_id': source.community_id, 'source_id': source.id }) nodes.append({ "id": source.id, "name": "%s (%s)" % (source.name, ConnectionManager.display_name(source.connector)), "link": link, "color": source_node_color, "connections": source.contact_count }) contacts = Contact.objects.filter( source__community=view.community, member__last_seen__gte=datetime.datetime.now() - datetime.timedelta(days=30)) contacts = contacts.annotate(member_name=F('member__name'), member_role=F('member__role'), tag_count=Count('member__tags')) for contact in contacts: links.append({ "source": contact.source_id, "target": contact.member_id }) connected.add((contact.source_id, contact.member_id)) member_map[contact.member_id] = contact if contact.member_id not in connection_counts: connection_counts[contact.member_id] = 1 else: connection_counts[contact.member_id] += 1 for member_id, contact in member_map.items(): tag_color = "1f77b4" if contact.tag_count > 0: tags = Tag.objects.filter(member__id=member_id) if len(tags) > 0: tag_color = tags[0].color elif contact.member_role == Member.BOT: tag_color = "aeaeae" elif contact.member_role == Member.STAFF: tag_color = "36b9cc" link = reverse('member_profile', kwargs={'member_id': member_id}) nodes.append({ "id": member_id, "name": contact.member_name, "link": link, "color": tag_color, "connections": connection_counts.get(member_id, 0) }) return JsonResponse({"nodes": nodes, "links": links})
def limit_to(self, community): self.fields['owner'].widget.choices = [(member.id, member.name) for member in Member.objects.filter(community=community)] self.fields['owner'].widget.choices.insert(0, ('', '-----')) self.fields['tag'].widget.choices = [(tag.id, tag.name) for tag in Tag.objects.filter(community=community)] self.fields['tag'].widget.choices.insert(0, ('', '-----')) self.fields['channels'].widget.choices = [(channel.id, "%s (%s)" % (channel.name, ConnectionManager.display_name(channel.source_connector))) for channel in Channel.objects.filter(source__community=community).annotate(source_connector=F('source__connector')).order_by('name')]