Beispiel #1
0
def manage_multimedia(request, domain):
    media = request.project.all_media()
    if request.method == "POST":
        for m_file in media:
            if '%s_tags' % m_file._id in request.POST:
                m_file.tags[domain] = request.POST.get('%s_tags' % m_file._id, '').split(' ')

            if domain not in m_file.shared_by and request.POST.get('%s_shared' % m_file._id, False):
                m_file.shared_by.append(domain)
            elif domain in m_file.shared_by and not request.POST.get('%s_shared' % m_file._id, False):
                m_file.shared_by.remove(domain)

            if '%s_license' % m_file._id in request.POST:
                m_file.update_or_add_license(domain, type=request.POST.get('%s_license' % m_file._id, 'public'))
            m_file.save()
        messages.success(request, "Multimedia updated successfully!")

    return render(request, 'domain/admin/media_manager.html', {'domain': domain,
        'media': [{
            'license': m.license.type if m.license else 'public',
            'shared': domain in m.shared_by,
            'url': m.url(),
            'm_id': m._id,
            'tags': m.tags.get(domain, []),
            'type': m.doc_type
                   } for m in media],
        'licenses': LICENSES.items()
    })
Beispiel #2
0
def manage_multimedia(request, domain):
    media = request.project.all_media()
    if request.method == "POST":
        for m_file in media:
            if '%s_tags' % m_file._id in request.POST:
                m_file.tags[domain] = request.POST.get('%s_tags' % m_file._id,
                                                       '').split(' ')

            if domain not in m_file.shared_by and request.POST.get(
                    '%s_shared' % m_file._id, False):
                m_file.shared_by.append(domain)
            elif domain in m_file.shared_by and not request.POST.get(
                    '%s_shared' % m_file._id, False):
                m_file.shared_by.remove(domain)

            if '%s_license' % m_file._id in request.POST:
                m_file.update_or_add_license(domain,
                                             type=request.POST.get(
                                                 '%s_license' % m_file._id,
                                                 'public'))
            m_file.save()
        messages.success(request, "Multimedia updated successfully!")

    return render(
        request, 'domain/admin/media_manager.html', {
            'domain':
            domain,
            'media': [{
                'license': m.license.type if m.license else 'public',
                'shared': domain in m.shared_by,
                'url': m.url(),
                'm_id': m._id,
                'tags': m.tags.get(domain, []),
                'type': m.doc_type
            } for m in media],
            'licenses':
            LICENSES.items()
        })
Beispiel #3
0
 def options(self):
     return [(code, license_name) for code, license_name in LICENSES.items()]
Beispiel #4
0
 def page_context(self):
     return {
         'media': self.project_media_data,
         'licenses': list(LICENSES.items()),
     }
Beispiel #5
0
 def update_params(self):
     available_licenses = [{'val': code, 'text': license} for code, license in LICENSES.items()]
     self.selected = self.request.GET.get(self.slug,'')
     self.options = available_licenses
