Skip to content

Commit

Permalink
Move channels for groups to channel model
Browse files Browse the repository at this point in the history
update details and list
  • Loading branch information
benjamin-antupit committed Jan 3, 2025
1 parent 9f8b9f2 commit dee3c68
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 11 deletions.
20 changes: 20 additions & 0 deletions accounts/lookups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ajax_select import LookupChannel
from django.contrib.auth import get_user_model
from django.db.models import Q
from django.contrib.auth.models import Group

from . import ldap
from . import graph
Expand Down Expand Up @@ -37,6 +38,25 @@ def format_item_display(self, obj):
(self.get_result(obj), ", ".join(map(str, obj.groups.all())))
return '&nbsp;<strong>%s</strong>' % self.get_result(obj)

class GroupLookup(LookupChannel):
model = Group

def check_auth(self, request):
if request.user.groups.filter(Q(name="Alumni") | Q(name="Active") | Q(name="Officer")).exists():
return True

def get_query(self, q, request, search_ldap=True):
qs = Q()
for term in q.split():
qs &= Q(name__icontains=term)
return Group.objects.filter(qs).distinct().all()

def format_match(self, obj):
return self.format_item_display(obj)

def format_item_display(self, obj):
return '&nbsp;<strong>%s</strong> <i>(%s users)</i>' % \
(self.get_result(obj), obj.user_set.count())

class OfficerLookup(LookupChannel):
model = get_user_model()
Expand Down
1 change: 1 addition & 0 deletions lnldb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def from_runtime(*x):

AJAX_LOOKUP_CHANNELS = {
'Users': ('accounts.lookups', 'UserLookup'),
'Groups': ('accounts.lookups', 'GroupLookup'),
'Orgs': ('events.lookups', 'OrgLookup'),
'UserLimitedOrgs': ('events.lookups', 'UserLimitedOrgLookup'),
'Officers': ('accounts.lookups', 'OfficerLookup'),
Expand Down
46 changes: 41 additions & 5 deletions site_tmpl/slack/slack_channel_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h4>Last Updated: {{ channel.last_updated }}</h4>
</div>

<a href="{{ slack_base_url }}{{ channel.id }}" target="_blank" class="btn btn-info btn-xl pull-right"><span class="glyphicon glyphicon-link"></span> Open in Slack</a>

</div>

</div>
Expand All @@ -24,17 +24,53 @@ <h4>Last Updated: {{ channel.last_updated }}</h4>
<!--<div class="container">
<div class="alert alert-warning">BIG WARNING GOES HERE.</div>
</div>-->
<h2>Channel Configuration</h2>
<h2>Channel Configuration
{% if form == None %}
<a href="{% url 'slack:channel-edit' channel.id %}" class="btn btn-info pull-right"><span class="glyphicon glyphicon-pencil"></span> Edit Groups</a>
{% endif %}
</h2>
{% if form %}
<form method="post">
{% csrf_token %}
{{ form.media }}
{% endif %}
<table class="table">
<tr>
<th>Groups Allowed</th>
<td>{{ channel.groups_allowed }}</td>
<td>
{% if form %}
{{ form.allowed_groups }}
{% for error in form.username.errors %}
<span style='color:red;'>{{error}}</span>
{% endfor %}
{% else %}
{% for group in channel.allowed_groups.all %}
<span class="label label-default">{{ group.name }}</span>
{% endfor %}
{% endif %}
</td>
</tr>
<tr>
<th>Groups Required</th>
<td>{{ channel.groups_required }}</td>
<td>
{% if form %}
{{ form.required_groups }}
{% for error in form.username.errors %}
<span style='color:red;'>{{error}}</span>
{% endfor %}

{% else %}
{% for group in channel.required_groups.all %}
<span class="label label-default">{{ group.name }}</span>
{% endfor %}
{% endif %}
</td>
</tr>
</table>
{% if form %}
<input name="save" value="Save Changes" class="btn btn-primary" type="submit">
</form>
{% endif %}
<h2>Channel Details</h2>
<table class="table">
<tr>
Expand All @@ -55,7 +91,7 @@ <h2>Channel Details</h2>
</tr>
<tr>
<th>Created by</th>
<td>{% if creator_name %}<a href="{% url 'accounts:detail' channel.creator %}">{{ creator_name }}</a>{% endif %}</td>
<td>{% if channel.creator %}<a href="{% url 'accounts:detail' channel.creator %}">{{ channel.creator.get_full_name }}</a>{% endif %}</td>
</tr>
<tr>
<th>Created on </th>
Expand Down
12 changes: 10 additions & 2 deletions site_tmpl/slack/slack_channel_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ <h2> {{ h2 }} </h2>
</td>
<td>{{ channel.num_members }}</td>
<td>{{ channel.last_updated }}</td>
<td>{{ channel.groups_allowed }}</td>
<td>{{ channel.groups_required }}</td>
<td>
{% for group in channel.allowed_groups.all %}
<span class="label label-default">{{ group.name }}</span>
{% endfor %}
</td>
<td>
{% for group in channel.required_groups.all %}
<span class="label label-default">{{ group.name }}</span>
{% endfor %}
</td>
</tr>
{% endfor %}
</table>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.2.13 on 2025-01-03 06:15

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("slack", "0004_remove_channel_channel_id_alter_channel_id"),
]

