示例#1
0
    def verify_email(self):
        import string
        import random
        from esp.users.models import PersistentQueryFilter
        from esp.dbmail.models import MessageRequest
        from django.template import loader

        symbols = string.ascii_uppercase + string.digits
        code = "".join([random.choice(symbols) for x in range(30)])

        regProf = RegistrationProfile.getLastForProgram(
            get_current_request().user, self.program)

        if regProf.email_verified:
            return self.goToCore(tl)

        if request.method == 'POST' and request.POST.has_key('verify_me'):
            # create the variable modules
            variable_modules = {
                'program': self.program,
                'user': get_current_request().user
            }

            # get the filter object
            filterobj = PersistentQueryFilter.getFilterFromQ(
                Q(id=get_current_request().user.id), User,
                'User %s' % get_current_request().user.username)

            newmsg_request = MessageRequest.createRequest(
                var_dict=variable_modules,
                subject='[ESP] Email Verification For esp.mit.edu',
                recipients=filterobj,
                sender='"MIT Educational Studies Program" <*****@*****.**>',
                creator=self,
                msgtext=loader.find_template_source('email/verify')[0])

            newmsg_request.save()

            return render_to_response(self.baseDir() + 'emailsent.html',
                                      request, {})

        return render_to_response(self.baseDir() + 'sendemail.html', request,
                                  {})
