def entries_view(self, request, form_id): """ Displays the form entries in a HTML table with option to export as CSV file. """ if request.POST.get("back"): change_url = admin_url(Form, "change", form_id) return HttpResponseRedirect(change_url) form = get_object_or_404(Form, id=form_id) entries_form = EntriesForm(form, request, request.POST or None) delete_entries_perm = "%s.delete_formentry" % FormEntry._meta.app_label can_delete_entries = request.user.has_perm(delete_entries_perm) submitted = entries_form.is_valid() if submitted: if request.POST.get("export"): response = HttpResponse(content_type="text/csv") timestamp = slugify(datetime.now().ctime()) fname = "%s-%s.csv" % (slugify(form.title), timestamp) header = "attachment; filename=%s" % fname response["Content-Disposition"] = header queue = StringIO() delimiter = settings.FORMS_CSV_DELIMITER try: csv = writer(queue, delimiter=delimiter) writerow = csv.writerow except TypeError: queue = BytesIO() delimiter = bytes(delimiter, encoding="utf-8") csv = writer(queue, delimiter=delimiter) writerow = lambda row: csv.writerow([c.encode("utf-8") if hasattr(c, "encode") else c for c in row]) writerow(entries_form.columns()) for row in entries_form.rows(csv=True): writerow(row) data = queue.getvalue() response.write(data) return response elif request.POST.get("delete") and can_delete_entries: selected = request.POST.getlist("selected") if selected: entries = FormEntry.objects.filter(id__in=selected) count = entries.count() if count > 0: entries.delete() message = ungettext("1 entry deleted", "%(count)s entries deleted", count) info(request, message % {"count": count}) template = "admin/clubhouse_forms/entries.html" context = {"title": _("View Entries"), "entries_form": entries_form, "opts": self.model._meta, "original": form, "can_delete_entries": can_delete_entries, "submitted": submitted} return render_to_response(template, context, RequestContext(request))
def export_view(self, request, form_id): """ Exports the form entries in either a HTML table or CSV file. """ if request.POST.get("back"): change_url = admin_url(Form, "change", form_id) return HttpResponseRedirect(change_url) form = get_object_or_404(Form, id=form_id) export_form = ExportForm(form, request, request.POST or None) submitted = export_form.is_valid() if submitted: if request.POST.get("export"): response = HttpResponse(mimetype="text/csv") timestamp = slugify(datetime.now().ctime()) fname = "%s-%s.csv" % (form.slug, timestamp) header = "attachment; filename=%s" % fname response["Content-Disposition"] = header csv = writer(response, delimiter=settings.FORMS_CSV_DELIMITER) csv.writerow(export_form.columns()) for rows in export_form.rows(): csv.writerow(rows) return response template = "admin/forms/export.html" context = { "title": _("Export Entries"), "export_form": export_form, "submitted": submitted } return render_to_response(template, context, RequestContext(request))
def export_view(self, request, form_id): """ Exports the form entries in either a HTML table or CSV file. """ if request.POST.get("back"): change_url = admin_url(Form, "change", form_id) return HttpResponseRedirect(change_url) form = get_object_or_404(Form, id=form_id) export_form = ExportForm(form, request, request.POST or None) submitted = export_form.is_valid() if submitted: if request.POST.get("export"): response = HttpResponse(mimetype="text/csv") timestamp = slugify(datetime.now().ctime()) fname = "%s-%s.csv" % (form.slug, timestamp) header = "attachment; filename=%s" % fname response["Content-Disposition"] = header csv = writer(response, delimiter=settings.FORMS_CSV_DELIMITER) csv.writerow(export_form.columns()) for rows in export_form.rows(): csv.writerow(rows) return response template = "admin/forms/export.html" context = {"title": _("Export Entries"), "export_form": export_form, "submitted": submitted} return render_to_response(template, context, RequestContext(request))
def save(self, *args, **kwargs): kwargs["commit"] = False user = super(AddUserForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: username = "******" % self.cleaned_data if not username.strip(): username = self.cleaned_data["email"].split("@")[0] qs = User.objects.exclude(id=self.instance.id) # @UndefinedVariable PyDev limitation ("exclude") user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) user.save() # Save profile model. ProfileFieldsForm(self.data, self.files, instance=user.profile).save() settings.use_editable() #if (settings.ACCOUNTS_VERIFICATION_REQUIRED or # settings.ACCOUNTS_APPROVAL_REQUIRED): # user.is_active = False # user.save() return user
def get_slug(cv_dict): """ emulates the cv model get_slug function with the dict data """ return slugify(u"{0}-{1}-{2}".format(cv_dict["Given First Name"], cv_dict.get("Given Middle Name", ''), cv_dict.get("Surname", '')))
def get_slug(cv_dict): """ emulates the cv model get_slug function with the dict data """ return slugify(u"{0}-{1}-{2}".format(cv_dict["Given First Name"], cv_dict.get("Given Middle Name", ''), cv_dict.get("Surname", '')))
def test_page_slug_has_correct_lang(self): """ Test that slug generation is done for the default language and not the active one. """ from collections import OrderedDict from django.utils.translation import get_language, activate from mezzanine.utils.urls import slugify default_language = get_language() code_list = OrderedDict(settings.LANGUAGES) del code_list[default_language] title_1 = "Title firt language" title_2 = "Title second language" page, _ = RichTextPage.objects.get_or_create(title=title_1) for code in code_list: try: activate(code) except: pass else: break # No valid language found page.delete() return page.title = title_2 page.save() self.assertEqual(page.get_slug(), slugify(title_1)) self.assertEqual(page.title, title_2) activate(default_language) self.assertEqual(page.title, title_1) page.delete()
def test_page_slug_has_correct_lang(self): """ Test that slug generation is done for the default language and not the active one. """ from django.utils.translation import get_language, activate from django.utils.datastructures import SortedDict from mezzanine.utils.urls import slugify default_language = get_language() code_list = SortedDict(settings.LANGUAGES) del code_list[default_language] title_1 = "Title firt language" title_2 = "Title second language" page, _ = RichTextPage.objects.get_or_create(title=title_1) for code in code_list: try: activate(code) except: pass else: break # No valid language found page.delete() return page.title = title_2 page.save() self.assertEqual(page.get_slug(), slugify(title_1)) self.assertEqual(page.title, title_2) activate(default_language) self.assertEqual(page.title, title_1) page.delete()
def render(self, context): if self.is_variable: real_slug = template.Variable(self.slug).resolve(context) else: real_slug = self.slug if isinstance(self.template_name, template.Variable): real_template = self.template_name.resolve(context) else: real_template = self.template_name real_title = real_slug real_slug = slugify(real_slug) # Eventually we want to pass the whole context to the template so that # users have the maximum of flexibility of what to do in there. if self.with_template: new_ctx = {} new_ctx.update(context.flatten()) try: flatblock = None if self.cache_time != 0: cache_key = settings.MEZZANINE_BLOCKS_CACHE_PREFIX + real_slug flatblock = cache.get(cache_key) if flatblock is None: if self.is_rich: _klass = RichBlock elif self.is_image: _klass = ImageBlock else: _klass = Block flatblock, created = _klass.objects.get_or_create( slug=real_slug, defaults = {'title': real_title} ) if self.cache_time != 0: if self.cache_time is None or self.cache_time == 'None': logger.debug("Caching %s for the cache's default timeout" % (real_slug,)) cache.set(cache_key, flatblock) else: logger.debug("Caching %s for %s seconds" % (real_slug, str(self.cache_time))) cache.set(cache_key, flatblock, int(self.cache_time)) else: logger.debug("Don't cache %s" % (real_slug,)) if self.with_template: tmpl = loader.get_template(real_template) new_ctx.update({'flatblock':flatblock}) return tmpl.render(new_ctx) else: return flatblock.content except Block.DoesNotExist: return ''
def render(self, context): if self.is_variable: real_slug = template.Variable(self.slug).resolve(context) else: real_slug = self.slug if isinstance(self.template_name, template.Variable): real_template = self.template_name.resolve(context) else: real_template = self.template_name real_title = real_slug real_slug = slugify(real_slug) # Eventually we want to pass the whole context to the template so that # users have the maximum of flexibility of what to do in there. if self.with_template: new_ctx = template.Context({}) new_ctx.update(context) try: flatblock = None if self.cache_time != 0: cache_key = settings.MEZZANINE_BLOCKS_CACHE_PREFIX + real_slug flatblock = cache.get(cache_key) if flatblock is None: if self.is_rich: _klass = RichBlock elif self.is_image: _klass = ImageBlock else: _klass = Block flatblock, created = _klass.objects.get_or_create( slug=real_slug, defaults = {'title': real_title} ) if self.cache_time != 0: if self.cache_time is None or self.cache_time == 'None': logger.debug("Caching %s for the cache's default timeout" % (real_slug,)) cache.set(cache_key, flatblock) else: logger.debug("Caching %s for %s seconds" % (real_slug, str(self.cache_time))) cache.set(cache_key, flatblock, int(self.cache_time)) else: logger.debug("Don't cache %s" % (real_slug,)) if self.with_template: tmpl = loader.get_template(real_template) new_ctx.update({'flatblock':flatblock}) return tmpl.render(new_ctx) else: return flatblock.content except Block.DoesNotExist: return ''
def entries_view(self, request, form_id): """ Displays the form entries in a HTML table with option to export as CSV file. """ if request.POST.get("back"): change_url = admin_url(Form, "change", form_id) return HttpResponseRedirect(change_url) form = get_object_or_404(Form, id=form_id) entries_form = EntriesForm(form, request, request.POST or None) delete_entries_perm = "%s.delete_formentry" % FormEntry._meta.app_label can_delete_entries = request.user.has_perm(delete_entries_perm) submitted = entries_form.is_valid() if submitted: if request.POST.get("export"): response = HttpResponse(content_type="text/csv") timestamp = slugify(datetime.now().ctime()) fname = "%s-%s.csv" % (form.slug, timestamp) header = "attachment; filename=%s" % fname response["Content-Disposition"] = header queue = StringIO() delimiter = settings.FORMS_CSV_DELIMITER try: csv = writer(queue, delimiter=delimiter) writerow = csv.writerow except TypeError: queue = BytesIO() delimiter = bytes(delimiter, encoding="utf-8") csv = writer(queue, delimiter=delimiter) writerow = lambda row: csv.writerow([ c.encode("utf-8") if hasattr(c, "encode") else c for c in row ]) writerow(entries_form.columns()) for row in entries_form.rows(csv=True): writerow(row) data = queue.getvalue() response.write(data) return response elif request.POST.get("delete") and can_delete_entries: selected = request.POST.getlist("selected") if selected: entries = FormEntry.objects.filter(id__in=selected) count = entries.count() if count > 0: entries.delete() message = ungettext("1 entry deleted", "%(count)s entries deleted", count) info(request, message % {"count": count}) template = "admin/forms/entries.html" context = { "title": _("View Entries"), "entries_form": entries_form, "opts": self.model._meta, "original": form, "can_delete_entries": can_delete_entries, "submitted": submitted } return render(request, template, context)
def get_slug(self): """ Allows subclasses to implement their own slug creation logic. """ attr = "name" # Get self.title_xx where xx is the default language, if any. # Get self.title otherwise. return slugify(getattr(self, attr, None) or self.name)
def get_slug(self): """ Recursively build the slug from the chain of parents. """ slug = slugify(self.title) if self.parent is not None: return "%s/%s" % (self.parent.slug, slug) return slug
def get_slug(self): """ Recursively build the slug from the chain of parents. """ slug = slugify(self.title) if self.parent is not None: return "%s/%s" % (self.parent.slug, slug) return slug
def save(self, *args, **kwargs): """ Create the new user. If no username is supplied (may be hidden via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so that if profile pages are enabled, we still have something to use as the profile's slug. """ kwargs["commit"] = False user = super(ProfileForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: try: username = ("%(first_name)s %(last_name)s" % self.cleaned_data).strip() except KeyError: username = "" if not username: username = self.cleaned_data["email"].split("@")[0] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) elif self._signup: try: user.set_unusable_password() except AttributeError: # This could happen if using a custom user model that # doesn't inherit from Django's AbstractBaseUser. pass user.save() try: profile = get_profile_for_user(user) profile_form = self.get_profile_fields_form() profile_form(self.data, self.files, instance=profile).save() except ProfileNotConfigured: pass if self._signup: settings.use_editable() if (settings.ACCOUNTS_VERIFICATION_REQUIRED or settings.ACCOUNTS_APPROVAL_REQUIRED): user.is_active = False user.save() else: token = default_token_generator.make_token(user) user = authenticate(uidb36=int_to_base36(user.id), token=token, is_active=True) return user
def save(self, *args, **kwargs): """ Create the new user. If no username is supplied (may be hidden via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so that if profile pages are enabled, we still have something to use as the profile's slug. """ kwargs["commit"] = False user = super(ProfileForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: try: username = ("%(first_name)s %(last_name)s" % self.cleaned_data).strip() except KeyError: username = "" if not username: username = self.cleaned_data["email"].split("@")[0] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) elif self._signup: try: user.set_unusable_password() except AttributeError: # This could happen if using a custom user model that # doesn't inherit from Django's AbstractBaseUser. pass user.save() try: profile = get_profile_for_user(user) profile_form = self.get_profile_fields_form() profile_form(self.data, self.files, instance=profile).save() except ProfileNotConfigured: pass if self._signup: settings.use_editable() if (settings.ACCOUNTS_VERIFICATION_REQUIRED or settings.ACCOUNTS_APPROVAL_REQUIRED): user.is_active = False user.save() else: token = default_token_generator.make_token(user) user = authenticate(uidb36=int_to_base36(user.id), token=token, is_active=True) return user
def get_slug(self): """ Allows subclasses to implement their own slug creation logic. """ attr = "title" if settings.USE_MODELTRANSLATION: from modeltranslation.utils import build_localized_fieldname attr = build_localized_fieldname(attr, settings.LANGUAGE_CODE) # Get self.title_xx where xx is the default language, if any. # Get self.title otherwise. return slugify(getattr(self, attr, None) or self.title)
def get_slug(self): """ Allows subclasses to implement their own slug creation logic. """ attr = "title" if settings.USE_MODELTRANSLATION: from modeltranslation.utils import build_localized_fieldname attr = build_localized_fieldname(attr, settings.LANGUAGE_CODE) # Get self.title_xx where xx is the default language, if any. # Get self.title otherwise. return slugify(getattr(self, attr, None) or self.title)
def save(self, commit=True): user = super(UserCreationForm, self).save(commit=False) user.set_password(str(uuid4())[:10]) user.email = self.cleaned_data['email'] #user.clean_email() user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] email_slug = self.cleaned_data["email"].split('@') username = slugify(email_slug[0] + '-' + email_slug[1])[:16] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) user.save() profile = get_profile_for_user(user) profile.membertype = self.cleaned_data['membertype'] profile.organisation = self.cleaned_data['organisation'] profile.save() # if commit: # user.save() return user
def handle(self, *args, **options): try: with open('students2.json') as data_file: data = json.load(data_file) member_group = EmailGroup.objects.get_or_create(title='Standard Members')[0] student_group = EmailGroup.objects.get_or_create(title='Students temporary')[0] marketing_group = EmailGroup.objects.get_or_create(title='Marketing')[0] for row in data: print(row['name']) user, created = User.objects.get_or_create(email=row['email']) if created: email_slug = row["email"].split('@') username = slugify(email_slug[0] + '-' + email_slug[1])[:16] qs = User.objects.exclude(id=user.id) user.username = unique_slug(qs, "username", slugify(username)) split_name = row['name'].split(' ',1) print(split_name) user.first_name = split_name[0] user.last_name = split_name[1] #profile = get_profile_for_user(user) user.profile.membertype = 'SM' user.profile.paid = True user.profile.paidfrom = timezone.now() user.profile.expires = timezone.now() + timezone.timedelta(days=366) user.profile.contacttype.add(member_group,student_group,marketing_group) user.profile.save() user.save() else: print('already in ', user) except Exception as e: print(e)
def save(self, *args, **kwargs): """ Create the new user. If no username is supplied (may be hidden via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so that if profile pages are enabled, we still have something to use as the profile's slug. """ kwargs["commit"] = False user = super(ProfileForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: try: username = ("%(first_name)s %(last_name)s" % self.cleaned_data).strip() except KeyError: username = "" if not username: username = self.cleaned_data["email"].split("@")[0] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) elif self._signup: user.set_unusable_password() user.save() # save profile profile, created = Profile.objects.get_or_create(user=user) profile_form = self.get_profile_fields_form() profile_form(self.data, self.files, instance=profile).save() # save lol account lol_account, created = LolAccount.objects.get_or_create(user=user) lolaccount_form = LolAccountFieldsForm lolaccount_form(self.data, self.files, instance=lol_account).save() if self._signup: if (settings.ACCOUNTS_VERIFICATION_REQUIRED or settings.ACCOUNTS_APPROVAL_REQUIRED): user.is_active = False user.save() else: token = default_token_generator.make_token(user) user = authenticate(uidb36=int_to_base36(user.id), token=token, is_active=True) return user
def clean_username(self): """ Ensure the username doesn't exist or contain invalid chars. We limit it to slugifiable chars since it's used as the slug for the user's profile view. """ username = self.cleaned_data.get("username") if username != slugify(username): raise forms.ValidationError(_("Username can only contain letters, " "numbers, dashes or underscores.")) try: User.objects.exclude(id=self.instance.id).get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(_("This username is already registered"))
def clean_username(self): """ Ensure the username doesn't exist or contain invalid chars. We limit it to slugifiable chars since it's used as the slug for the user's profile view. """ username = self.cleaned_data.get("username") if username.lower() != slugify(username).lower(): raise forms.ValidationError("Username can only contain letters, " "numbers, dashes or underscores.") lookup = {"username__iexact": username} try: User.objects.exclude(id=self.instance.id).get(**lookup) # @UndefinedVariable PyDev limitation ("exclude") except User.DoesNotExist: return username raise forms.ValidationError("This username is already registered")
def clean_username(self): """ Ensure the username doesn't exist or contain invalid chars. We limit it to slugifiable chars since it's used as the slug for the user's profile view. """ username = self.cleaned_data.get("username") if username.lower() != slugify(username).lower(): raise forms.ValidationError(_("Username can only contain letters, " "numbers, dashes or underscores.")) lookup = {"username__iexact": username} try: User.objects.exclude(id=self.instance.id).get(**lookup) except User.DoesNotExist: return username raise forms.ValidationError(_("This username is already registered"))
def save(self, *args, **kwargs): """ Create the new user. If no username is supplied (may be hidden via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so that if profile pages are enabled, we still have something to use as the profile's slug. """ kwargs["commit"] = False user = super(ProfileForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: username = "******" % self.cleaned_data if not username.strip(): username = self.cleaned_data["email"].split("@")[0] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) user.save() # Save profile model. if self._has_profile: try: profile = user.get_profile() except Profile.DoesNotExist: profile = Profile(user=user) profile_fields_form = self.get_profile_fields_form() profile_fields_form(self.data, self.files, instance=profile).save() if self._signup: settings.use_editable() if (settings.ACCOUNTS_VERIFICATION_REQUIRED or settings.ACCOUNTS_APPROVAL_REQUIRED): user.is_active = False user.save() else: user = authenticate(username=user.username, password=password, is_active=True) return user
def return_formentry(slug): """ take a form slug, possibly other filter conditions, return form entry results. """ # A 'form' is the description and fields for a form. print >>sys.stderr,"slug:%r" % slug single_form = Form.objects.get(slug=slug) # A 'form entry' represents an instance of filling out a form. # this formentry_list contains all of the filled out forms with the # passed in slug. formentry_list=FormEntry.objects.filter(form_id=single_form.id) label_list = [ ] field_list = Field.objects.filter(form_id=single_form.id) fieldsByID = {f.id:f for f in field_list} #for field in fieldsByID: # print "field_id: %r field: %r" % (field, fieldsByID[field] ) entrylist = [ ] entrydictlist = [ ] # I want a list of formentries? # now I build a list of data entries for formentry in formentry_list: entry={} entrydict={} fieldentry_list = FieldEntry.objects.filter(entry_id=formentry.id) for fieldentry in fieldentry_list: fieldid =fieldentry.field_id slug = slugify(fieldsByID[fieldentry.field_id]) value =fieldentry.value #print "try label:%s slug:%s value:%s " % (fieldsByID[fieldentry.field_id] , slug, fieldentry.value) entry[fieldsByID[fieldentry.field_id]] = fieldentry entrydict[slug] = value entrylist.append(entry) entrydictlist.append(entrydict) rv = {} rv['slug'] = slug rv['fieldsByID'] = fieldsByID rv['entrylist']=entrylist rv['entrydictlist']=entrydictlist return rv
def return_formentry(slug): """ take a form slug, possibly other filter conditions, return form entry results. """ # A 'form' is the description and fields for a form. print >> sys.stderr, "slug:%r" % slug single_form = Form.objects.get(slug=slug) # A 'form entry' represents an instance of filling out a form. # this formentry_list contains all of the filled out forms with the # passed in slug. formentry_list = FormEntry.objects.filter(form_id=single_form.id) label_list = [] field_list = Field.objects.filter(form_id=single_form.id) fieldsByID = {f.id: f for f in field_list} #for field in fieldsByID: # print "field_id: %r field: %r" % (field, fieldsByID[field] ) entrylist = [] entrydictlist = [] # I want a list of formentries? # now I build a list of data entries for formentry in formentry_list: entry = {} entrydict = {} fieldentry_list = FieldEntry.objects.filter(entry_id=formentry.id) for fieldentry in fieldentry_list: fieldid = fieldentry.field_id slug = slugify(fieldsByID[fieldentry.field_id]) value = fieldentry.value #print "try label:%s slug:%s value:%s " % (fieldsByID[fieldentry.field_id] , slug, fieldentry.value) entry[fieldsByID[fieldentry.field_id]] = fieldentry entrydict[slug] = value entrylist.append(entry) entrydictlist.append(entrydict) rv = {} rv['slug'] = slug rv['fieldsByID'] = fieldsByID rv['entrylist'] = entrylist rv['entrydictlist'] = entrydictlist return rv
def save(self, *args, **kwargs): """ Create the new user. If no username is supplied (may be hidden via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so that if profile pages are enabled, we still have something to use as the profile's slug. """ kwargs["commit"] = False user = super(ProfileForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: username = "******" % self.cleaned_data if not username.strip(): username = self.cleaned_data["email"].split("@")[0] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) user.save() # Save profile model. if self._has_profile: try: profile = user.get_profile() except Profile.DoesNotExist: profile = Profile(user=user) profile_fields_form = self.get_profile_fields_form() profile_fields_form(self.data, self.files, instance=profile).save() if self._signup: settings.use_editable() if (settings.ACCOUNTS_VERIFICATION_REQUIRED or settings.ACCOUNTS_APPROVAL_REQUIRED): user.is_active = False user.save() else: user = authenticate(username=user.username, password=password, is_active=True) return user
def get_slug(self): """ Allows subclasses to implement their own slug creation logic. """ return slugify(self.title)
def handle(self, *args, **options): all_agents = Agent.objects.filter(id__gte=186777) # all_agents = Agent.objects.all() agent_dict = { # 'License Type:':'', # 'Name:':'', # 'Mailing Address:':'', # 'License ID:':'', # 'Expiration Date:':'', # 'License Status':'', # 'Salesperson License Issued':'', # 'Corporation License Issued:':'', # 'Broker License Issued':'', # 'Former Name(s):':'', # 'Employing Broker:':'', # 'Main Office:':'', # 'DBA':'', # 'Branches:':'', # 'Affiliated Licensed Corporation(s):':'', # 'Comment':'', } errcount = 0 concat_value = [] for a in all_agents: time.sleep(2) # BROKER # agent_raw_data = requests.get('http://www2.dre.ca.gov/PublicASP/pplinfo.asp', # params={'License_id':'00811105',}, # headers=UA) ## SALESPERSON # agent_raw_data = requests.get('http://www2.dre.ca.gov/PublicASP/pplinfo.asp', # params={'License_id':'01027681',}, # headers=UA) # CORP # agent_raw_data = requests.get('http://www2.dre.ca.gov/PublicASP/pplinfo.asp', # params={'License_id':'01154618',}, # headers=UA) # ALL agent_raw_data = requests.get(DATA_LOCATION, params={ 'License_id': a.license_id, }, headers=UA) agentsoup = BeautifulSoup(agent_raw_data.text) # print agentsoup.prettify() try: table = agentsoup.find('table') rows = table.findChildren(['th', 'tr']) for rownum, row in enumerate(rows): cells = row.findChildren('td') agent_attribute = cells[0].findAll('font', text=True) agent_attribute_value = cells[1].findAll('font', text=True) try: concat_value = [] agent_dict[agent_attribute[0]] = agent_attribute_value errcount = 0 except IndexError: errcount += 1 cell_header = rows[rownum - errcount].findChildren('td') agent_attribute = cell_header[0].findAll('font', text=True) concat_value.append(agent_attribute_value) agent_dict[agent_attribute[0]] = [ cell_header[1].findAll('font', text=True) ] + concat_value if agent_dict['License Type:'][0] == "SALESPERSON": try: try: name_parts = agent_dict['Name:'][0].split(',') city_state_parts = agent_dict['Mailing Address:'][ 1].split(',') state_zip_parts = city_state_parts[1].split(' ') print "License Type", agent_dict['License Type:'][ 0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'][0].strip() # print "City", city_state_parts[0].strip() # print "State", state_zip_parts[1].strip() # print "zip", state_zip_parts[3].strip() # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Sales Lic Issued:", agent_dict['Salesperson License Issued'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] if len(agent_dict['Employing Broker:']) > 1: employing_broker = agent_dict[ 'Employing Broker:'][1].strip() else: employing_broker = 'No Employing Broker' # print "Employing Broker", employing_broker # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Agent {2} {3}'.format( name_parts[1].strip(), name_parts[0].strip(), city_state_parts[0].strip(), state_zip_parts[1].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'][ 0].strip() a.mailing_city = city_state_parts[0].strip() a.mailing_state = state_zip_parts[1].strip() a.slugged_city = city_state_parts[0].strip() a.slugged_state = state_zip_parts[1].strip() a.mailing_zip = state_zip_parts[3].strip() a.license_type = agent_dict['License Type:'][ 0].strip() a.salesperson_license_issue_date = agent_dict[ 'Salesperson License Issued'][0].strip() a.license_status = agent_dict['License Status'][ 0].strip() a.license_expiration_date = agent_dict[ 'Expiration Date:'][0].strip() a.employing_broker = employing_broker a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except IndexError, e: name_parts = agent_dict['Name:'][0].split(',') print "License Type", agent_dict['License Type:'][ 0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'] # print "City", "N/A" # print "State", "N/A" # print "zip", "N/A" # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Sales Lic Issued:", agent_dict['Salesperson License Issued'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] if len(agent_dict['Employing Broker:']) > 1: employing_broker = agent_dict[ 'Employing Broker:'][1].strip() else: employing_broker = 'No Employing Broker' # print "Employing Broker", employing_broker # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Agent'.format( name_parts[1].strip(), name_parts[0].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'] # a.mailing_city = "N/A" # a.mailing_state = "N/A" # a.mailing_zip = "N/A" a.license_type = agent_dict['License Type:'][ 0].strip() a.salesperson_license_issue_date = agent_dict[ 'Salesperson License Issued'][0].strip() a.license_status = agent_dict['License Status'][ 0].strip() a.license_expiration_date = agent_dict[ 'Expiration Date:'][0].strip() a.employing_broker = employing_broker a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except KeyError, e: print 'Keyerror for Salesperson' elif agent_dict['License Type:'][0] == "BROKER": try: name_parts = agent_dict['Name:'][0].split(',') city_state_parts = agent_dict['Mailing Address:'][ 1].split(',') state_zip_parts = city_state_parts[1].split(' ') print "License Type", agent_dict['License Type:'][ 0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'][0].strip() # print "City", city_state_parts[0].strip() # print "State", state_zip_parts[1].strip() # print "zip", state_zip_parts[3].strip() # print "Lic ID", agent_dict['License ID:'][0].strip() try: salespersonlic = agent_dict[ 'Salesperson License Issued'][0].strip() except KeyError: salespersonlic = "N/A" # print "Salesperson Lic Issued", salespersonlic # print "Broker Lic Issued", agent_dict['Broker License Issued'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] # print "Affiliated Licensed Corps", agent_dict['Affiliated Licensed Corporation(s):'] # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Broker {2} {3}'.format( name_parts[1].strip(), name_parts[0].strip(), city_state_parts[0].strip(), state_zip_parts[1].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'][ 0].strip() a.mailing_city = city_state_parts[0].strip() a.mailing_state = state_zip_parts[1].strip() a.slugged_city = city_state_parts[0].strip() a.slugged_state = state_zip_parts[1].strip() a.mailing_zip = state_zip_parts[3].strip() a.license_type = agent_dict['License Type:'][0].strip() a.salesperson_license_issue_date = salespersonlic a.broker_license_issue_date = agent_dict[ 'Broker License Issued'][0].strip() a.license_status = agent_dict['License Status'][ 0].strip() a.license_expiration_date = agent_dict[ 'Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except IndexError: name_parts = agent_dict['Name:'][0].split(',') print "License Type", agent_dict['License Type:'][ 0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'] # print "City", "N/A" # print "State", "N/A" # print "zip", "N/A" # print "Lic ID", agent_dict['License ID:'][0].strip() try: salespersonlic = agent_dict[ 'Salesperson License Issued'][0].strip() except KeyError: salespersonlic = "N/A" # print "Sales Lic Issued:", salespersonlic # print "Broker Lic Issued", agent_dict['Broker License Issued'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] # print "Affiliated Licensed Corps", agent_dict['Affiliated Licensed Corporation(s):'] # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Broker'.format( name_parts[1].strip(), name_parts[0].strip(), ) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'][ 0].strip() # a.mailing_city = "N/A" # a.mailing_state = "N/A" # a.mailing_zip = "N/A" a.license_type = agent_dict['License Type:'][0].strip() a.salesperson_license_issue_date = salespersonlic a.broker_license_issue_date = agent_dict[ 'Broker License Issued'][0].strip() a.license_status = agent_dict['License Status'][ 0].strip() a.license_expiration_date = agent_dict[ 'Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() elif agent_dict['License Type:'][0] == "CORPORATION": try: city_state_parts = agent_dict['Mailing Address:'][ 1].split(',') state_zip_parts = city_state_parts[1].split(' ') print "License Type", agent_dict['License Type:'][ 0].strip() print "Name: ", agent_dict['Name:'][0].strip() # print "Full name", agent_dict['Name:'][0].strip() # print "Mailing Addr", unicode(agent_dict['Mailing Address:'][0].strip().encode("utf-8")) # print "City", city_state_parts[0].strip() # print "State", state_zip_parts[1].strip() # print "zip", state_zip_parts[3].strip() # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Corp License Issued", agent_dict['Corporation License Issued:'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "Licensed Officer(s):", agent_dict['Licensed Officer(s):'] # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] try: employed_agents = str(agent_dict['Salespersons:']) except KeyError: employed_agents = "N/A" print "Employed Agents", employed_agents print "Comments", agent_dict['Comment'] a.title = '{0} Real Estate Corporation'.format( agent_dict['Name:'][0].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() # a.first_name = "N/A" # a.last_name = "N/A" a.mailing_address = agent_dict['Mailing Address:'][ 0].strip() a.mailing_city = city_state_parts[0].strip() a.mailing_state = state_zip_parts[1].strip() a.slugged_city = city_state_parts[0].strip() a.slugged_state = state_zip_parts[1].strip() a.mailing_zip = state_zip_parts[3].strip() a.license_type = agent_dict['License Type:'][0].strip() a.corp_license_issue_date = agent_dict[ 'Corporation License Issued:'][0].strip() a.license_status = agent_dict['License Status'][ 0].strip() a.license_expiration_date = agent_dict[ 'Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.licensed_officers = str( agent_dict['Licensed Officer(s):']) a.former_names = str(agent_dict['Former Name(s):']) a.salespersons_employed = employed_agents a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except IndexError: print "License Type", agent_dict['License Type:'][ 0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "Name: ", agent_dict['Name:'][0].strip() # print "Mailing Addr", unicode(agent_dict['Mailing Address:'][0].strip().encode("utf-8")) # print "City", "N/A" # print "State", "N/A" # print "zip", "N/A" # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Corp License Issued", agent_dict['Corporation License Issued:'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "Licensed Officer(s):", agent_dict['Licensed Officer(s):'] # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] try: employed_agents = str(agent_dict['Salespersons:']) except KeyError: employed_agents = "N/A" a.title = '{0} Real Estate Corporation'.format( agent_dict['Name:'][0].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() # a.first_name = "N/A" # a.last_name = "N/A" a.mailing_address = agent_dict['Mailing Address:'][ 0].strip() # a.mailing_city = "N/A" # a.mailing_state = "N/A" # a.mailing_zip = "N/A" a.license_type = agent_dict['License Type:'][0].strip() a.corp_license_issue_date = agent_dict[ 'Corporation License Issued:'][0].strip() a.license_status = agent_dict['License Status'][ 0].strip() a.license_expiration_date = agent_dict[ 'Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.licensed_officers = str( agent_dict['Licensed Officer(s):']) a.former_names = str(agent_dict['Former Name(s):']) a.salespersons_employed = employed_agents a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear()
def get_slug(self): """ Allows subclasses to implement their own slug creation logic. """ return slugify(self.title)
def create_user(data,membertype): #username = slugify(data['First name']) + '-' + slugify(data['Last name']) email_slug = data['e-Mail'].split('@') username = slugify(email_slug[0] + '-' + email_slug[1])[:16] #qs = User.objects.exclude(id=self.instance.id) #username = unique_slug(qs, "username", slugify(username)) # username = slugify(data['e-Mail']) + '-' + slugify(data['Last name']) user, created = User.objects.get_or_create(first_name=data['First name'], last_name=data['Last name'], email=data['e-Mail'], username=username, ) password = User.objects.make_random_password() user.set_password(password) profile = get_profile_for_user(user) if data['Organization'] != 0: profile.organisation = data['Organization'] if data['Personal Email'] != 0: profile.emailwork = data['Personal Email'] if data['Phone'] != 0: profile.phone = data['Phone'] if data['Website'] != 0: profile.website = data['Website'] if data['Short Bio'] != 0: profile.bio = data['Short Bio'] profile.membertype = membertype profile.paid = True try: date_object = datetime.strptime(data['Renewal due'], '%m/%d/%Y %H:%M:%S') date = date_object.replace(tzinfo=timezone('GMT')) profile.expires = date except Exception as e: pass try: date_object = datetime.strptime(data['Renewal date last changed'], '%m/%d/%Y %H:%M:%S') date = date_object.replace(tzinfo=timezone('GMT')) profile.paidfrom = date except Exception as e: self.stdout.write(e) pass try: date_object = datetime.strptime(data['Member since'], '%m/%d/%Y %H:%M:%S') date = date_object.replace(tzinfo=timezone('GMT')) # user.date_joined = date membersince = date except Exception as e: membersince = djangodatetime.now() self.stdout.write(e) pass user.save() profile.save() if membertype == 'PM': g = EmailGroup.objects.get_or_create(title='Members')[0] gp = EmailGroup.objects.get_or_create(title='Pro Members')[0] profile.contacttype.add(g,gp) if membertype == 'SM': g = EmailGroup.objects.get_or_create(title='Members')[0] gp = EmailGroup.objects.get_or_create(title='Standard Members')[0] profile.contacttype.add(g,gp)
def handle(self, *args, **options): reader = csv.reader(open('{0}/docs/agents/CurrList_Comma_Delimited6.txt'.format(PROJECT_ROOT), 'r'), delimiter=',') # reader = csv.reader(open('/home/mattym/webapps/vba/docs/CurrList_Comma_Delimited.txt', 'rU'), delimiter=',') # reader = csv.reader(open(TEMPLATE_DIRS[0] + '/agents.csv', 'rU'), delimiter=',') reader.next() # Skip first line # col[1] lastname_primary or corp name # col[2] firstname_secondary # col[3] name_suffix # col[4] lic_number # col[5] lic_type # col[6] lic_status # col[7] lic_effective_date # col[8] lic_expiration_date # col[9] rel_lic_number (employing broker of agent etc...) # col[10] empl_lastname_primary (agent's employer last name / Corp name) # col[11] empl_firstname_secondary (agent's employer first name / Corp name) # col[12] empl_name_suffix # col[13] officerlastname # col[14] officerfirstname # col[15] officersuffixname # col[16] rel_lic_type (employer type of license) # col[17] address_1 # col[18] address_2 # col[19] city # col[20] state # col[21] zip_code # col[22] foreign_nation # col[23] foreign_postal_info # col[24] county_code # col[25] county_name # col[26] restricted_flag # col[27] misc_indicator # col[28] ethics_and_agency_ind from dateutil import parser for col in reader: dt_lic_effective_date = parser.parse(col[7]) dt_lic_expiration_date = parser.parse(col[8]) if col[5] != 'Salesperson': clean_lic_type = col[5] else: clean_lic_type = "Agent" the_title = '{0} {1} Real Estate {2} {3} {4}'.format(col[2], col[1], clean_lic_type, col[19], col[20] ) full_name = "{0} {1} {2}".format(col[2], col[1], col[3]) full_name_officer = "{0} {1} {2}".format(col[14], col[13], col[15]) employer = "{0} {1}".format(col[10], col[11]) try: agent = Agent.objects.get(license_id=col[4]) agent.in_sitemap = False agent.licence_type = col[5] agent.license_issue_date = dt_lic_effective_date agent.license_exp_date = dt_lic_expiration_date agent.employing_broker = col[9] agent.licensed_officers = unicode(full_name_officer.strip()).encode("utf-8") agent.save() print 'Agent exists: {0}'.format(agent.title) except Agent.DoesNotExist: agent = Agent.objects.create( title=unicode(the_title).encode("utf-8"), slug=slugify(unicode(the_title).encode("utf-8")), status=2, full_name=unicode(full_name.strip()).encode("utf-8"), first_name=unicode(col[2].strip()).encode("utf-8"), last_name=unicode(col[1].strip()).encode("utf-8"), mailing_address=unicode(col[17]).encode("utf-8"), licensed_officers=unicode(full_name_officer.strip()).encode("utf-8"), mailing_city=col[19], mailing_state=col[20], mailing_zip=col[21], slugged_city=col[19], slugged_state=col[20], license_id=col[4], license_type=col[5], license_status=col[6], license_issue_date=dt_lic_effective_date, license_exp_date=dt_lic_expiration_date, employing_broker=unicode(employer).encode("utf-8"), in_sitemap=False, # # affiliated_corporations= col[34], # # licensed_officers = col[35] # # main_office_address= col[30], # # branches = col[32], # # dba = col[33], # # former_names = col[36], # # salespersons_employed= col[37], # # public_comment = col[38], ) print "Created Agent: {0}".format(unicode(agent).encode("utf-8")) print 'Finished importing agents'
def compare(request): form = DifferenceForm(auto_id=True) if request.method == "POST": form = DifferenceForm(request.POST, auto_id=True) if form.is_valid(): sub_one_word = form.cleaned_data['subject_one'].lower() sub_two_word = form.cleaned_data['subject_two'].lower() sub_one_word_slugged = slugify(sub_one_word) sub_two_word_slugged = slugify(sub_two_word) # FORCE WORDS TO BE IN CERTAIN ORDER TO AVOID DUPE CONTENT word_list = [sub_one_word_slugged, sub_two_word_slugged] word_list_sorted = sorted(word_list) page_slug = "{0}-and-{1}".format(word_list_sorted[0],word_list_sorted[1]) page_title = "{0} and {1}".format(word_list_sorted[0], word_list_sorted[1]) try: obj = DifferncePage.objects.get(subject_one=word_list_sorted[0],subject_two=word_list_sorted[1]) redirect = "{0}{1}/".format(request.path, obj.slug) return HttpResponseRedirect(redirect) except DifferncePage.DoesNotExist: # print 'This Comparison Does Not Exist. Create It' try: subject_one_subject=get_subject_one_data(word_list_sorted[0])['query'] subject_one_description = get_subject_one_data(word_list_sorted[0])['description'] subject_one_description_dictservice = ''.join(get_subject_one_data_dictservice(word_list_sorted[0])['data']) except Exception: errormsg1 = "<strong>Error occured (Subject One)</strong>: <ul><li>Tech glitch!</li></ul>" # return{'form':form,'errormsg1':errormsg1} return render_to_response('pages/compare.html', {'form': form, 'errormsg1': errormsg1}, context_instance=RequestContext(request)) try: subject_two_subject=get_subject_two_data(word_list_sorted[1])['query'] subject_two_description = get_subject_two_data(word_list_sorted[1])['description'] subject_two_description_dictservice =''.join(get_subject_two_data_dictservice(word_list_sorted[1])['data']) except Exception: errormsg2 = "<strong>Error occured (Subject Two)</strong>: <ul><li>Tech glitch!</li></ul>" # return{'form':form,'errormsg2':errormsg2} return render_to_response('pages/compare.html', {'form': form, 'errormsg2':errormsg2}, context_instance=RequestContext(request)) subject_data_sources_api ='{0} {1}'.format( ''.join(get_subject_one_data_dictservice(word_list_sorted[0])['sources']), ''.join(get_subject_two_data_dictservice(word_list_sorted[1])['sources']), ) obj = DifferncePage(title=page_title, subject_one=subject_one_subject, subject_two=subject_two_subject, subject_one_data=subject_one_description, subject_two_data=subject_two_description, subject_one_data_dictservice=subject_one_description_dictservice, subject_two_data_dictservice=subject_two_description_dictservice, subject_data_sources=subject_data_sources_api) obj.save() redirect = "{0}{1}/".format(request.path, page_slug) return HttpResponseRedirect(redirect) return render_to_response('pages/compare.html', {'form': form}, context_instance=RequestContext(request))
def get_slug(self): slug = slugify(self.title) if not self.slug: slug = 'bccf/%s' % slug return slug
def slug(self): return slugify(self.__str__())
def valid_tag(tag): def valid_char(char): return not (char in punctuation or char.isdigit()) return filter(valid_char, slugify(tag[0]))
def save(self, *args, **kwargs): """ Create the new user. If no username is supplied (may be hidden via ``ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS`` or ``ACCOUNTS_NO_USERNAME``), we generate a unique username, so that if profile pages are enabled, we still have something to use as the profile's slug. """ kwargs["commit"] = False user = super(ModProfileForm, self).save(*args, **kwargs) try: self.cleaned_data["username"] except KeyError: if not self.instance.username: try: email_slug = self.cleaned_data["email"].split('@') username = slugify(email_slug[0] + '-' + email_slug[1])[:16] #username = self.cleaned_data["email"].split("@")[0] except KeyError: username = "" if not username: #username = self.cleaned_data["email"].split("@")[0] email_slug = self.cleaned_data["email"].split('@') username = slugify(email_slug[0] + '-' + email_slug[1])[:16] qs = User.objects.exclude(id=self.instance.id) user.username = unique_slug(qs, "username", slugify(username)) password = self.cleaned_data.get("password1") if password: user.set_password(password) elif self._signup: try: user.set_unusable_password() except AttributeError: # This could happen if using a custom user model that # doesn't inherit from Django's AbstractBaseUser. pass user.save() try: profile = get_profile_for_user(user) profile_form = self.get_profile_fields_form() profile_form(self.data, self.files, instance=profile).save() except ProfileNotConfigured: pass if self._signup: ## create SubscriberEmailAddress object for the group "members" ## so we can keep email address on record in future # g = EmailGroup.objects.get_or_create(title="Sign Ups")[0] # sub = SubscriberEmailAddress(sub_email=g, # email=self.cleaned_data["email"], # firstname=self.cleaned_data["first_name"], # lastname=self.cleaned_data["last_name"], # source='Website' # ) # sub.save() profile.membertype = 'SU' profile.save() g = EmailGroup.objects.get_or_create(title='Sign Ups')[0] profile.contacttype.add(g) ############################################### if (settings.ACCOUNTS_VERIFICATION_REQUIRED or settings.ACCOUNTS_APPROVAL_REQUIRED): user.is_active = False user.save() else: token = default_token_generator.make_token(user) user = authenticate(uidb36=int_to_base36(user.id), token=token, is_active=True) # else: # if not user.is_staff: # sub = SubscriberEmailAddress.objects.get(email=user.email) # if self.cleaned_data["organisation"]: # sub.organisation = self.cleaned_data["organisation"] # if self.cleaned_data["nationality"]: # sub.country = self.cleaned_data["nationality"] # sub.save() return user
def save(self, *args, **kwargs): # pylint: disable=arguments-differ if not self.slug: self.slug = slugify(self.title) super(NewsPost, self).save(*args, **kwargs)
def get_html_id(page): return slugify(page.title)
def valid_tag(tag): def valid_char(char): return not (char in punctuation or char.isdigit()) return filter(valid_char, slugify(tag[0]))
def save(self, *args, **kwargs): # pylint: disable=signature-differs if not self.slug: self.slug = slugify(self.title) super().save(*args, **kwargs)
def handle(self, *args, **options): reader = csv.reader(open( '{0}/docs/agents/CurrList_Comma_Delimited6.txt'.format( PROJECT_ROOT), 'r'), delimiter=',') # reader = csv.reader(open('/home/mattym/webapps/vba/docs/CurrList_Comma_Delimited.txt', 'rU'), delimiter=',') # reader = csv.reader(open(TEMPLATE_DIRS[0] + '/agents.csv', 'rU'), delimiter=',') reader.next() # Skip first line # col[1] lastname_primary or corp name # col[2] firstname_secondary # col[3] name_suffix # col[4] lic_number # col[5] lic_type # col[6] lic_status # col[7] lic_effective_date # col[8] lic_expiration_date # col[9] rel_lic_number (employing broker of agent etc...) # col[10] empl_lastname_primary (agent's employer last name / Corp name) # col[11] empl_firstname_secondary (agent's employer first name / Corp name) # col[12] empl_name_suffix # col[13] officerlastname # col[14] officerfirstname # col[15] officersuffixname # col[16] rel_lic_type (employer type of license) # col[17] address_1 # col[18] address_2 # col[19] city # col[20] state # col[21] zip_code # col[22] foreign_nation # col[23] foreign_postal_info # col[24] county_code # col[25] county_name # col[26] restricted_flag # col[27] misc_indicator # col[28] ethics_and_agency_ind from dateutil import parser for col in reader: dt_lic_effective_date = parser.parse(col[7]) dt_lic_expiration_date = parser.parse(col[8]) if col[5] != 'Salesperson': clean_lic_type = col[5] else: clean_lic_type = "Agent" the_title = '{0} {1} Real Estate {2} {3} {4}'.format( col[2], col[1], clean_lic_type, col[19], col[20]) full_name = "{0} {1} {2}".format(col[2], col[1], col[3]) full_name_officer = "{0} {1} {2}".format(col[14], col[13], col[15]) employer = "{0} {1}".format(col[10], col[11]) try: agent = Agent.objects.get(license_id=col[4]) agent.in_sitemap = False agent.licence_type = col[5] agent.license_issue_date = dt_lic_effective_date agent.license_exp_date = dt_lic_expiration_date agent.employing_broker = col[9] agent.licensed_officers = unicode( full_name_officer.strip()).encode("utf-8") agent.save() print 'Agent exists: {0}'.format(agent.title) except Agent.DoesNotExist: agent = Agent.objects.create( title=unicode(the_title).encode("utf-8"), slug=slugify(unicode(the_title).encode("utf-8")), status=2, full_name=unicode(full_name.strip()).encode("utf-8"), first_name=unicode(col[2].strip()).encode("utf-8"), last_name=unicode(col[1].strip()).encode("utf-8"), mailing_address=unicode(col[17]).encode("utf-8"), licensed_officers=unicode( full_name_officer.strip()).encode("utf-8"), mailing_city=col[19], mailing_state=col[20], mailing_zip=col[21], slugged_city=col[19], slugged_state=col[20], license_id=col[4], license_type=col[5], license_status=col[6], license_issue_date=dt_lic_effective_date, license_exp_date=dt_lic_expiration_date, employing_broker=unicode(employer).encode("utf-8"), in_sitemap=False, # # affiliated_corporations= col[34], # # licensed_officers = col[35] # # main_office_address= col[30], # # branches = col[32], # # dba = col[33], # # former_names = col[36], # # salespersons_employed= col[37], # # public_comment = col[38], ) print "Created Agent: {0}".format( unicode(agent).encode("utf-8")) print 'Finished importing agents'
def handle(self, *args, **options): all_agents = Agent.objects.filter(id__gte=186777) # all_agents = Agent.objects.all() agent_dict = { # 'License Type:':'', # 'Name:':'', # 'Mailing Address:':'', # 'License ID:':'', # 'Expiration Date:':'', # 'License Status':'', # 'Salesperson License Issued':'', # 'Corporation License Issued:':'', # 'Broker License Issued':'', # 'Former Name(s):':'', # 'Employing Broker:':'', # 'Main Office:':'', # 'DBA':'', # 'Branches:':'', # 'Affiliated Licensed Corporation(s):':'', # 'Comment':'', } errcount = 0 concat_value = [] for a in all_agents: time.sleep(2) # BROKER # agent_raw_data = requests.get('http://www2.dre.ca.gov/PublicASP/pplinfo.asp', # params={'License_id':'00811105',}, # headers=UA) ## SALESPERSON # agent_raw_data = requests.get('http://www2.dre.ca.gov/PublicASP/pplinfo.asp', # params={'License_id':'01027681',}, # headers=UA) # CORP # agent_raw_data = requests.get('http://www2.dre.ca.gov/PublicASP/pplinfo.asp', # params={'License_id':'01154618',}, # headers=UA) # ALL agent_raw_data = requests.get(DATA_LOCATION, params={'License_id':a.license_id,}, headers=UA) agentsoup=BeautifulSoup(agent_raw_data.text) # print agentsoup.prettify() try: table = agentsoup.find('table') rows = table.findChildren(['th', 'tr']) for rownum, row in enumerate(rows): cells = row.findChildren('td') agent_attribute = cells[0].findAll('font', text=True) agent_attribute_value = cells[1].findAll('font', text=True) try: concat_value = [] agent_dict[agent_attribute[0]] = agent_attribute_value errcount = 0 except IndexError: errcount += 1 cell_header = rows[rownum - errcount].findChildren('td') agent_attribute = cell_header[0].findAll('font', text=True) concat_value.append(agent_attribute_value) agent_dict[agent_attribute[0]] = [cell_header[1].findAll('font', text=True)] + concat_value if agent_dict['License Type:'][0] == "SALESPERSON": try: try: name_parts = agent_dict['Name:'][0].split(',') city_state_parts = agent_dict['Mailing Address:'][1].split(',') state_zip_parts = city_state_parts[1].split(' ') print "License Type", agent_dict['License Type:'][0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'][0].strip() # print "City", city_state_parts[0].strip() # print "State", state_zip_parts[1].strip() # print "zip", state_zip_parts[3].strip() # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Sales Lic Issued:", agent_dict['Salesperson License Issued'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] if len(agent_dict['Employing Broker:']) > 1: employing_broker = agent_dict['Employing Broker:'][1].strip() else: employing_broker = 'No Employing Broker' # print "Employing Broker", employing_broker # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Agent {2} {3}'.format(name_parts[1].strip(), name_parts[0].strip(), city_state_parts[0].strip(), state_zip_parts[1].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'][0].strip() a.mailing_city = city_state_parts[0].strip() a.mailing_state = state_zip_parts[1].strip() a.slugged_city = city_state_parts[0].strip() a.slugged_state = state_zip_parts[1].strip() a.mailing_zip = state_zip_parts[3].strip() a.license_type = agent_dict['License Type:'][0].strip() a.salesperson_license_issue_date = agent_dict['Salesperson License Issued'][0].strip() a.license_status = agent_dict['License Status'][0].strip() a.license_expiration_date = agent_dict['Expiration Date:'][0].strip() a.employing_broker = employing_broker a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except IndexError, e: name_parts = agent_dict['Name:'][0].split(',') print "License Type", agent_dict['License Type:'][0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'] # print "City", "N/A" # print "State", "N/A" # print "zip", "N/A" # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Sales Lic Issued:", agent_dict['Salesperson License Issued'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] if len(agent_dict['Employing Broker:']) > 1: employing_broker = agent_dict['Employing Broker:'][1].strip() else: employing_broker = 'No Employing Broker' # print "Employing Broker", employing_broker # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Agent'.format(name_parts[1].strip(), name_parts[0].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'] # a.mailing_city = "N/A" # a.mailing_state = "N/A" # a.mailing_zip = "N/A" a.license_type = agent_dict['License Type:'][0].strip() a.salesperson_license_issue_date = agent_dict['Salesperson License Issued'][0].strip() a.license_status = agent_dict['License Status'][0].strip() a.license_expiration_date = agent_dict['Expiration Date:'][0].strip() a.employing_broker = employing_broker a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except KeyError, e: print 'Keyerror for Salesperson' elif agent_dict['License Type:'][0] == "BROKER": try: name_parts = agent_dict['Name:'][0].split(',') city_state_parts = agent_dict['Mailing Address:'][1].split(',') state_zip_parts = city_state_parts[1].split(' ') print "License Type", agent_dict['License Type:'][0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'][0].strip() # print "City", city_state_parts[0].strip() # print "State", state_zip_parts[1].strip() # print "zip", state_zip_parts[3].strip() # print "Lic ID", agent_dict['License ID:'][0].strip() try: salespersonlic = agent_dict['Salesperson License Issued'][0].strip() except KeyError: salespersonlic = "N/A" # print "Salesperson Lic Issued", salespersonlic # print "Broker Lic Issued", agent_dict['Broker License Issued'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] # print "Affiliated Licensed Corps", agent_dict['Affiliated Licensed Corporation(s):'] # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Broker {2} {3}'.format(name_parts[1].strip(), name_parts[0].strip(), city_state_parts[0].strip(), state_zip_parts[1].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'][0].strip() a.mailing_city = city_state_parts[0].strip() a.mailing_state = state_zip_parts[1].strip() a.slugged_city = city_state_parts[0].strip() a.slugged_state = state_zip_parts[1].strip() a.mailing_zip = state_zip_parts[3].strip() a.license_type = agent_dict['License Type:'][0].strip() a.salesperson_license_issue_date = salespersonlic a.broker_license_issue_date = agent_dict['Broker License Issued'][0].strip() a.license_status = agent_dict['License Status'][0].strip() a.license_expiration_date = agent_dict['Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except IndexError: name_parts = agent_dict['Name:'][0].split(',') print "License Type", agent_dict['License Type:'][0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "First Name", name_parts[1].strip() # print "Last Name", name_parts[0].strip() # print "Mailing Addr", agent_dict['Mailing Address:'] # print "City", "N/A" # print "State", "N/A" # print "zip", "N/A" # print "Lic ID", agent_dict['License ID:'][0].strip() try: salespersonlic = agent_dict['Salesperson License Issued'][0].strip() except KeyError: salespersonlic = "N/A" # print "Sales Lic Issued:", salespersonlic # print "Broker Lic Issued", agent_dict['Broker License Issued'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] # print "Affiliated Licensed Corps", agent_dict['Affiliated Licensed Corporation(s):'] # print "Comments", agent_dict['Comment'] a.title = '{0} {1} Real Estate Broker'.format(name_parts[1].strip(), name_parts[0].strip(), ) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() a.first_name = name_parts[1].strip() a.last_name = name_parts[0].strip() a.mailing_address = agent_dict['Mailing Address:'][0].strip() # a.mailing_city = "N/A" # a.mailing_state = "N/A" # a.mailing_zip = "N/A" a.license_type = agent_dict['License Type:'][0].strip() a.salesperson_license_issue_date = salespersonlic a.broker_license_issue_date = agent_dict['Broker License Issued'][0].strip() a.license_status = agent_dict['License Status'][0].strip() a.license_expiration_date = agent_dict['Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.former_names = str(agent_dict['Former Name(s):']) a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() elif agent_dict['License Type:'][0] == "CORPORATION": try: city_state_parts = agent_dict['Mailing Address:'][1].split(',') state_zip_parts = city_state_parts[1].split(' ') print "License Type", agent_dict['License Type:'][0].strip() print "Name: ", agent_dict['Name:'][0].strip() # print "Full name", agent_dict['Name:'][0].strip() # print "Mailing Addr", unicode(agent_dict['Mailing Address:'][0].strip().encode("utf-8")) # print "City", city_state_parts[0].strip() # print "State", state_zip_parts[1].strip() # print "zip", state_zip_parts[3].strip() # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Corp License Issued", agent_dict['Corporation License Issued:'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "Licensed Officer(s):", agent_dict['Licensed Officer(s):'] # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] try: employed_agents = str(agent_dict['Salespersons:']) except KeyError: employed_agents = "N/A" print "Employed Agents", employed_agents print "Comments", agent_dict['Comment'] a.title = '{0} Real Estate Corporation'.format(agent_dict['Name:'][0].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() # a.first_name = "N/A" # a.last_name = "N/A" a.mailing_address = agent_dict['Mailing Address:'][0].strip() a.mailing_city = city_state_parts[0].strip() a.mailing_state = state_zip_parts[1].strip() a.slugged_city = city_state_parts[0].strip() a.slugged_state = state_zip_parts[1].strip() a.mailing_zip = state_zip_parts[3].strip() a.license_type = agent_dict['License Type:'][0].strip() a.corp_license_issue_date = agent_dict['Corporation License Issued:'][0].strip() a.license_status = agent_dict['License Status'][0].strip() a.license_expiration_date = agent_dict['Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.licensed_officers = str(agent_dict['Licensed Officer(s):']) a.former_names = str(agent_dict['Former Name(s):']) a.salespersons_employed = employed_agents a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear() except IndexError: print "License Type", agent_dict['License Type:'][0].strip() print "Full name", agent_dict['Name:'][0].strip() # print "Name: ", agent_dict['Name:'][0].strip() # print "Mailing Addr", unicode(agent_dict['Mailing Address:'][0].strip().encode("utf-8")) # print "City", "N/A" # print "State", "N/A" # print "zip", "N/A" # print "Lic ID", agent_dict['License ID:'][0].strip() # print "Corp License Issued", agent_dict['Corporation License Issued:'][0].strip() # print "Lic Status", agent_dict['License Status'][0].strip() # print "Lic Expiration:", agent_dict['Expiration Date:'][0].strip() # print "Former Name", agent_dict['Former Name(s):'] # print "Main Office", ' '.join(agent_dict['Main Office:']) # print "Licensed Officer(s):", agent_dict['Licensed Officer(s):'] # print "DBA", agent_dict['DBA'] # print "Branches", agent_dict['Branches:'] try: employed_agents = str(agent_dict['Salespersons:']) except KeyError: employed_agents = "N/A" a.title = '{0} Real Estate Corporation'.format(agent_dict['Name:'][0].strip()) a.slug = slugify(a.title) a.full_name = agent_dict['Name:'][0].strip() # a.first_name = "N/A" # a.last_name = "N/A" a.mailing_address = agent_dict['Mailing Address:'][0].strip() # a.mailing_city = "N/A" # a.mailing_state = "N/A" # a.mailing_zip = "N/A" a.license_type = agent_dict['License Type:'][0].strip() a.corp_license_issue_date = agent_dict['Corporation License Issued:'][0].strip() a.license_status = agent_dict['License Status'][0].strip() a.license_expiration_date = agent_dict['Expiration Date:'][0].strip() a.main_office_address = agent_dict['Main Office:'] a.dba = str(agent_dict['DBA']) a.branches = str(agent_dict['Branches:']) a.licensed_officers = str(agent_dict['Licensed Officer(s):']) a.former_names = str(agent_dict['Former Name(s):']) a.salespersons_employed = employed_agents a.public_comment = str(agent_dict['Comment']) a.save() agent_dict.clear()
def save(self): slug = "seminar/{}".format(slugify(self.last_name())) slug_qs = Seminar.objects.exclude(id=self.id) self.slug = unique_slug(slug_qs, "slug", slug) super(Seminar, self).save()
def get_slug(self): return slugify(unidecode(self.title))