def snapshot_settings_form(self): if self.request.method == 'POST': form = SnapshotSettingsForm(self.request.POST, self.request.FILES, domain=self.domain_object, is_superuser=self.request.user.is_superuser) return form proj = self.published_snapshot if self.published_snapshot else self.domain_object initial = { 'case_sharing': json.dumps(proj.case_sharing), 'publish_on_submit': True, 'share_multimedia': self.published_snapshot.multimedia_included if self.published_snapshot else True, } init_attribs = ['default_timezone', 'project_type', 'license'] if self.published_snapshot: init_attribs.extend(['title', 'description', 'short_description']) if self.published_snapshot.yt_id: initial['video'] = 'http://www.youtube.com/watch?v=%s' % self.published_snapshot.yt_id for attr in init_attribs: initial[attr] = getattr(proj, attr) return SnapshotSettingsForm(initial=initial, domain=self.domain_object, is_superuser=self.request.user.is_superuser)
def snapshot_settings_form(self): if self.request.method == 'POST': form = SnapshotSettingsForm(self.request.POST, self.request.FILES) form.dom = self.domain_object return form proj = self.published_snapshot if self.published_snapshot else self.domain_object initial = { 'case_sharing': json.dumps(proj.case_sharing), 'publish_on_submit': True, 'share_multimedia': self.published_snapshot.multimedia_included if self.published_snapshot else True, } init_attribs = ['default_timezone', 'project_type', 'license'] if self.published_snapshot: init_attribs.extend(['title', 'description', 'short_description']) if self.published_snapshot.yt_id: initial['video'] = 'http://www.youtube.com/watch?v=%s' % self.published_snapshot.yt_id for attr in init_attribs: initial[attr] = getattr(proj, attr) return SnapshotSettingsForm(initial=initial)
def create_snapshot(request, domain): # todo: refactor function into smaller pieces domain = Domain.get_by_name(domain) can_publish_as_org = domain.get_organization() and request.couch_user.is_org_admin(domain.get_organization().name) def render_with_ctxt(snapshot=None, error_msg=None): ctxt = {'error_message': error_msg} if error_msg else {} ctxt.update({'domain': domain.name, 'form': form, 'app_forms': app_forms, 'can_publish_as_org': can_publish_as_org, 'autocomplete_fields': ('project_type', 'phone_model', 'user_type', 'city', 'country', 'region')}) if snapshot: ctxt.update({'published_as_org': snapshot.publisher == 'organization', 'author': snapshot.author}) else: ctxt.update({'published_as_org': request.POST.get('publisher', '') == 'organization', 'author': request.POST.get('author', '')}) return render(request, 'domain/create_snapshot.html', ctxt) if request.method == 'GET': form = SnapshotSettingsForm(initial={ 'default_timezone': domain.default_timezone, 'case_sharing': json.dumps(domain.case_sharing), 'project_type': domain.project_type, 'share_multimedia': True, 'license': domain.license, 'publish_on_submit': True, }) snapshots = list(domain.snapshots()) published_snapshot = snapshots[0] if snapshots else domain published_apps = {} if published_snapshot is not None: initial={ 'default_timezone': published_snapshot.default_timezone, 'case_sharing': json.dumps(published_snapshot.case_sharing), 'project_type': published_snapshot.project_type, 'license': published_snapshot.license, 'title': published_snapshot.title, 'share_multimedia': published_snapshot.multimedia_included, 'description': published_snapshot.description, 'short_description': published_snapshot.short_description, 'publish_on_submit': True, } if published_snapshot.yt_id: initial['video'] = 'http://www.youtube.com/watch?v=%s' % published_snapshot.yt_id form = SnapshotSettingsForm(initial=initial) for app in published_snapshot.full_applications(): base_app_id = app.copy_of if domain == published_snapshot else app.copied_from.copy_of published_apps[base_app_id] = app app_forms = [] for app in domain.applications(): app = app.get_latest_saved() or app if published_snapshot and app.copy_of in published_apps: original = published_apps[app.copy_of] app_forms.append((app, SnapshotApplicationForm(initial={ 'publish': True, 'name': original.name, 'description': original.description, 'deployment_date': original.deployment_date, 'user_type': original.user_type, 'attribution_notes': original.attribution_notes, 'phone_model': original.phone_model, }, prefix=app.id))) else: app_forms.append((app, SnapshotApplicationForm(initial={'publish': (published_snapshot is None or published_snapshot == domain)}, prefix=app.id))) return render_with_ctxt(snapshot=published_snapshot) elif request.method == 'POST': form = SnapshotSettingsForm(request.POST, request.FILES) form.dom = domain app_forms = [] publishing_apps = False for app in domain.applications(): app = app.get_latest_saved() or app app_forms.append((app, SnapshotApplicationForm(request.POST, prefix=app.id))) publishing_apps = publishing_apps or request.POST.get("%s-publish" % app.id, False) if not publishing_apps: messages.error(request, "Cannot publish a project without applications to CommCare Exchange") return render_with_ctxt() current_user = request.couch_user if not current_user.is_eula_signed(): messages.error(request, 'You must agree to our eula to publish a project to Exchange') return render_with_ctxt() if not form.is_valid(): messages.error(request, _("There are some problems with your form. Please address these issues and try again.")) return render_with_ctxt() new_license = request.POST['license'] if request.POST.get('share_multimedia', False): app_ids = form._get_apps_to_publish() media = domain.all_media(from_apps=app_ids) for m_file in media: if domain.name not in m_file.shared_by: m_file.shared_by.append(domain.name) # set the license of every multimedia file that doesn't yet have a license set if not m_file.license: m_file.update_or_add_license(domain.name, type=new_license) m_file.save() old = domain.published_snapshot() new_domain = domain.save_snapshot() new_domain.license = new_license new_domain.description = request.POST['description'] new_domain.short_description = request.POST['short_description'] new_domain.project_type = request.POST['project_type'] new_domain.title = request.POST['title'] new_domain.multimedia_included = request.POST.get('share_multimedia', '') == 'on' new_domain.publisher = request.POST.get('publisher', None) or 'user' if request.POST.get('video'): new_domain.yt_id = form.cleaned_data['video'] new_domain.author = request.POST.get('author', None) new_domain.is_approved = False publish_on_submit = request.POST.get('publish_on_submit', "no") == "yes" image = form.cleaned_data['image'] if image: new_domain.image_path = image.name new_domain.image_type = image.content_type elif request.POST.get('old_image', False): new_domain.image_path = old.image_path new_domain.image_type = old.image_type new_domain.save() if publish_on_submit: _publish_snapshot(request, domain, published_snapshot=new_domain) else: new_domain.published = False new_domain.save() if image: im = Image.open(image) out = cStringIO.StringIO() im.thumbnail((200, 200), Image.ANTIALIAS) im.save(out, new_domain.image_type.split('/')[-1]) new_domain.put_attachment(content=out.getvalue(), name=image.name) elif request.POST.get('old_image', False): new_domain.put_attachment(content=old.fetch_attachment(old.image_path), name=new_domain.image_path) for application in new_domain.full_applications(): original_id = application.copied_from._id if request.POST.get("%s-publish" % original_id, False): application.name = request.POST["%s-name" % original_id] application.description = request.POST["%s-description" % original_id] date_picked = request.POST["%s-deployment_date" % original_id] try: date_picked = dateutil.parser.parse(date_picked) if date_picked.year > 2009: application.deployment_date = date_picked except Exception: pass #if request.POST.get("%s-name" % original_id): application.phone_model = request.POST["%s-phone_model" % original_id] application.attribution_notes = request.POST["%s-attribution_notes" % original_id] application.user_type = request.POST["%s-user_type" % original_id] if not new_domain.multimedia_included: application.multimedia_map = None application.save() else: application.delete() if new_domain is None: return render_with_ctxt(error_message=_('Version creation failed; please try again')) if publish_on_submit: messages.success(request, _("Created a new version of your app. This version will be posted to CommCare Exchange pending approval by admins.")) else: messages.success(request, _("Created a new version of your app.")) return redirect('domain_snapshot_settings', domain.name)
def create_snapshot(request, domain): # todo: refactor function into smaller pieces domain = Domain.get_by_name(domain) can_publish_as_org = domain.get_organization( ) and request.couch_user.is_org_admin(domain.get_organization().name) def render_with_ctxt(snapshot=None, error_msg=None): ctxt = {'error_message': error_msg} if error_msg else {} ctxt.update({ 'domain': domain.name, 'form': form, 'app_forms': app_forms, 'can_publish_as_org': can_publish_as_org, 'autocomplete_fields': ('project_type', 'phone_model', 'user_type', 'city', 'country', 'region') }) if snapshot: ctxt.update({ 'published_as_org': snapshot.publisher == 'organization', 'author': snapshot.author }) else: ctxt.update({ 'published_as_org': request.POST.get('publisher', '') == 'organization', 'author': request.POST.get('author', '') }) return render(request, 'domain/create_snapshot.html', ctxt) if request.method == 'GET': form = SnapshotSettingsForm( initial={ 'default_timezone': domain.default_timezone, 'case_sharing': json.dumps(domain.case_sharing), 'project_type': domain.project_type, 'share_multimedia': True, 'license': domain.license, 'publish_on_submit': True, }) snapshots = list(domain.snapshots()) published_snapshot = snapshots[0] if snapshots else domain published_apps = {} if published_snapshot is not None: initial = { 'default_timezone': published_snapshot.default_timezone, 'case_sharing': json.dumps(published_snapshot.case_sharing), 'project_type': published_snapshot.project_type, 'license': published_snapshot.license, 'title': published_snapshot.title, 'share_multimedia': published_snapshot.multimedia_included, 'description': published_snapshot.description, 'short_description': published_snapshot.short_description, 'publish_on_submit': True, } if published_snapshot.yt_id: initial[ 'video'] = 'http://www.youtube.com/watch?v=%s' % published_snapshot.yt_id form = SnapshotSettingsForm(initial=initial) for app in published_snapshot.full_applications(): base_app_id = app.copy_of if domain == published_snapshot else app.copied_from.copy_of published_apps[base_app_id] = app app_forms = [] for app in domain.applications(): app = app.get_latest_saved() or app if published_snapshot and app.copy_of in published_apps: original = published_apps[app.copy_of] app_forms.append((app, SnapshotApplicationForm(initial={ 'publish': True, 'name': original.name, 'description': original.description, 'deployment_date': original.deployment_date, 'user_type': original.user_type, 'attribution_notes': original.attribution_notes, 'phone_model': original.phone_model, }, prefix=app.id))) else: app_forms.append((app, SnapshotApplicationForm(initial={ 'publish': (published_snapshot is None or published_snapshot == domain) }, prefix=app.id))) return render_with_ctxt(snapshot=published_snapshot) elif request.method == 'POST': form = SnapshotSettingsForm(request.POST, request.FILES) form.dom = domain app_forms = [] publishing_apps = False for app in domain.applications(): app = app.get_latest_saved() or app app_forms.append( (app, SnapshotApplicationForm(request.POST, prefix=app.id))) publishing_apps = publishing_apps or request.POST.get( "%s-publish" % app.id, False) if not publishing_apps: messages.error( request, "Cannot publish a project without applications to CommCare Exchange" ) return render_with_ctxt() current_user = request.couch_user if not current_user.is_eula_signed(): messages.error( request, 'You must agree to our eula to publish a project to Exchange') return render_with_ctxt() if not form.is_valid(): messages.error( request, _("There are some problems with your form. Please address these issues and try again." )) return render_with_ctxt() new_license = request.POST['license'] if request.POST.get('share_multimedia', False): app_ids = form._get_apps_to_publish() media = domain.all_media(from_apps=app_ids) for m_file in media: if domain.name not in m_file.shared_by: m_file.shared_by.append(domain.name) # set the license of every multimedia file that doesn't yet have a license set if not m_file.license: m_file.update_or_add_license(domain.name, type=new_license) m_file.save() old = domain.published_snapshot() new_domain = domain.save_snapshot() new_domain.license = new_license new_domain.description = request.POST['description'] new_domain.short_description = request.POST['short_description'] new_domain.project_type = request.POST['project_type'] new_domain.title = request.POST['title'] new_domain.multimedia_included = request.POST.get( 'share_multimedia', '') == 'on' new_domain.publisher = request.POST.get('publisher', None) or 'user' if request.POST.get('video'): new_domain.yt_id = form.cleaned_data['video'] new_domain.author = request.POST.get('author', None) new_domain.is_approved = False publish_on_submit = request.POST.get('publish_on_submit', "no") == "yes" image = form.cleaned_data['image'] if image: new_domain.image_path = image.name new_domain.image_type = image.content_type elif request.POST.get('old_image', False): new_domain.image_path = old.image_path new_domain.image_type = old.image_type new_domain.save() if publish_on_submit: _publish_snapshot(request, domain, published_snapshot=new_domain) else: new_domain.published = False new_domain.save() if image: im = Image.open(image) out = cStringIO.StringIO() im.thumbnail((200, 200), Image.ANTIALIAS) im.save(out, new_domain.image_type.split('/')[-1]) new_domain.put_attachment(content=out.getvalue(), name=image.name) elif request.POST.get('old_image', False): new_domain.put_attachment(content=old.fetch_attachment( old.image_path), name=new_domain.image_path) for application in new_domain.full_applications(): original_id = application.copied_from._id if request.POST.get("%s-publish" % original_id, False): application.name = request.POST["%s-name" % original_id] application.description = request.POST["%s-description" % original_id] date_picked = request.POST["%s-deployment_date" % original_id] try: date_picked = dateutil.parser.parse(date_picked) if date_picked.year > 2009: application.deployment_date = date_picked except Exception: pass #if request.POST.get("%s-name" % original_id): application.phone_model = request.POST["%s-phone_model" % original_id] application.attribution_notes = request.POST[ "%s-attribution_notes" % original_id] application.user_type = request.POST["%s-user_type" % original_id] if not new_domain.multimedia_included: application.multimedia_map = None application.save() else: application.delete() if new_domain is None: return render_with_ctxt( error_message=_('Version creation failed; please try again')) if publish_on_submit: messages.success( request, _("Created a new version of your app. This version will be posted to CommCare Exchange pending approval by admins." )) else: messages.success(request, _("Created a new version of your app.")) return redirect('domain_snapshot_settings', domain.name)
def create_snapshot(request, domain): domain = Domain.get_by_name(domain) #latest_applications = [app.get_latest_saved() or app for app in domain.applications()] if request.method == 'GET': form = SnapshotSettingsForm(initial={ 'default_timezone': domain.default_timezone, 'case_sharing': json.dumps(domain.case_sharing), 'city': domain.city, 'country': domain.country, 'region': domain.region, 'project_type': domain.project_type, 'share_multimedia': True, 'license': domain.license }) published_snapshot = domain.published_snapshot() or domain published_apps = {} if published_snapshot is not None: form = SnapshotSettingsForm(initial={ 'default_timezone': published_snapshot.default_timezone, 'case_sharing': json.dumps(published_snapshot.case_sharing), 'city': published_snapshot.city, 'country': published_snapshot.country, 'region': published_snapshot.region, 'project_type': published_snapshot.project_type, 'license': published_snapshot.license, 'title': published_snapshot.title, 'author': published_snapshot.author, 'share_multimedia': True, 'description': published_snapshot.description }) for app in published_snapshot.full_applications(): if domain == published_snapshot: published_apps[app._id] = app else: published_apps[app.original_doc] = app app_forms = [] for app in domain.applications(): app = app.get_latest_saved() or app if published_snapshot and app._id in published_apps: original = published_apps[app._id] app_forms.append((app, SnapshotApplicationForm(initial={ 'publish': True, 'name': original.name, 'description': original.description, 'deployment_date': original.deployment_date, 'user_type': original.user_type, 'attribution_notes': original.attribution_notes, 'phone_model': original.phone_model, }, prefix=app.id))) else: app_forms.append((app, SnapshotApplicationForm(initial={'publish': (published_snapshot is None or published_snapshot == domain)}, prefix=app.id))) return render_to_response(request, 'domain/create_snapshot.html', {'domain': domain.name, 'form': form, #'latest_applications': latest_applications, 'app_forms': app_forms, 'autocomplete_fields': ('project_type', 'phone_model', 'user_type', 'city', 'country', 'region')}) elif request.method == 'POST': form = SnapshotSettingsForm(request.POST, request.FILES) app_forms = [] publishing_apps = False for app in domain.applications(): app = app.get_latest_saved() or app app_forms.append((app, SnapshotApplicationForm(request.POST, prefix=app.id))) publishing_apps = publishing_apps or request.POST.get("%s-publish" % app.id, False) if not publishing_apps: messages.error(request, "Cannot publish a project without applications to CommCare Exchange") return render_to_response(request, 'domain/create_snapshot.html', {'domain': domain.name, 'form': form, 'app_forms': app_forms, 'autocomplete_fields': ('project_type', 'phone_model', 'user_type', 'city', 'country', 'region')}) if not form.is_valid(): return render_to_response(request, 'domain/create_snapshot.html', {'domain': domain.name, 'form': form, #'latest_applications': latest_applications, 'app_forms': app_forms, 'autocomplete_fields': ('project_type', 'phone_model', 'user_type', 'city', 'country', 'region')}) if request.POST.get('share_multimedia', False): media = domain.all_media() for m_file in media: if domain.name not in m_file.shared_by: m_file.shared_by.append(domain.name) m_file.licenses[domain.name] = domain.license m_file.save() old = domain.published_snapshot() new_domain = domain.save_snapshot() if request.POST['license'] in LICENSES.keys(): new_domain.license = request.POST['license'] new_domain.description = request.POST['description'] new_domain.project_type = request.POST['project_type'] new_domain.region = request.POST['region'] new_domain.city = request.POST['city'] new_domain.country = request.POST['country'] new_domain.title = request.POST['title'] new_domain.author = request.POST['author'] for snapshot in domain.snapshots(): if snapshot.published and snapshot._id != new_domain._id: snapshot.published = False snapshot.save() new_domain.is_approved = False new_domain.published = True image = form.cleaned_data['image'] if image: new_domain.image_path = image.name new_domain.image_type = image.content_type elif request.POST.get('old_image', False): new_domain.image_path = old.image_path new_domain.image_type = old.image_type new_domain.save() if image: im = Image.open(image) out = cStringIO.StringIO() im.thumbnail((200, 200), Image.ANTIALIAS) im.save(out, new_domain.image_type.split('/')[-1]) new_domain.put_attachment(content=out.getvalue(), name=image.name) elif request.POST.get('old_image', False): new_domain.put_attachment(content=old.fetch_attachment(old.image_path), name=new_domain.image_path) for application in new_domain.full_applications(): original_id = application.original_doc if request.POST.get("%s-publish" % original_id, False): application.name = request.POST["%s-name" % original_id] application.description = request.POST["%s-description" % original_id] date_picked = request.POST["%s-deployment_date" % original_id].split('-') if len(date_picked) > 1: if int(date_picked[0]) > 2009 and date_picked[1] and date_picked[2]: application.deployment_date = datetime.datetime(int(date_picked[0]), int(date_picked[1]), int(date_picked[2])) #if request.POST.get("%s-name" % original_id): application.phone_model = request.POST["%s-phone_model" % original_id] application.attribution_notes = request.POST["%s-attribution_notes" % original_id] application.user_type = request.POST["%s-user_type" % original_id] application.save() else: application.delete() if new_domain is None: return render_to_response(request, 'domain/snapshot_settings.html', {'domain': domain.name, 'form': form, #'latest_applications': latest_applications, 'app_forms': app_forms, 'error_message': 'Snapshot creation failed; please try again'}) messages.success(request, "Created snapshot. The snapshot will be posted to CommCare Exchange pending approval by admins.") return redirect('domain_snapshot_settings', domain.name)