def handle(request):
    myself = siteaction.getLoggedInUser(request)
    if not myself:
        return HttpResponseRedirect('/login.htm')

    if request.method == 'GET':        
        action_result = ''
        if dataplus.dictGetVal(request.REQUEST, 'flashId'):
            action_result = dataplus.dictGetVal(statix.action_messages, dataplus.dictGetVal(request.REQUEST, 'flashId'), '')
            
        return siteaction.render_to_response('me/newcommunity.htm', {'action_result':action_result, 'myself':myself})
    elif request.method == 'POST':
        result, errMsg = codejar_validation.validateCommunityName(dataplus.dictGetVal(request.REQUEST,'communityName'))
        if not result:
            return siteaction.render_to_response('me/showmessage.htm', {'myself':myself, 'msg_heading':'Error', 'msg_html':'Invalid user input.'})

        comm_name = dataplus.dictGetSafeVal(request.REQUEST,'communityName')
        if codejar_network.checkCommunityNameExists(comm_name):
            return siteaction.render_to_response('me/newcommunity.htm', 
                                        {'communityName':comm_name,
                                         'communityDesc':dataplus.dictGetSafeVal(request.REQUEST,'communityDesc'),
                                         'action_result':dataplus.dictGetVal(statix.action_messages,'comm_name_exists') })
            
        community = models.Community()
        community.name = comm_name
        community.desc = dataplus.dictGetSafeVal(request.REQUEST,'communityDesc')
        community.is_moderated = { 'isPublic':False,
                                    'isModerated':True}[dataplus.dictGetVal(request.REQUEST, 'communityType')]
        community.owner_username = myself.username
        community.save()
        
        if request.FILES:
            img_save_success, error_code, img1, img2, img3, img4 = \
                studio.saveImage_x4_ByDate(request, 'comm_' + str(community.id), 'commIcon')

            if img_save_success:
                community.image_file_path = img1
                community.image_size1_file_path = img2
                community.image_size2_file_path = img3
                community.image_size3_file_path = img4
            else:
                return siteaction.render_to_response('me/newcommunity.htm', 
                                            {'communityName':community.name,
                                             'communityDesc':community.desc,
                                             'action_result':dataplus.dictGetVal(statix.action_messages,error_code) })
        else:
            community.image_file_path = config.noimg_comm
            community.image_size1_file_path = config.noimg_comm_50
            community.image_size2_file_path = config.noimg_comm_72
            community.image_size3_file_path = config.noimg_comm_128

        community.save()
        myself.communities.add(community)
        
        return HttpResponseRedirect(config.communities_url + '/' + str(community.id) + '/invite.htm')
def handle(request, comm_id):
    myself = siteaction.getLoggedInUser(request)
    if not myself:
        return HttpResponseRedirect('/login.htm')
    
    community = models.Community.objects.select_related().get(id=comm_id)
    if myself.username != community.owner_username:
        return siteaction.render_to_response('me/showmessage.htm', {'msg_heading':'Error', 'msg_html':'Unauthorized access.'})

    if request.method == 'GET':
        action_result = ''
        if dataplus.dictGetVal(request.REQUEST, 'flashId'):
            action_result = dataplus.dictGetVal(statix.action_messages, dataplus.dictGetVal(request.REQUEST, 'flashId'), '')
            
        community.image_url = dataplus.getStaticUrl(community.image_size2_file_path)
        return siteaction.render_to_response('communities/editinfo.htm', 
                    {'community':community,
                     'action_result':action_result,
                     'myself':myself})
                    
    elif request.method == 'POST':
        comm_name = dataplus.dictGetSafeVal(request.REQUEST, 'name')
        comm_desc = dataplus.dictGetSafeVal(request.REQUEST, 'desc')
        if comm_name != community.name:
            result, errMsg = codejar_validation.validateCommunityName(comm_name)
            if not result:
                return siteaction.render_to_response('communities/editinfo.htm', 
                        {'community':community,
                         'myself':myself, 
                         'action_result':'Invalid Community Name. ' + errMsg,})
            
            if codejar_network.checkCommunityNameExists(comm_name):
                return siteaction.render_to_response('communities/editinfo.htm', 
                        {'community':community,
                         'myself':myself, 
                         'action_result':dataplus.dictGetVal(statix.action_messages, 'comm_name_exists', ''), })
            
        community.name = comm_name
        community.desc = comm_desc
        community.save()
            
        return HttpResponseRedirect('./?flashId=comm_settings_chnged')