本文整理汇总了Python中tendenci.apps.perms.utils.update_perms_and_save函数的典型用法代码示例。如果您正苦于以下问题:Python update_perms_and_save函数的具体用法?Python update_perms_and_save怎么用?Python update_perms_and_save使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了update_perms_and_save函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_model
def save_model(self, request, object, form, change):
"""
update the permissions backend
"""
print('enter save_model')
instance = form.save(commit=False)
update_perms_and_save(request, form, instance) # Returns perms
return instance
示例2: save_model
def save_model(self, request, object, form, change):
instance = form.save(commit=False)
instance = update_perms_and_save(request, form, instance)
if instance.meta:
meta = instance.meta
else:
meta = MetaTags()
meta.title = form.cleaned_data["meta_title"]
meta.description = form.cleaned_data["meta_description"]
meta.keywords = form.cleaned_data["meta_keywords"]
meta.canonical_url = form.cleaned_data["meta_canonical_url"]
meta.save()
instance.meta = meta
instance.save()
# notifications
if not request.user.profile.is_superuser:
# send notification to administrators
recipients = get_notice_recipients("module", "pages", "pagerecipients")
notice_type = "page_added"
if change:
notice_type = "page_edited"
if recipients:
if notification:
extra_context = {"object": instance, "request": request}
notification.send_emails(recipients, notice_type, extra_context)
return instance
示例3: add
def add(request, form_class=LocationForm, template_name="locations/add.html"):
if has_perm(request.user,'locations.add_location'):
if request.method == "POST":
form = form_class(request.POST, request.FILES, user=request.user)
if form.is_valid():
location = form.save(commit=False)
# update all permissions and save the model
location = update_perms_and_save(request, form, location)
if 'photo_upload' in form.cleaned_data:
photo = form.cleaned_data['photo_upload']
if photo:
location.save(photo=photo)
msg_string = 'Successfully added %s' % location
messages.add_message(request, messages.SUCCESS, _(msg_string))
return HttpResponseRedirect(reverse('location', args=[location.slug]))
else:
form = form_class(user=request.user)
return render_to_resp(request=request, template_name=template_name,
context={'form':form})
else:
raise Http403
示例4: add
def add(request, form_class=FormForm, template_name="forms/add.html"):
if not has_perm(request.user,'forms.add_form'):
raise Http403
PricingFormSet = inlineformset_factory(Form, Pricing, form=PricingForm, extra=2, can_delete=False)
formset = PricingFormSet()
if request.method == "POST":
form = form_class(request.POST, user=request.user)
if form.is_valid():
form_instance = form.save(commit=False)
# save form and associated pricings
form_instance = update_perms_and_save(request, form, form_instance)
formset = PricingFormSet(request.POST, instance=form_instance)
if formset.is_valid():
# update_perms_and_save does not appear to consider ManyToManyFields
for method in form.cleaned_data['payment_methods']:
form_instance.payment_methods.add(method)
formset.save()
messages.add_message(request, messages.SUCCESS, _('Successfully added %(f)s' % {'f':form_instance}))
return HttpResponseRedirect(reverse('form_field_update', args=[form_instance.pk]))
else:
form = form_class(user=request.user)
return render_to_resp(request=request, template_name=template_name, context={
'form':form,
'formset': formset,
})
示例5: photoset_add
def photoset_add(request, form_class=PhotoSetAddForm, template_name="photos/photo-set/add.html"):
""" Add a photo set """
# if no permission; permission exception
if not has_perm(request.user,'photos.add_photoset'):
raise Http403
if request.method == "POST":
if request.POST["action"] == "add":
form = form_class(request.POST, user=request.user)
if form.is_valid():
photo_set = form.save(commit=False)
photo_set.author = request.user
# update all permissions and save the model
photo_set = update_perms_and_save(request, form, photo_set)
checklist_update('add-album')
messages.add_message(request, messages.SUCCESS, _('Successfully added photo set!'))
return HttpResponseRedirect(reverse('photos_batch_add', kwargs={'photoset_id':photo_set.id}))
else:
form = form_class(user=request.user)
return render_to_response(template_name, {
"photoset_form": form,
}, context_instance=RequestContext(request))
示例6: edit
def edit(request, id, form_class=LocationForm, template_name="locations/edit.html"):
location = get_object_or_404(Location, pk=id)
if has_perm(request.user,'locations.change_location',location):
if request.method == "POST":
form = form_class(request.POST, request.FILES, instance=location, user=request.user)
if form.is_valid():
location = form.save(commit=False)
# update all permissions and save the model
location = update_perms_and_save(request, form, location)
if 'photo_upload' in form.cleaned_data:
photo = form.cleaned_data['photo_upload']
if photo:
location.save(photo=photo)
msg_string = 'Successfully updated %s' % location
messages.add_message(request, messages.SUCCESS, _(msg_string))
return HttpResponseRedirect(reverse('location', args=[location.slug]))
else:
form = form_class(instance=location, user=request.user)
return render_to_response(template_name, {'location': location, 'form':form},
context_instance=RequestContext(request))
else:
raise Http403
示例7: edit
def edit(request, id, form_class=DirectoryForm, template_name="directories/edit.html"):
directory = get_object_or_404(Directory, pk=id)
if not has_perm(request.user,'directories.change_directory', directory):
raise Http403
form = form_class(request.POST or None, request.FILES or None,
instance=directory,
user=request.user)
del form.fields['payment_method']
if not request.user.profile.is_superuser:
del form.fields['pricing']
del form.fields['list_type']
if request.method == "POST":
if form.is_valid():
directory = form.save(commit=False)
if directory.logo:
try:
directory.logo.file.seek(0)
except IOError:
directory.logo = None
# update all permissions and save the model
directory = update_perms_and_save(request, form, directory)
msg_string = 'Successfully updated %s' % directory
messages.add_message(request, messages.SUCCESS, _(msg_string))
return HttpResponseRedirect(reverse('directory', args=[directory.slug]))
return render_to_response(template_name, {'directory': directory, 'form':form},
context_instance=RequestContext(request))
示例8: mark_as_paid
def mark_as_paid(request, id, template_name="invoices/mark-as-paid.html"):
"""
Makes a payment-record with a specified date/time
payment method and payment amount.
"""
invoice = get_object_or_404(Invoice, pk=id)
if not has_perm(request.user, "payments.change_payment"):
raise Http403
if request.method == "POST":
form = MarkAsPaidForm(request.POST)
if form.is_valid():
# make payment record
payment = form.save(user=request.user, invoice=invoice, commit=False)
payment = update_perms_and_save(request, form, payment)
# update invoice; make accounting entries
action_taken = invoice.make_payment(payment.creator, payment.amount)
if action_taken:
EventLog.objects.log(instance=invoice)
messages.add_message(request, messages.SUCCESS, _("Payment successfully made"))
return redirect(invoice)
else:
form = MarkAsPaidForm(initial={"amount": invoice.balance, "submit_dt": datetime.now()})
return render_to_response(
template_name, {"invoice": invoice, "form": form}, context_instance=RequestContext(request)
)
示例9: edit
def edit(request, id=None, form_class=HelpFileForm, template_name="help_files/edit.html"):
help_file = get_object_or_404(HelpFile, pk=id)
if has_perm(request.user,'help_files.change_helpfile', help_file):
if request.method == "POST":
form = form_class(request.POST, instance=help_file, user=request.user)
if form.is_valid():
help_file = form.save(commit=False)
# add all permissions and save the model
help_file = update_perms_and_save(request, form, help_file)
form.save_m2m()
msg_string = 'Successfully edited %s' % help_file
messages.add_message(request, messages.SUCCESS, _(msg_string))
# send notification to administrator(s) and module recipient(s)
recipients = get_notice_recipients('module', 'help_files', 'helpfilerecipients')
# if recipients and notification:
# notification.send_emails(recipients,'help_file_added', {
# 'object': help_file,
# 'request': request,
# })
return HttpResponseRedirect(reverse('help_file.details', args=[help_file.slug]))
else:
form = form_class(instance=help_file, user=request.user)
return render_to_response(template_name, {'help_file': help_file, 'form':form},
context_instance=RequestContext(request))
else:
raise Http403
示例10: edit
def edit(request, id, form_class=ArticleForm,
category_form_class=CategoryForm,
template_name="articles/edit.html"):
article = get_object_or_404(Article, pk=id)
content_type = get_object_or_404(ContentType, app_label='articles',
model='article')
if has_perm(request.user, 'articles.change_article', article):
if request.method == "POST":
form = form_class(request.POST, request.FILES or None, instance=article, user=request.user)
categoryform = category_form_class(content_type,
request.POST,)
if form.is_valid() and categoryform.is_valid():
article = form.save()
article.update_category_subcategory(
categoryform.cleaned_data['category'],
categoryform.cleaned_data['sub_category']
)
# update all permissions and save the model
update_perms_and_save(request, form, article)
msg_string = 'Successfully updated %s' % str(article)
messages.add_message(request, messages.SUCCESS, _(msg_string))
return HttpResponseRedirect(reverse('article', args=[article.slug]))
else:
form = form_class(instance=article, user=request.user)
category = Category.objects.get_for_object(article, 'category')
sub_category = Category.objects.get_for_object(article, 'sub_category')
initial_category_form_data = {
'app_label': 'articles',
'model': 'article',
'pk': article.pk,
'category': getattr(category, 'name', '0'),
'sub_category': getattr(sub_category, 'name', '0')
}
categoryform = category_form_class(content_type,
initial=initial_category_form_data,)
return render_to_resp(request=request, template_name=template_name,
context={'article': article,
'form': form,
'categoryform': categoryform,})
else:
raise Http403
示例11: add
def add(request, form_class=FileForm,template_name="files/add.html"):
# check permission
if not has_perm(request.user, 'files.add_file'):
raise Http403
if request.method == "POST":
form = form_class(request.POST, request.FILES, user=request.user)
if form.is_valid():
file = form.save(commit=False)
# set up the user information
file.creator = request.user
file.creator_username = request.user.username
file.owner = request.user
file.owner_username = request.user.username
file.save()
# update all permissions and save the model
file = update_perms_and_save(request, form, file)
#setup categories
category = Category.objects.get_for_object(file, 'category')
sub_category = Category.objects.get_for_object(file, 'sub_category')
## update the category of the file
category_removed = False
category = file.file_cat.name if file.file_cat else u''
if category:
Category.objects.update(file, category, 'category')
else: # remove
category_removed = True
Category.objects.remove(file, 'category')
Category.objects.remove(file, 'sub_category')
if not category_removed:
# update the sub category of the article
sub_category = file.file_sub_cat.name if file.file_sub_cat else u''
if sub_category:
Category.objects.update(file, sub_category, 'sub_category')
else: # remove
Category.objects.remove(file, 'sub_category')
#Save relationships
file.save()
# assign creator permissions
ObjectPermission.objects.assign(file.creator, file)
return HttpResponseRedirect(reverse('file.search'))
else:
form = form_class(user=request.user)
if 'group' in form.fields:
form.fields['group'].initial = Group.objects.get_initial_group_id()
return render_to_response(
template_name, {
'form': form,
}, context_instance=RequestContext(request))
示例12: save
def save(self, *args, **kwargs):
data = self.cleaned_data
counter = 0
files = data.get('files')
tags = data.get('tags')
group = data.get('group')
file_cat = data.get('file_cat', None)
file_sub_cat = data.get('file_sub_cat', None)
is_public = data.get('allow_anonymous_view', False)
for new_file in files:
file = File(
file=new_file,
tags=tags,
group=group,
allow_anonymous_view=is_public,
file_cat=file_cat,
file_sub_cat=file_sub_cat)
file.save()
# update all permissions and save the model
file = update_perms_and_save(self.request, self, file)
#setup categories
category = Category.objects.get_for_object(file, 'category')
sub_category = Category.objects.get_for_object(file, 'sub_category')
## update the category of the file
category_removed = False
category = file.file_cat.name if file.file_cat else u''
if category:
Category.objects.update(file, category, 'category')
else: # remove
category_removed = True
Category.objects.remove(file, 'category')
Category.objects.remove(file, 'sub_category')
if not category_removed:
# update the sub category of the file
sub_category = file.file_sub_cat.name if file.file_sub_cat else u''
if sub_category:
Category.objects.update(file, sub_category, 'sub_category')
else: # remove
Category.objects.remove(file, 'sub_category')
#Save relationships
file.save()
counter += 1
return counter
示例13: save
def save(self, *args, **kwargs):
request = kwargs.get('request')
if request:
del kwargs['request']
report = super(ReportForm, self).save(*args, **kwargs)
if request:
update_perms_and_save(request, self, report)
config_dict = {}
if self.cleaned_data['type'] == "invoices":
for k, v in self.cleaned_data.items():
if "invoice" in k:
config_dict.update({k: v})
config_json = json.dumps(config_dict, cls=DjangoJSONEncoder)
report.config = config_json
report.allow_anonymous_view = False
report.save()
return report
示例14: add
def add(request, form_class=ResumeForm, template_name="resumes/add.html"):
can_add_active = has_perm(request.user, 'resumes.add_resume')
if not any([request.user.profile.is_superuser,
can_add_active,
get_setting('module', 'resumes', 'usercanadd'),
(request.user.profile.is_member and get_setting('module', 'resumes', 'userresumesrequiresmembership'))
]):
raise Http403
if request.method == "POST":
form = form_class(request.POST or None, user=request.user)
if form.is_valid():
resume = form.save(commit=False)
# set it to pending if the user does not have add permission
if not can_add_active:
resume.status_detail = 'pending'
# set up the expiration time based on requested duration
now = datetime.now()
resume.expiration_dt = now + timedelta(days=resume.requested_duration)
resume = update_perms_and_save(request, form, resume)
# we need to save instance first since we need the id for the file path
if request.FILES:
resume.resume_file = request.FILES['resume_file']
resume.resume_file.file.seek(0)
resume.save()
EventLog.objects.log(instance=resume)
if request.user.is_authenticated():
messages.add_message(request, messages.SUCCESS, _('Successfully added %(r)s' % {'r':resume}))
# send notification to administrators
recipients = get_notice_recipients('module', 'resumes', 'resumerecipients')
if recipients:
if notification:
extra_context = {
'object': resume,
'request': request,
}
notification.send_emails(recipients,'resume_added', extra_context)
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('resume.thank_you'))
else:
return HttpResponseRedirect(reverse('resume', args=[resume.slug]))
else:
form = form_class(user=request.user)
return render_to_response(template_name, {'form':form},
context_instance=RequestContext(request))
示例15: save_model
def save_model(self, request, object, form, change):
instance = form.save(request=request, commit=False)
instance = update_perms_and_save(request, form, instance, log=False)
log_defaults = {
'instance': object,
'action': "edit"
}
if not change:
log_defaults['action'] = "add"
EventLog.objects.log(**log_defaults)
return instance