Beispiel #1
0
def create_notice(creator, message, directory):
    profile = get_profile(creator)
    if not check_profile_permission(profile, 'administrate', directory):
        raise PermissionDenied
    new_notice = Notice(directory=directory, message=message)
    new_notice.save()
    return new_notice
Beispiel #2
0
def create_notice(creator, message, directory):
    profile = get_profile(creator)
    if not check_profile_permission(profile, 'administrate', directory):
        raise PermissionDenied
    new_notice = Notice(directory=directory, message=message)
    new_notice.save()
    return new_notice
Beispiel #3
0
def new_object(created, message, parent):
    if created:
        notice = Notice(directory=parent, message=message)
        notice.save()
Beispiel #4
0
def new_object(created, message, parent):
    if created:
        notice = Notice(directory=parent, message=message)
        notice.save()
Beispiel #5
0
def create_notice(creator, message, directory):
    if not check_user_permission(creator, "administrate", directory):
        raise PermissionDenied
    new_notice = Notice(directory=directory, message=message)
    new_notice.save()
    return new_notice
Beispiel #6
0
def create_notice(creator, message, directory):
    if not check_user_permission(creator, "administrate", directory):
        raise PermissionDenied
    new_notice = Notice(directory=directory, message=message)
    new_notice.save()
    return new_notice
Beispiel #7
0
def handle_form(request, current_directory, current_project, current_language, template_vars):

    current_project_pk = None
    if current_project != None:
        current_project_pk = current_project.pk
        
    current_language_pk = None
    if current_language != None:
        current_language_pk = current_language.pk
    
    # Check if the user submitted the form    
    if request.method == 'POST':

        # Reconstruct the NoticeForm with the user data.
        form = NoticeForm(request.POST)
        template_vars['notices_published'] = None

        # Basic validation, only proceed if the form data is valid.
        if form.is_valid():
            
            # Lets save this NoticeForm as a Notice (an RSS item on the website)
            # if it is requsted we do that - ie 'publish_rss' is true.
            if form.cleaned_data['publish_rss'] == True:
            
                proj_filter = Q()
                lang_filter = Q()
                # Find the Projects we want to publish this news to.
                if form.cleaned_data['project_all'] == True:
                    projs = Project.objects.all()
                else:
                    projs = form.cleaned_data['project_selection']

                # Find the Languages we want to publish this news to.
                if form.cleaned_data['language_all'] == True:
                    langs = Language.objects.all()
                else:
                    langs = form.cleaned_data['language_selection']
                # construct the language OR filter
                for lang in langs:
                    lang_filter|=Q(language__exact=lang)

                #
                # We use all the projects that we want to publish this News to.
                #
                # For each project, depending on language selection, publish news into that Directory
                #
                for p in projs:
                    # If the user selected no language, and not "every lang", then just use the project's directory.    
                    if form.cleaned_data['language_selection'] == [] and form.cleaned_data['language_all'] == False:
                        # Publish this Notice, using the project's Directory object
                        new_notice = Notice()
                        new_notice.message = form.cleaned_data['message']
                        new_notice.directory = p.directory
                        new_notice.save()

                        if template_vars['notices_published'] == None:
                            template_vars['notices_published'] = []
                        template_vars['notices_published'].append("Published to Project %s" % p.fullname)
                            
                    else:
                        # Find the languages we want to restrict publishing News to, for this particular Project.
                        # Lets find the TranslationProject to find the directory object to use.
                        translationprojects_to_publish_to = TranslationProject.objects.filter(lang_filter,project__exact=p).distinct()
                        for tp in translationprojects_to_publish_to:
                            # Publish this Notice, using the translation project's Directory obejct
                            new_notice = Notice()
                            new_notice.message = form.cleaned_data['message']
                            new_notice.directory = tp.directory
                            new_notice.save()

                            if template_vars['notices_published'] == None:
                                template_vars['notices_published'] = []
                            template_vars['notices_published'].append("Published to Translation Project %s" % tp.fullname)

                #
                # We use all the languages that we want to publish this News to, and for each lang, publish news into that Directory
                # We only need to check if the user selected no projects - the case of selected projects and languages is covered above.

                # If the user selected no project, and not "every proj", then just use the languages's directory.   
                if form.cleaned_data['project_selection'] == [] and form.cleaned_data['project_all'] == False:
                    for l in langs:
                        # Publish this Notice, using the languages's Directory object
                        new_notice = Notice()
                        new_notice.message = form.cleaned_data['message']
                        new_notice.directory = l.directory
                        new_notice.save()

                        if template_vars['notices_published'] == None:
                            template_vars['notices_published'] = []
                        template_vars['notices_published'].append("Published to Language %s" % l.fullname)
                            

            # If we want to email it , then do that.
            if form.cleaned_data['send_email'] == True:

                email_header = form.cleaned_data['email_header']
                proj_filter = Q()
                lang_filter = Q()
                # Find users to send email too, based on project
                if form.cleaned_data['project_all'] == True:
                    projs = Project.objects.all()
                else:
                    projs = form.cleaned_data['project_selection']
                # Construct the project OR filter
                for proj in projs:
                    proj_filter|=Q(projects__exact=proj)

                # Find users to send email too, based on language
                if form.cleaned_data['language_all'] == True:
                    langs = Language.objects.all()
                else:
                    langs = form.cleaned_data['language_selection']
                # construct the language OR filter
                for lang in langs:
                    lang_filter|=Q(languages__exact=lang)
            
                # Generate a list of pootleprofile objects, which are linked to Users and their emails. 
                #

                # Take into account 'only active users' flag from the form.
                if form.cleaned_data['restrict_to_active_users'] == True:
                    to_list = PootleProfile.objects.filter(lang_filter,proj_filter).distinct().exclude(submission=None).exclude(suggestion=None).exclude(suggester=None)
                else:
                    # Grab all appropriate Profiles.
                    to_list = PootleProfile.objects.filter(lang_filter,proj_filter).distinct()

                to_list_emails = []
                for person in to_list:
                    #Check if the User object here as permissions
                    if not check_profile_permission(person, 'view', form.cleaned_data['directory']):
                        continue
                    if person.user.email != '':
                        to_list_emails.append(person.user.email)        
                        if template_vars['notices_published'] == None:
                            template_vars['notices_published'] = []
                        template_vars['notices_published'].append("Sent an email to %s" % person.user.email)
    
                # The rest of the email settings 
                from_email = DEFAULT_FROM_EMAIL
                message = form.cleaned_data['message']

                # Send the email to the list of people
                send_mail(email_header, message, from_email, to_list_emails, fail_silently=True)        

        # Finally return a blank Form to allow user to continue publishing notices
        # with our defaults
        form = NoticeForm(initial = noticeform_initial_dict(current_directory,\
                current_project_pk, current_language_pk) )

    else:
        # Not a POST method. Return a default starting state of the form
        form = NoticeForm(initial = noticeform_initial_dict(current_directory,\
                current_project_pk, current_language_pk) )

    return form