Beispiel #6
0
class SnapshotSettingsForm(SnapshotSettingsMixin):
    title = CharField(label=ugettext_noop("Title"),
                      required=True,
                      max_length=100)
    project_type = CharField(label=ugettext_noop("Project Category"),
                             required=True,
                             help_text=ugettext_noop("e.g. MCH, HIV, etc."))
    license = ChoiceField(label=ugettext_noop("License"),
                          required=True,
                          choices=LICENSES.items(),
                          widget=Select(attrs={'class': 'input-xxlarge'}))
    description = CharField(
        label=ugettext_noop("Long Description"),
        required=False,
        widget=forms.Textarea,
        help_text=ugettext_noop(
            "A high-level overview of your project as a whole"))
    short_description = CharField(
        label=ugettext_noop("Short Description"),
        required=False,
        widget=forms.Textarea(attrs={'maxlength': 200}),
        help_text=ugettext_noop(
            "A brief description of your project (max. 200 characters)"))
    share_multimedia = BooleanField(
        label=ugettext_noop("Share all multimedia?"),
        required=False,
        help_text=ugettext_noop(
            "This will allow any user to see and use all multimedia in this project"
        ))
    share_reminders = BooleanField(
        label=ugettext_noop("Share Reminders?"),
        required=False,
        help_text=ugettext_noop(
            "This will publish reminders along with this project"))
    image = forms.ImageField(
        label=ugettext_noop("Exchange image"),
        required=False,
        help_text=ugettext_noop(
            "An optional image to show other users your logo or what your app looks like"
        ))
    video = CharField(
        label=ugettext_noop("Youtube Video"),
        required=False,
        help_text=ugettext_noop(
            "An optional youtube clip to tell users about your app. Please copy and paste a URL to a youtube video"
        ))
    cda_confirmed = BooleanField(
        required=False, label=ugettext_noop("Content Distribution Agreement"))

    def __init__(self, *args, **kw):
        super(SnapshotSettingsForm, self).__init__(*args, **kw)
        self.fields.keyOrder = [
            'title',
            'short_description',
            'description',
            'project_type',
            'image',
            'video',
            'share_multimedia',
            'share_reminders',
            'license',
            'cda_confirmed',
        ]
        self.fields['license'].help_text = \
            render_to_string('domain/partials/license_explanations.html', {
                'extra': _("All un-licensed multimedia files in "
                           "your project will be given this license")
            })
        self.fields['cda_confirmed'].help_text = \
            render_to_string('domain/partials/cda_modal.html')

    def clean_cda_confirmed(self):
        data_cda = self.cleaned_data['cda_confirmed']
        data_publish = self.data.get('publish_on_submit', "no") == "yes"
        if data_publish and data_cda is False:
            raise forms.ValidationError(
                'You must agree to our Content Distribution Agreement to publish your project.'
            )
        return data_cda

    def clean_video(self):
        video = self.cleaned_data['video']
        if not video:
            return video

        def video_id(value):
            # http://stackoverflow.com/questions/4356538/how-can-i-extract-video-id-from-youtubes-link-in-python#answer-7936523
            """
            Examples:
            - http://youtu.be/SA2iWivDJiE
            - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
            - http://www.youtube.com/embed/SA2iWivDJiE
            - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
            """
            query = urlparse(value)
            if query.hostname == 'youtu.be':
                return query.path[1:]
            if query.hostname in ('www.youtube.com', 'youtube.com'):
                if query.path == '/watch':
                    p = parse_qs(query.query)
                    return p['v'][0]
                if query.path[:7] == '/embed/':
                    return query.path.split('/')[2]
                if query.path[:3] == '/v/':
                    return query.path.split('/')[2]
                    # fail?
            return None

        v_id = video_id(video)
        if not v_id:
            raise forms.ValidationError(
                'This is not a correctly formatted youtube URL. Please use a different URL.'
            )
        return v_id

    def clean(self):
        cleaned_data = self.cleaned_data
        sm = cleaned_data["share_multimedia"]
        license = cleaned_data["license"]
        app_ids = self._get_apps_to_publish()

        if sm and license not in self.dom.most_restrictive_licenses(
                apps_to_check=app_ids):
            license_choices = [
                LICENSES[l] for l in self.dom.most_restrictive_licenses(
                    apps_to_check=app_ids)
            ]
            msg = render_to_string('domain/partials/restrictive_license.html',
                                   {'licenses': license_choices})
            self._errors["license"] = self.error_class([msg])

            del cleaned_data["license"]

        sr = cleaned_data["share_reminders"]
        if sr:  # check that the forms referenced by the events in each reminders exist in the project
            referenced_forms = CaseReminderHandler.get_referenced_forms(
                domain=self.dom.name)
            if referenced_forms:
                apps = [Application.get(app_id) for app_id in app_ids]
                app_forms = [
                    f.unique_id for forms in [app.get_forms() for app in apps]
                    for f in forms
                ]
                nonexistent_forms = filter(lambda f: f not in app_forms,
                                           referenced_forms)
                nonexistent_forms = [
                    FormBase.get_form(f) for f in nonexistent_forms
                ]
                if nonexistent_forms:
                    msg = """
                        Your reminders reference forms that are not being published.
                        Make sure the following forms are being published: %s
                    """ % str([f.default_name()
                               for f in nonexistent_forms]).strip('[]')
                    self._errors["share_reminders"] = self.error_class([msg])

        return cleaned_data

    def _get_apps_to_publish(self):
        app_ids = []
        for d, val in self.data.iteritems():
            d = d.split('-')
            if len(d) < 2:
                continue
            if d[1] == 'publish' and val == 'on':
                app_ids.append(d[0])

        return app_ids
Beispiel #7
0
 def page_context(self):
     return {
         'media': self.project_media_data,
         'licenses': list(LICENSES.items()),
     }
Beispiel #8
0
 def update_params(self):
     available_licenses = [{"val": code, "text": license} for code, license in LICENSES.items()]
     self.selected = self.request.GET.get(self.slug, "")
     self.options = available_licenses
Beispiel #9
0
 def options(self):
     return [(code, license_name)
             for code, license_name in LICENSES.items()]
Beispiel #10
0
 def update_params(self):
     available_licenses = [{'val': code, 'text': license} for code, license in LICENSES.items()]
     self.selected = self.request.GET.get(self.slug,'')
     self.options = available_licenses