import logging
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.shortcuts import get_object_or_404
from slacker import Slacker
from website.models import Team, SharedChannel
from . import verified_token

logger = logging.getLogger('basicLogger')

@csrf_exempt
@require_POST
def list_members(request):
    logger.debug(request.POST)

    #NOTE: Slash commands are sent in x-www-form-urlencoded format, not JSON

    if not verified_token(request.POST['token']):
        logger.warning("Token verification failed. ({})".format(request.POST['token']))
        return HttpResponse(status=401)

    local_team = get_object_or_404(Team, team_id=request.POST['team_id'])
    try:
        local_channel = SharedChannel.objects.get(channel_id=request.POST['channel_id'])
    except Exception as e:
        return JsonResponse({"text":"This doesn't seem to be a shared channel managed by this application."})

    breakdown = []

    try:
        for channel in SharedChannel.objects.all():
            team_interface = Slacker(channel.local_team.app_access_token)
            team_info = team_interface.team.info().body['team']

            channel_info = team_interface.channels.info(channel.channel_id).body['channel']
            roster = team_interface.users.list().body['members']
            filtered_roster = {u['id']:u for u in roster}

            fields = []
            for member in channel_info['members']:
                member_info = filtered_roster[member]
                fields.append({
                    'title':(member_info['profile'].get('real_name') or member_info['name']),
                    'short':True
                })

            breakdown.append({
                'title':team_info['name'],
                'thumb_url':team_info['icon']['image_88'],
                'text':'There are {} people from this team on this channel.'.format(len(channel_info['members'])),
                'fields':fields
            })

            logger.info('Breakdown built for "{}."'.format(team_info['name']))
    except Exception as e:
        logger.exception(e)

    return JsonResponse({"attachments":breakdown})

@@ -1466,124 +1466,203 @@
     
-fields = %5B%5D%0A            for member in channel_info%5B'members'%5D:%0A               
+channel_info%5B'members'%5D = %5Bfiltered_roster%5Bx%5D for x in channel_info%5B'members'%5D if not filtered_roster%5Bx%5D.get('is_bot')%5D%0A%0A            fields = %5B%5D%0A            for
  member
-_info = filtered_roster%5B
+ in channel_info%5B'
 member
-%5D
+s'%5D:
 %0A   
@@ -1678,22 +1678,15 @@
     
-fields.append(
+temp = 
 %7B%0A  
@@ -1718,21 +1718,16 @@
 :(member
-_info
 %5B'profil
@@ -1756,21 +1756,16 @@
 r member
-_info
 %5B'name'%5D
@@ -1821,83 +1821,321 @@
    %7D
-)%0A%0A            breakdown.append(%7B%0A                'title':team_info%5B'name'%5D
+%0A%0A                if member%5B'profile'%5D.get('real_name'):%0A                    temp%5B'value'%5D = '@%7B%7D'.format(member%5B'name'%5D)%0A%0A                fields.append(temp)%0A%0A            breakdown.append(%7B%0A                'title':team_info%5B'name'%5D,%0A                'title_link':'https://%7B%7D.slack.com'.format(team_info%5B'domain'%5D)
 ,%0A  

Add link to title of attachments and filtering (inefficiently af) out all bot users. #11