def home(request): if request.user.is_authenticated(): profile = request.user.get_profile() my_groups = profile.groups.exclude(steward=None).order_by('name') curated_groups = Group.get_curated() data = dict(groups=my_groups, curated_groups=curated_groups) return render(request, 'phonebook/home.html', data) else: return render(request, 'phonebook/home.html')
def search(request): num_pages = 0 limit = None nonvouched_only = False picture_only = False people = [] show_pagination = False form = forms.SearchForm(request.GET) groups = None curated_groups = None if form.is_valid(): query = form.cleaned_data.get('q', u'') limit = form.cleaned_data['limit'] vouched = False if form.cleaned_data['nonvouched_only'] else None profilepic = True if form.cleaned_data['picture_only'] else None page = request.GET.get('page', 1) curated_groups = Group.get_curated() # If nothing has been entered don't load any searches. if not (not query and vouched is None and profilepic is None): profiles = UserProfile.search(query, vouched=vouched, photo=profilepic) groups = Group.search(query) paginator = Paginator(profiles, limit) try: people = paginator.page(page) except PageNotAnInteger: people = paginator.page(1) except EmptyPage: people = paginator.page(paginator.num_pages) if len(profiles) == 1 and not groups: return redirect( reverse('profile', args=[people[0].user.username])) if paginator.count > forms.PAGINATION_LIMIT: show_pagination = True num_pages = len(people.paginator.page_range) d = dict(people=people, form=form, limit=limit, nonvouched_only=nonvouched_only, picture_only=picture_only, show_pagination=show_pagination, num_pages=num_pages, groups=groups, curated_groups=curated_groups) if request.is_ajax(): return render(request, 'search_ajax.html', d) return render(request, 'phonebook/search.html', d)
def search(request): num_pages = 0 limit = None nonvouched_only = False picture_only = False people = [] show_pagination = False form = forms.SearchForm(request.GET) groups = None curated_groups = None if form.is_valid(): query = form.cleaned_data.get('q', u'') limit = form.cleaned_data['limit'] vouched = False if form.cleaned_data['nonvouched_only'] else None profilepic = True if form.cleaned_data['picture_only'] else None page = request.GET.get('page', 1) curated_groups = Group.get_curated() # If nothing has been entered don't load any searches. if not (not query and vouched is None and profilepic is None): profiles = UserProfile.search(query, vouched=vouched, photo=profilepic) groups = Group.search(query) paginator = Paginator(profiles, limit) try: people = paginator.page(page) except PageNotAnInteger: people = paginator.page(1) except EmptyPage: people = paginator.page(paginator.num_pages) if len(profiles) == 1 and not groups: return redirect(reverse('profile', args=[people[0].user.username])) if paginator.count > forms.PAGINATION_LIMIT: show_pagination = True num_pages = len(people.paginator.page_range) d = dict(people=people, form=form, limit=limit, nonvouched_only=nonvouched_only, picture_only=picture_only, show_pagination=show_pagination, num_pages=num_pages, groups=groups, curated_groups=curated_groups) if request.is_ajax(): return render(request, 'search_ajax.html', d) return render(request, 'phonebook/search.html', d)
class RegistrationForm(UserForm): username = forms.CharField( label=_lazy(u'Username'), max_length=30, required=False, widget=forms.TextInput(attrs={'placeholder': 'Example: IRC Nickname'})) groups = forms.ModelMultipleChoiceField( widget=forms.CheckboxSelectMultiple, label=_lazy(u'Please indicate the functional areas you are currently ' 'contributing to or would like to contribute to:'), queryset=Group.get_curated(), required=False) skills = forms.CharField( label=_lazy(u'Enter your skills (e.g. Python, Photoshop' ') below:'), required=False) languages = forms.CharField( label=_lazy(u'Mozillians speak many languages (e.g. English, French). ' 'Find and be found by others who speak the same ' 'languages as you.'), required=False) optin = forms.BooleanField( label=_lazy(u"I'm okay with you handling this info as you " u"explain in Mozilla's privacy policy."), widget=forms.CheckboxInput(attrs={'class': 'checkbox'}), required=True) class Meta: model = UserProfile fields = ('ircname', 'website', 'bio', 'photo', 'country', 'region', 'city') widgets = {'bio': forms.Textarea()} def clean_skills(self): if not re.match(r'^[a-zA-Z0-9 .:,-]*$', self.cleaned_data['skills']): raise forms.ValidationError(_(u'Skills can only contain ' 'alphanumeric characters, dashes, ' 'spaces.')) return [s.strip() for s in self.cleaned_data['skills'].lower().split(',') if s and ',' not in s] def clean_languages(self): if not re.match(r'^[a-zA-Z0-9 .:,-]*$', self.cleaned_data['languages']): raise forms.ValidationError(_(u'Languages can only contain ' 'alphanumeric characters, dashes, ' 'spaces.')) return [s.strip() for s in self.cleaned_data['languages'].lower().split(',') if s and ',' not in s] def clean(self): """Make sure geographic fields aren't underspecified.""" cleaned_data = super(RegistrationForm, self).clean() # Rather than raising ValidationErrors for the whole form, we can # add errors to specific fields. if cleaned_data['city'] and not cleaned_data['region']: self._errors['region'] = [_(u'You must specify a region to ' 'specify a city.')] if cleaned_data['region'] and not cleaned_data['country']: self._errors['country'] = [_(u'You must specify a country to ' 'specify a district.')] return cleaned_data def save(self, user): d = self.cleaned_data if 'username' in d: d['ircname'] = d['username'] self.instance.set_membership(Skill, self.cleaned_data['skills']) self.instance.set_membership(Language, self.cleaned_data['languages']) super(RegistrationForm, self).save(user)