本文整理汇总了Python中tendenci.apps.user_groups.models.GroupMembership类的典型用法代码示例。如果您正苦于以下问题:Python GroupMembership类的具体用法?Python GroupMembership怎么用?Python GroupMembership使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GroupMembership类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: group_membership_self_add
def group_membership_self_add(request, slug, user_id):
group = get_object_or_404(Group, slug=slug)
user = get_object_or_404(User, pk=user_id)
if not has_view_perm(request.user,'user_groups.view_group', group) and not group.allow_self_add:
raise Http403
group_membership = GroupMembership.objects.filter(member=user, group=group)
if not group_membership:
group_membership = GroupMembership()
group_membership.group = group
group_membership.member = user
group_membership.creator_id = user.id
group_membership.creator_username = user.username
group_membership.owner_id = user.id
group_membership.owner_username = user.username
group_membership.save()
EventLog.objects.log(instance=group_membership)
messages.add_message(request, messages.SUCCESS, 'Successfully added yourself to group %s' % group)
else:
messages.add_message(request, messages.INFO, 'You are already in the group %s' % group)
return HttpResponseRedirect(reverse('group.search'))
开发者ID:jkimma,项目名称:tendenci,代码行数:28,代码来源:views.py
示例2: groupmembership_bulk_add
def groupmembership_bulk_add(request, group_slug,
form_class=GroupMembershipBulkForm,
template_name="user_groups/member_add.html"):
group = get_object_or_404(Group, slug=group_slug)
user_count = User.objects.all().count()
if user_count > 1000:
return HttpResponseRedirect(reverse('group.adduser_redirect'))
if request.method == 'POST':
form = form_class(group, request.POST)
if form.is_valid():
members = form.cleaned_data['members']
old_members = GroupMembership.objects.filter(group=group)
#delete removed groupmemberships
if members:
for old_m in old_members:
try:
members.get(pk=old_m.member.pk)
except User.DoesNotExist:
EventLog.objects.log(instance=old_m)
old_m.delete()
else: #when members is None
for old_m in old_members:
EventLog.objects.log(instance=old_m)
old_m.delete()
for m in members:
try:
group_membership = GroupMembership.objects.get(group=group, member=m)
except GroupMembership.DoesNotExist:
group_membership = GroupMembership(group=group, member=m)
group_membership.creator_id = request.user.id
group_membership.creator_username = request.user.username
group_membership.role=form.cleaned_data['role']
group_membership.status=form.cleaned_data['status']
group_membership.status_detail=form.cleaned_data['status_detail']
group_membership.owner_id = request.user.id
group_membership.owner_username = request.user.username
group_membership.save()
EventLog.objects.log(instance=group_membership)
return HttpResponseRedirect(group.get_absolute_url())
else:
member_label = request.GET.get('member_label', 'username')
form = form_class(group, member_label=member_label)
return render_to_response(template_name, locals(), context_instance=RequestContext(request))
开发者ID:jiwonlee777,项目名称:tendenci,代码行数:52,代码来源:views.py
示例3: save
def save(self, *args, **kwargs):
if self.pk:
old_officer = Officer.objects.get(pk=self.pk)
if old_officer.position and old_officer.position.group and old_officer.user:
gm = GroupMembership.objects.filter(member=old_officer.user, group=old_officer.position.group)[:1]
if gm:
gm[0].delete()
super(Officer, self).save(*args, **kwargs)
officer = self
if officer.position:
if officer.position.group and officer.user:
try:
GroupMembership.objects.get(member=officer.user, group=officer.position.group)
except:
GroupMembership.add_to_group(member=officer.user, group=officer.position.group)
return officer
开发者ID:RMUCapstone-MIS590,项目名称:tendenci,代码行数:19,代码来源:models.py
示例4: save
def save(self):
data = self.cleaned_data
#delete old memberships
old_memberships = GroupMembership.objects.filter(member=self.user
).exclude(group__type__in=['system_generated', 'membership'])
if not self.editor.is_superuser:
old_memberships = old_memberships.filter(group__show_as_option=True,
group__allow_self_remove=True)
if self.editor.profile.is_member:
old_memberships = old_memberships.filter(Q(group__allow_anonymous_view=True)
| Q(group__allow_user_view=True)
| Q(group__allow_member_view=True))
else:
old_memberships = old_memberships.filter(Q(group__allow_anonymous_view=True)
| Q(group__allow_user_view=True))
for old_m in old_memberships:
if old_m.group not in data['groups']:
#print "membership to %s deleted" % old_m.group
log_defaults = {
'event_id' : 223000,
'event_data': '%s (%d) deleted by %s' % (old_m._meta.object_name, old_m.pk, self.editor),
'description': '%s deleted' % old_m._meta.object_name,
'user': self.editor,
'request': self.request,
'instance': old_m,
}
EventLog.objects.log(**log_defaults)
old_m.delete()
#create new memberships
for group in data['groups']:
[group_membership] = GroupMembership.objects.filter(group=group, member=self.user)[:1] or [None]
if not group_membership:
group_membership = GroupMembership(group=group, member=self.user)
group_membership.creator_id = self.editor.id
group_membership.creator_username = self.editor.username
group_membership.owner_id = self.editor.id
group_membership.owner_username = self.editor.username
group_membership.save()
log_defaults = {
'event_id' : 221000,
'event_data': '%s (%d) added by %s' % (group_membership._meta.object_name, group_membership.pk, self.editor),
'description': '%s added' % group_membership._meta.object_name,
'user': self.editor,
'request': self.request,
'instance': group_membership,
}
EventLog.objects.log(**log_defaults)
开发者ID:RMUCapstone-MIS590,项目名称:tendenci,代码行数:54,代码来源:forms.py
示例5: execute_function
def execute_function(self, entry, value, user=None):
if self.field_function == "GroupSubscription":
if value:
for val in self.choices.split(","):
group = Group.objects.get(name=val.strip())
if user:
try:
group_membership = GroupMembership.objects.get(group=group, member=user)
except GroupMembership.DoesNotExist:
group_membership = GroupMembership(group=group, member=user)
group_membership.creator_id = user.id
group_membership.creator_username = user.username
group_membership.role = "subscriber"
group_membership.owner_id = user.id
group_membership.owner_username = user.username
group_membership.save()
开发者ID:bpotterschipul,项目名称:tendenci,代码行数:16,代码来源:models.py
示例6: execute_function
def execute_function(self, entry, value, user=None):
if self.field_function == "GroupSubscription":
if value:
for val in self.function_params.split(','):
group = Group.objects.get(name=val)
if user:
try:
group_membership = GroupMembership.objects.get(group=group, member=user)
except GroupMembership.DoesNotExist:
group_membership = GroupMembership(group=group, member=user)
group_membership.creator_id = user.id
group_membership.creator_username = user.username
group_membership.role = 'subscriber'
group_membership.owner_id = user.id
group_membership.owner_username = user.username
group_membership.save()
else:
entry.subscribe(group) # subscribe form-entry to a group
开发者ID:Spacepoints,项目名称:tendenci,代码行数:18,代码来源:models.py
示例7: subscribe_to_newsletter_interactive
def subscribe_to_newsletter_interactive(request, group_slug):
group = get_object_or_404(Group, slug=group_slug,
allow_self_add=True,
status_detail='active')
[groupmembership] = GroupMembership.objects.filter(
group=group,
member=request.user,
status=True,
status_detail='active')[:1] or [None]
if not groupmembership:
groupmembership = GroupMembership.add_to_group(group=group,
member=request.user)
if groupmembership.subscribe_to_newsletter():
messages.success(request, _('Successfully subscribed to Newsletters.'))
return redirect(reverse('group.detail', kwargs={'group_slug': group_slug}))
开发者ID:goetzk,项目名称:tendenci,代码行数:18,代码来源:views.py
示例8: group_membership_self_add
def group_membership_self_add(request, slug, user_id):
group = get_object_or_404(Group, slug=slug)
user = get_object_or_404(User, pk=user_id)
if not has_view_perm(request.user,'user_groups.view_group', group) and not group.allow_self_add:
raise Http403
group_membership = GroupMembership.objects.filter(member=user, group=group)
if not group_membership:
group_membership = GroupMembership()
group_membership.group = group
group_membership.member = user
group_membership.creator_id = user.id
group_membership.creator_username = user.username
group_membership.owner_id = user.id
group_membership.owner_username = user.username
group_membership.save()
log_defaults = {
'event_id' : 221000,
'event_data': '%s (%d) added by %s' % (group_membership._meta.object_name, group_membership.pk, request.user),
'description': '%s added' % group_membership._meta.object_name,
'user': request.user,
'request': request,
'instance': group_membership,
}
EventLog.objects.log(**log_defaults)
messages.add_message(request, messages.SUCCESS, 'Successfully added yourself to group %s' % group)
else:
messages.add_message(request, messages.INFO, 'You are already in the group %s' % group)
return HttpResponseRedirect(reverse('group.search'))
开发者ID:legutierr,项目名称:tendenci,代码行数:36,代码来源:views.py
示例9: forwards
def forwards(self, orm):
from django.contrib.auth.models import User
from tendenci.apps.profiles.models import Profile
from tendenci.apps.subscribers.models import GroupSubscription, SubscriberData
from tendenci.apps.user_groups.models import GroupMembership
phone_labels = ["Phone", "phone", "Phone Number", "phone number", "Home Phone", "Cell Phone"]
for sub in GroupSubscription.objects.all():
sub_email = sub.email
if sub_email:
if sub.subscriber and sub.subscriber.creator:
user = sub.subscriber.creator
else:
if User.objects.filter(email=sub_email).exists():
user = User.objects.filter(email=sub_email).order_by('last_login')[0]
elif User.objects.filter(username=sub_email).exists():
user = User.objects.filter(username=sub_email).order_by('last_login')[0]
else:
user = User(username=sub_email[:30], email=sub_email, is_active=False)
if SubscriberData.objects.filter(field_label="First Name", subscription=sub):
user.first_name = SubscriberData.objects.filter(field_label="First Name", subscription=sub)[0].value
if SubscriberData.objects.filter(field_label="Last Name", subscription=sub):
user.last_name = SubscriberData.objects.filter(field_label="Last Name", subscription=sub)[0].value
user.save()
profile = Profile(user=user, owner=user, creator=user)
if SubscriberData.objects.filter(field_label__in=phone_labels, subscription=sub):
profile.phone = SubscriberData.objects.filter(field_label__in=phone_labels, subscription=sub)[0].value
profile.save()
try:
group_membership = GroupMembership.objects.get(group=sub.group, member=user)
except GroupMembership.DoesNotExist:
group_membership = GroupMembership(group=sub.group, member=user)
group_membership.creator_id = user.id
group_membership.creator_username = user.username
group_membership.role = 'subscriber'
group_membership.owner_id = user.id
group_membership.owner_username = user.username
group_membership.save()
开发者ID:BillTheBest,项目名称:tendenci,代码行数:39,代码来源:0002_convert_subscribers.py
示例10: save
def save(self):
data = self.cleaned_data
#delete old memberships
for old_m in self.old_memberships:
if old_m.group not in data['groups']:
print "membership to %s deleted" % old_m.group
log_defaults = {
'event_id' : 223000,
'event_data': '%s (%d) deleted by %s' % (old_m._meta.object_name, old_m.pk, self.editor),
'description': '%s deleted' % old_m._meta.object_name,
'user': self.editor,
'request': self.request,
'instance': old_m,
}
EventLog.objects.log(**log_defaults)
old_m.delete()
#create new memberships
for group in data['groups']:
try:
group_membership = GroupMembership.objects.get(group=group, member=self.user)
except GroupMembership.DoesNotExist:
group_membership = GroupMembership(group=group, member=self.user)
group_membership.creator_id = self.editor.id
group_membership.creator_username = self.editor.username
group_membership.owner_id = self.editor.id
group_membership.owner_username = self.editor.username
group_membership.save()
log_defaults = {
'event_id' : 221000,
'event_data': '%s (%d) added by %s' % (group_membership._meta.object_name, group_membership.pk, self.editor),
'description': '%s added' % group_membership._meta.object_name,
'user': self.editor,
'request': self.request,
'instance': group_membership,
}
EventLog.objects.log(**log_defaults)
开发者ID:a2call,项目名称:tendenci,代码行数:41,代码来源:forms.py
示例11: do_user_import
def do_user_import(request, user, user_object_dict, setting_dict):
"""
the real work is here - do the insert or update
"""
insert = not bool(user) # insert or update
user = user or User() # existing or new user
override = setting_dict['override'] # update ALL fields
# insert/update user
for field in user_field_names:
if field == 'password' or field == 'username' or \
(not insert and field in setting_dict['key']):
continue
if field in user_object_dict:
if override:
setattr(user, field, user_object_dict[field])
else:
# fill out the blank field only
if getattr(user, field) == '':
setattr(user, field, user_object_dict[field])
if insert:
if 'username' in user_object_dict: # set username
user.username = user_object_dict['username']
# generate if not username
user.username = get_unique_username(user)
if 'password' in user_object_dict and (insert or override):
user.set_password(user_object_dict['password'])
if not user.password:
user.set_password(User.objects.make_random_password(length=8))
user.is_active = bool(setting_dict['interactive'])
if not bool(validate_email(user.email)):
user.email = '' # if not valid; empty it out
# loop through user properties; truncate at max_length
for key, value in user.__dict__.items():
max_length = 90
try:
max_length = User._meta.get_field(key).max_length
except FieldDoesNotExist:
max_length = None
if max_length: # truncate per max_length field attribute
setattr(user, key, value[:max_length])
# username and email required
if user.username and user.email:
# insert/update record
if insert:
user.save(force_insert=True)
else:
user.save(force_update=True)
try: # get or create
profile = user.profile
except Profile.DoesNotExist:
profile = Profile.objects.create(user=user,
creator=request.user,
creator_username=request.user.username,
owner=request.user,
owner_username=request.user.username,
)
for field in profile_field_names:
if field in user_object_dict:
if override:
setattr(profile, field, user_object_dict[field])
else:
# fill out the blank field only
if getattr(profile, field) == '':
setattr(profile, field, user_object_dict[field])
profile.save()
# add to group
if setting_dict['group']:
try:
gm = GroupMembership.objects.get(group=setting_dict['group'],
member=user)
except GroupMembership.DoesNotExist:
gm = GroupMembership()
gm.member = user
gm.group = setting_dict['group']
gm.creator_id = request.user.id
gm.creator_username = request.user.username
gm.owner_id = request.user.id
gm.owner_username = request.user.username
gm.status = 1
gm.status_detail = 'active'
gm.save()
return user
开发者ID:tendenci,项目名称:tendenci,代码行数:99,代码来源:utils.py
示例12: register
def register(request, success_url=None,
form_class=RegistrationForm, profile_callback=None,
template_name='registration/registration_form.html',
event_id=None,
extra_context=None):
"""
Allow a new user to register an account.
Following successful registration, issue a redirect; by default,
this will be whatever URL corresponds to the named URL pattern
``registration_complete``, which will be
``/accounts/register/complete/`` if using the included URLConf. To
change this, point that named pattern at another URL, or pass your
preferred URL as the keyword argument ``success_url``.
By default, ``registration.forms.RegistrationForm`` will be used
as the registration form; to change this, pass a different form
class as the ``form_class`` keyword argument. The form class you
specify must have a method ``save`` which will create and return
the new ``User``, and that method must accept the keyword argument
``profile_callback`` (see below).
To enable creation of a site-specific user profile object for the
new user, pass a function which will create the profile object as
the keyword argument ``profile_callback``. See
``RegistrationManager.create_inactive_user`` in the file
``models.py`` for details on how to write this function.
By default, use the template
``registration/registration_form.html``; to change this, pass the
name of a template as the keyword argument ``template_name``.
**Required arguments**
None.
**Optional arguments**
``form_class``
The form class to use for registration.
``extra_context``
A dictionary of variables to add to the template context. Any
callable object in this dictionary will be called to produce
the end result which appears in the context.
``profile_callback``
A function which will be used to create a site-specific
profile instance for the new ``User``.
``success_url``
The URL to redirect to on successful registration.
``template_name``
A custom template to use.
**Context:**
``form``
The registration form.
Any extra variables supplied in the ``extra_context`` argument
(see above).
**Template:**
registration/registration_form.html or ``template_name`` keyword
argument.
"""
# check if this site allows self registration, if not, redirect to login page
allow_self_registration = get_setting('module', 'users', 'selfregistration')
if not allow_self_registration:
return HttpResponseRedirect(reverse('auth_login'))
form_params = {}
if request.session.get('form_params', None):
form_params = request.session.pop('form_params')
if request.method == 'POST':
form = form_class(data=request.POST, files=request.FILES, **form_params)
if form.is_valid():
# This is for including a link in the reg email back to the event viewed
event = None
if event_id: # the user signed up via an event
from tendenci.apps.events.models import Event
event = get_object_or_404(Event, pk=event_id)
new_user = form.save(profile_callback=profile_callback, event=event)
# success_url needs to be dynamically generated here; setting a
# a default value using reverse() will cause circular-import
# problems with the default URLConf for this application, which
# imports this file.
# add to the default group(s)
default_user_groups =[g.strip() for g in (get_setting('module', 'users', 'defaultusergroup')).split(',')]
if default_user_groups:
from tendenci.apps.user_groups.models import Group, GroupMembership
from django.db.models import Q
for group_name in default_user_groups:
#.........这里部分代码省略.........
开发者ID:BIGGANI,项目名称:tendenci,代码行数:101,代码来源:views.py
示例13: group_membership_self_add
def group_membership_self_add(request, slug, user_id):
group = get_object_or_404(Group, slug=slug)
user = get_object_or_404(User, pk=user_id)
if not has_view_perm(request.user, "user_groups.view_group", group) and not group.allow_self_add:
raise Http403
group_membership = GroupMembership.objects.filter(member=user, group=group)
if not group_membership:
group_membership = GroupMembership()
group_membership.group = group
group_membership.member = user
group_membership.creator_id = user.id
group_membership.creator_username = user.username
group_membership.owner_id = user.id
group_membership.owner_username = user.username
group_membership.save()
EventLog.objects.log(instance=group_membership)
if group_membership.is_newsletter_subscribed:
group_membership.subscribe_to_newsletter()
messages.add_message(
request, messages.SUCCESS, _("Successfully added yourself to group %(grp)s" % {"grp": group})
)
else:
messages.add_message(request, messages.INFO, _("You are already in the group %(grp)s" % {"grp": group}))
return HttpResponseRedirect(reverse("group.search"))
开发者ID:RMUCapstone-MIS590,项目名称:tendenci,代码行数:33,代码来源:views.py
示例14: authenticate
def authenticate(self, *args, **kwargs):
"""Authenticate user using social credentials
Authentication is made if this is the correct backend, backend
verification is made by kwargs inspection for current backend
name presence.
"""
# Validate backend and arguments. Require that the Social Auth
# response be passed in as a keyword argument, to make sure we
# don't match the username/password calling conventions of
# authenticate.
if not (self.name and kwargs.get(self.name) and 'response' in kwargs):
return None
response = kwargs.get('response')
request = kwargs.get('request')
details = self.get_user_details(response)
uid = self.get_user_id(details, response)
is_new = False
try:
social_user = UserSocialAuth.objects.select_related('user')\
.get(provider=self.name,
uid=uid)
except UserSocialAuth.DoesNotExist:
user = kwargs.get('user')
if user is None: # new user
if not CREATE_USERS:
return None
email = details.get('email')
if email and ASSOCIATE_BY_MAIL:
# try to associate accounts registered with the same email
# address, only if it's a single object. ValueError is
# raised if multiple objects are returned
try:
user = User.objects.get(email=email)
except MultipleObjectsReturned:
raise ValueError('Not unique email address supplied')
except User.DoesNotExist:
user = None
if not user:
username = self.username(details)
user = User.objects.create_user(username=username,
email=email)
is_new = True
default_user_groups =[g.strip() for g in (get_setting('module', 'users', 'defaultusergroup')).split(',')]
if default_user_groups:
from tendenci.apps.user_groups.models import Group, GroupMembership
from django.db.models import Q
for group_name in default_user_groups:
groups = Group.objects.filter(Q(name=group_name) | Q(label=group_name)).filter(allow_self_add=1, status=1, status_detail='active')
if groups:
group = groups[0]
else:
# group doesnot exist, so create the group
group = Group()
group.name = group_name
group.label = group_name
group.type = 'distribution'
group.show_as_option = 1
group.allow_self_add = 1
group.allow_self_remove = 1
group.creator = user
group.creator_username = user.username
group.owner = user
group.owner_username = user.username
try:
group.save()
except:
group = None
if group:
gm = GroupMembership()
gm.group = group
gm.member = user
gm.creator_id = user.id
gm.creator_username = user.username
gm.owner_id = user.id
gm.owner_username = user.username
gm.save()
log_defaults = {
'event_id' : 121000,
'event_data': '%s (%d) self added by form' % (user._meta.object_name, user.pk),
'description': '%s self added' % user._meta.object_name,
'user': user,
'request': request,
'instance': user,
}
EventLog.objects.log(**log_defaults)
social_user = self.associate_auth(user, uid, response, details)
else:
# This account was registered to another user, so we raise an
# error in such case and the view should decide what to do on
# at this moment, merging account is not an option because that
# would imply update user references on other apps, that's too
# much intrusive
if 'user' in kwargs and kwargs['user'] != social_user.user:
raise ValueError('Account already in use.', social_user)
user = social_user.user
#.........这里部分代码省略.........
开发者ID:a2call,项目名称:tendenci,代码行数:101,代码来源:__init__.py
示例15: handle
def handle(self, *args, **options):
"""
Converts subscribers to users, maintains group memberships, removes subscribers.
"""
from tendenci.apps.subscribers.models import SubscriberData, GroupSubscription
from tendenci.apps.user_groups.models import GroupMembership
from tendenci.apps.profiles.models import Profile
if GroupSubscription.objects.exists():
for sub in GroupSubscription.objects.all():
sub_email = sub.email
if sub_email:
if not User.objects.filter(email=sub_email).exists():
print "Creating new user for group subscription %s." % sub.id
if len(sub_email) <= 30:
username = sub_email
else:
username = sub_email.split("@")[0]
user = User(
username = username,
email = sub_email,
is_active = False
)
if sub.subscriber_id:
user.first_name = sub.subscriber.get_first_name()
user.last_name = sub.subscriber.get_last_name()
phone = sub.subscriber.get_phone_number()
else:
if SubscriberData.objects.filter(field_label="First Name").filter(subscription_id=sub.id):
user.first_name = SubscriberData.objects.filter(field_label="First Name").filter(subscription_id=sub.id)[0].value
if SubscriberData.objects.filter(field_label="Last Name").filter(subscription_id=sub.id):
user.last_name = SubscriberData.objects.filter(field_label="Last Name").filter(subscription_id=sub.id)[0].value
if SubscriberData.objects.filter(field_label__in=["Phone", "phone", "Phone Number", "phone number", "Home Phone", "Cell Phone"]).filter(subscription_id=sub.id):
phone = SubscriberData.objects.filter(field_label__in=["Phone", "phone", "Phone Number", "phone number", "Home Phone", "Cell Phone"]).filter(subscription_id=sub.id)[0].value
else:
phone = ''
user.save()
profile = Profile(
user = user,
creator = user,
creator_username = user.username,
owner = user,
owner_username = user.username,
phone = phone,
allow_anonymous_view = False
)
profile.save()
else:
print "User with matching email found for group subscription %s." % sub.id
user = User.objects.filter(email=sub_email).order_by('last_login')[0]
if not GroupMembership.objects.filter(group_id=sub.group_id).filter(member_id=user.id).exists():
print "Creating a group membership for user %s and group %s." % (user.id, sub.group_id)
gm = GroupMembership(
group_id = sub.group_id,
member_id = user.id,
role = "subscriber"
)
gm.save()
else:
print "Group membership already exists for user %s and group %s." % (user.id, sub.group_id)
print "Deleting group subscription %s." % sub.id
sub.delete()
else:
print "No email field found for subscriber %s." % sub.id
else:
print "No group subscriptions exist."
开发者ID:DeepInTheCode,项目名称:tendenci,代码行数:76,代码来源:convert_subscribers.py
注:本文中的tendenci.apps.user_groups.models.GroupMembership类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论