operations = [
migrations.AddField(
model_name="channel",
name="allowed_groups",
field=models.ManyToManyField(
blank=True,
related_name="allowed_channels",
to="slack.channel",
verbose_name="Allowed Groups",
),
),
migrations.AddField(
model_name="channel",
name="required_groups",
field=models.ManyToManyField(
blank=True,
related_name="required_channels",
to="slack.channel",
verbose_name="Required Groups",
),
),
]
33 changes: 33 additions & 0 deletions slack/migrations/0006_alter_channel_allowed_groups_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.13 on 2025-01-03 06:18

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("auth", "0014_remove_group_allowed_channels_and_more"),
("slack", "0005_channel_allowed_groups_channel_required_groups"),
]

operations = [
migrations.AlterField(
model_name="channel",
name="allowed_groups",
field=models.ManyToManyField(
blank=True,
related_name="allowed_channels",
to="auth.group",
verbose_name="Allowed Groups",
),
),
migrations.AlterField(
model_name="channel",
name="required_groups",
field=models.ManyToManyField(
blank=True,
related_name="required_channels",
to="auth.group",
verbose_name="Required Groups",
),
),
]
13 changes: 13 additions & 0 deletions slack/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
from django.db import models
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from .api import channel_info, channel_members, channel_latest_message, user_profile


Expand Down Expand Up @@ -42,6 +43,18 @@ class Channel(models.Model):
Used to store Slack channel ID and retrieve common information about Slack channels
"""
id = models.CharField(max_length=256, unique=True, primary_key=True)
allowed_groups = models.ManyToManyField(
Group,
verbose_name="Allowed Groups",
related_name="allowed_channels",
blank=True,
)
required_groups = models.ManyToManyField(
Group,
verbose_name="Required Groups",
related_name="required_channels",
blank=True,
)

def __str__(self):
return self.name
Expand Down
1 change: 1 addition & 0 deletions slack/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
re_path(r'^moderate/$', views.report_list, name="moderate"),
re_path(r'^moderate/(?P<pk>\d+)/$', views.view_report, name="report"),
re_path(r'^channels/$', views.channel_list, name="channel-list"),
re_path(r'^channel/(?P<id>[^/]+)/edit/$', views.channel_detail_edit, name="channel-edit"),
re_path(r'^channel/(?P<id>[^/]+)/$', views.channel_detail, name="channel"),
]
26 changes: 22 additions & 4 deletions slack/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from ajax_select.fields import AutoCompleteSelectMultipleField
from django import forms
from django.contrib.auth.decorators import permission_required, login_required
from django.shortcuts import reverse, render, get_object_or_404
from django.http import HttpResponseRedirect
Expand All @@ -22,20 +24,36 @@ def channel_list(request):
'channels': channels,
'slack_base_url': settings.SLACK_BASE_URL+'/archives/'})

class ChannelAssignGroupForm(forms.ModelForm):
allowed_groups = AutoCompleteSelectMultipleField('Groups', required=False)
required_groups = AutoCompleteSelectMultipleField('Groups', required=False)
class Meta:
model = Channel
fields = ('allowed_groups', 'required_groups')

@login_required
@permission_required('slack.view_channel', raise_exception=True)
def channel_detail(request, id):
def channel_detail_edit(request, id):
return channel_detail(request, id, edit=True)

@login_required
@permission_required('slack.view_channel', raise_exception=True)
def channel_detail(request, id, edit=False):
"""
View details for a specific Slack channel
"""
channel = get_object_or_404(Channel, id=id)
if request.method == 'POST':
form = ChannelAssignGroupForm(data=request.POST, instance=channel)
if form.is_valid():
form.save(commit=True)
return HttpResponseRedirect(reverse('slack:channel', args=[id]))
return render(request, 'slack/slack_channel_detail.html',
{'h2': "#"+channel.name+' Details',
'channel': channel,
'creator_name': channel.creator.get_full_name() if channel.creator else None,
#'creator_name': channel.creator.get_full_name() if channel.creator else None,
'slack_base_url': settings.SLACK_BASE_URL+'/archives/',
'groups_allowed': channel.allowed_groups.all(), # TODO: Fix implementation
'groups_required': channel.required_groups.all()}) # TODO: Fix implementation
'form': ChannelAssignGroupForm(instance=channel) if edit else None})



Expand Down

0 comments on commit dee3c68

Please sign in to comment.