示例#2
0
def get_user_list(request, listDict2, extra=''):
    """ Get a list of users from some complicated mixture of other lists.
        The listDict must be of the form:
          {'list1_key': {'list:         Q_Object,
                         'description': "UseFul_Description"}
            ...
          }
    
        This will return a tuple (userlist_or_response, found_list).
        If found_list is True, then userlist_or_response is a UserList object.
        
        Otherwise, it returns a response that's expected to be returned to django.
        """

    if type(listDict2) != dict or len(listDict2) == 0:
        raise ESPError(), 'User lists were not specified correctly!'

    listDict = {}

    for key, value in listDict2.items():
        listDict[key] = {'list': getQForUser(value['list']),
                         'description': value['description']}


    if request.POST.has_key('select_mailman'):
        from esp.mailman import list_members
        import operator

        lists = request.POST.getlist('select_mailman')

        all_list_members = reduce(operator.or_, (list_members(x) for x in lists))
        filterObj = PersistentQueryFilter.getFilterFromQ(Q(id__in=[x.id for x in all_list_members]), ESPUser, 'Custom Mailman filter: ' + ", ".join(lists))

        if request.POST['submitform'] == 'I want to search within this list':
            getUser, found = search_for_user(request, ESPUser.objects.filter(filterObj.get_Q()).distinct(), filterObj.id, True)
            if found:
                if type(getUser) == User or type(getUser) == ESPUser:
                    newfilterObj = PersistentQueryFilter.getFilterFromQ(Q(id = getUser.id), ESPUser, 'User %s' % getUser.username)
                else:
                    newfilterObj = PersistentQueryFilter.getFilterFromQ(filterObj.get_Q() & getUser, ESPUser, 'Custom user filter')
                return (newfilterObj, True)
            else:
                return (getUser, False)

        elif request.POST['submitform'] == 'I want a subset of this list':
            getUsers, found = get_user_checklist(request, ESPUser.objects.filter(filterObj.get_Q()).distinct(), filterObj.id)
            if found:
                newfilterObj = PersistentQueryFilter.getFilterFromQ(getUsers, ESPUser, 'Custom list')
                return (newfilterObj, True)
            else:
                return (getUsers, False)

        return (filterObj, True) # We got the list, return it.
        

    if request.POST.has_key('submit_checklist') and \
            request.POST['submit_checklist'] == 'true':

        # If we're coming back after having checked off users from a checklist...
        filterObj = PersistentQueryFilter.getFilterFromID(request.POST['extra'], ESPUser)
        getUsers, found = get_user_checklist(request, ESPUser.objects.filter(filterObj.get_Q()).distinct(), filterObj.id)
        if found:
            # want to make a PersistentQueryFilter out of this returned query
            newfilterObj = PersistentQueryFilter.getFilterFromQ(getUsers, ESPUser, 'Custom list')
            return (newfilterObj, True)
        else:
            return (getUsers, False)
    

    if request.POST.has_key('submit_user_list') and \
       request.POST['submit_user_list'] == 'true':

        # If a user list was submitted....
        import operator

        # map of operators that can be done on lists (appropriately, Q Objects)
        opmapping = {'and'  : operator.and_,
                     'or'   : operator.or_,
                     'not'  : (lambda x: ~x), # aseering 7/12/2008 -- Should work with Django SVN HEAD
                     'ident': (lambda x: x) # for completeness
                     }

        # this is the "first" list...the list we start with.
        if listDict.has_key(request.POST['base_list']):
            curList = listDict[request.POST['base_list']]['list']
        else:
            raise ESPError(), 'I do not know of list "%s".' % request.POST['base_list']
        
        # we start with all the sparated lists, and apply the and'd lists onto the or'd lists before
        # we or. This closely represents the sentence (it's not as powerful, but makes "sense")
        separated = {'or': [curList], 'and': []}


        # use double-commas because it's safer?
        keys = request.POST['keys'].split(',,')

        for key in keys:
            if request.POST.has_key('operator_'+key) and \
               request.POST['operator_'+key]         and \
               request.POST['operator_'+key] != 'ignore':     # and it's not ignore (it should be 'and' or 'or')
                # We are adding to the list of 'and'd' lists and 'or'd' lists.
                separated[request.POST['operator_'+key]].append(opmapping[request.POST['not_'+key]](listDict[key]['list']))


        # ^ - now separated has all the necessary Q objects.

        # essentially the below turns: a OR b AND c OR d ... into: (a AND c) or (b AND c) or (d AND c)

        # now we can apply the ands on all the or'd lists separately
        for i in range(len(separated['or'])):
            for j in range(len(separated['and'])):
                separated['or'][i] = opmapping['and'](separated['or'][i], separated['and'][j])

        # and now we OR the leftover ors
        curList = separated['or'][0]
        for List in separated['or'][1:]:
            curList = opmapping['or'](curList, List)


        filterObj = PersistentQueryFilter.getFilterFromQ(curList, ESPUser, request.POST['finalsent'])


        if request.POST['submitform'] == 'I want to search within this list':
            getUser, found = search_for_user(request, ESPUser.objects.filter(filterObj.get_Q()).distinct(), filterObj.id, True)
            if found:
                if type(getUser) == User or type(getUser) == ESPUser:
                    newfilterObj = PersistentQueryFilter.getFilterFromQ(Q(id = getUser.id), ESPUser, 'User %s' % getUser.username)
                else:
                    newfilterObj = PersistentQueryFilter.getFilterFromQ(filterObj.get_Q() & getUser, ESPUser, 'Custom user filter')
                return (newfilterObj, True)
            else:
                return (getUser, False)

        elif request.POST['submitform'] == 'I want a subset of this list':
            getUsers, found = get_user_checklist(request, ESPUser.objects.filter(filterObj.get_Q()).distinct(), filterObj.id)
            if found:
                newfilterObj = PersistentQueryFilter.getFilterFromQ(getUsers, ESPUser, 'Custom list')
                return (newfilterObj, True)
            else:
                return (getUsers, False)

        return (filterObj, True) # We got the list, return it.


    # if we found a single user:
    if request.method == 'GET' and request.GET.has_key('op') and request.GET['op'] == 'usersearch':
        filterObj = PersistentQueryFilter.getFilterFromID(request.GET['extra'], ESPUser)
        getUser, found = search_for_user(request, ESPUser.objects.filter(filterObj.get_Q()).distinct(), filterObj.id, True)
        if found:
            if type(getUser) == User or type(getUser) == ESPUser:
                newfilterObj = PersistentQueryFilter.getFilterFromQ(Q(id = getUser.id), ESPUser, 'User %s' % getUser.username)
            else:
                newfilterObj = PersistentQueryFilter.getFilterFromQ(filterObj.get_Q() & getUser, ESPUser, 'Custom user filter')         

            if 'usersearch_containers' in request.session:
                request.POST, request.GET = request.session['usersearch_containers']
                del request.session['usersearch_containers']   

            return (newfilterObj, True)
        
        else:
            return (getUser, False)


    if request.GET.has_key('advanced') or not settings.USE_MAILMAN:
        # we're going to prepare a list to send out.
        arrLists = []

        pickled_post = pickle.dumps(request.POST)
        pickled_get  = pickle.dumps(request.GET)

        request.session['usersearch_containers'] = (pickled_post, pickled_get)
        
        for key, value in listDict.items():
            arrLists.append(DBList(key = key, QObject = value['list'], description = value['description'].strip('.'))) # prepare a nice list thing.
        
        arrLists.sort(reverse=True) 

        return (render_to_response('users/create_list.html', request, None, {'lists': arrLists}), False) # No, we didn't find it yet...
    else:
        from esp.mailman import all_lists
        public_lists = all_lists()
        nonpublic_lists = list( set(all_lists(show_nonpublic=True)) - set(public_lists) )
        return (render_to_response('users/select_mailman_list.html', request, None, {'public_lists': public_lists, 'nonpublic_lists': nonpublic_lists}), False) # No, we didn't find it yet...