def update_admin(request): """ View: Updates administration settings for an authenticated administrator or staff member. User is redirected back to administration panel once complete. """ if not request.user.is_authenticated( ) or not request.user.is_staff or request.method != 'POST': return redirect('index') exclusions = request.POST['exclusions'] preserved = request.POST['preserved'] # Clear old exclusion/preserved/extension domains (will be parsed from scratch) ExcludedDomain.objects.all().delete() PreservedDomain.objects.all().delete() # Parse text area of exclusion domains (one on each line) for e in exclusions.split('\n'): e = e.strip() if len(e) == 0: continue ed = ExcludedDomain(domain=e) ed.save() # Parse text area of preservation domains (one on each line) for p in preserved.split('\n'): p = p.strip() if len(p) == 0: continue pd = PreservedDomain(domain=p) pd.save() # Only update central administration settings if the user is recorded as an administrator (not just staff) if request.user.is_superuser: # Clear old extension prefixes extensions = request.POST['extensions'] ExtensionPrefix.objects.all().delete() # Parse text area of extension prefixes (one on each line) for x in extensions.split('\n'): x = x.strip() if len(x) == 0: continue ep = ExtensionPrefix(prefix=x) ep.save() # Note that the field names are used to determine the AdminSetting key ads = AdminSetting.objects.all() for ad in ads: if ad.type == 'boolean': ad.value = 'true' if request.POST.has_key(ad.key) else 'false' ad.save() elif request.POST.has_key(ad.key): if len(request.POST[ad.key]) == 0: request.session[ 'profile_message'] = '<b>Error updating settings:</b> Field "%s" cannot be blank' % ad.key request.session['profile_messagetype'] = 'error' return redirect('admin_settings') else: ad.value = request.POST[ad.key] ad.save() staff = request.POST['staff'] staff_list = [] for s in staff.split('\n'): s = s.strip() if len(s) == 0: continue staff_list.append(s) for u in User.objects.all(): u.is_staff = u.username in staff_list u.save() request.session[ 'profile_message'] = 'Administration settings successfully updated' request.session['profile_messagetype'] = 'success' return redirect('admin_settings')
def update_admin(request): """ View: Updates administration settings for an authenticated administrator or staff member. User is redirected back to administration panel once complete. """ if not request.user.is_authenticated() or not request.user.is_staff or request.method != 'POST': return redirect('index') exclusions = request.POST['exclusions'] preserved = request.POST['preserved'] # Clear old exclusion/preserved/extension domains (will be parsed from scratch) ExcludedDomain.objects.all().delete() PreservedDomain.objects.all().delete() # Parse text area of exclusion domains (one on each line) for e in exclusions.split('\n'): e = e.strip() if len(e) == 0: continue ed = ExcludedDomain(domain=e) ed.save() # Parse text area of preservation domains (one on each line) for p in preserved.split('\n'): p = p.strip() if len(p) == 0: continue pd = PreservedDomain(domain=p) pd.save() # Only update central administration settings if the user is recorded as an administrator (not just staff) if request.user.is_superuser: # Clear old extension prefixes extensions = request.POST['extensions'] ExtensionPrefix.objects.all().delete() # Parse text area of extension prefixes (one on each line) for x in extensions.split('\n'): x = x.strip() if len(x) == 0: continue ep = ExtensionPrefix(prefix=x) ep.save() # Note that the field names are used to determine the AdminSetting key ads = AdminSetting.objects.all() for ad in ads: if ad.type == 'boolean': ad.value = 'true' if request.POST.has_key(ad.key) else 'false' ad.save() elif request.POST.has_key(ad.key): if len(request.POST[ad.key]) == 0: request.session['profile_message'] = '<b>Error updating settings:</b> Field "%s" cannot be blank' % ad.key request.session['profile_messagetype'] = 'error' return redirect('admin_settings') else: ad.value = request.POST[ad.key] ad.save() staff = request.POST['staff'] staff_list = [] for s in staff.split('\n'): s = s.strip() if len(s) == 0: continue staff_list.append(s) for u in User.objects.all(): u.is_staff = u.username in staff_list u.save() request.session['profile_message'] = 'Administration settings successfully updated' request.session['profile_messagetype'] = 'success' return redirect('admin_settings')
def handle(self, *args, **options): tld_filename = 'tld_list.txt' exclusion_filename = 'exclusion_domains.txt' settings_filename = 'clean_admin.txt' tldf = open(tld_filename) tlds = [line.strip() for line in tldf if line[0] not in '/\n'] tldf.close() exf = open(exclusion_filename) exl = [line.strip() for line in exf] exf.close() sf = open(settings_filename) ss = [line.strip() for line in sf] sf.close() tic = 0 for tld in tlds: try: t = TLD.objects.get(domain=tld) except TLD.DoesNotExist: t = TLD() t.domain = tld t.is_recognized = False t.is_api_registerable = False t.description = None t.type = '' t.save() tic += 1 self.stdout.write('TLDs: Inserted %d row(s) (out of %d TLDs)' % (tic, len(tlds))) eic = 0 for exd in exl: try: ed = ExcludedDomain.objects.get(domain=exd) except ExcludedDomain.DoesNotExist: ed = ExcludedDomain() ed.domain = exd ed.save() eic += 1 self.stdout.write( 'Excluded domains: Inserted %d row(s) (out of %d listed domains)' % (eic, len(exl))) sic = 0 for s in ss: if len(s) == 0: continue vals = s.split('\t') key = vals[0] value = vals[1] valtype = vals[2] choices = None if len(vals) > 3: choices = vals[4] try: aso = AdminSetting.objects.get(key=key) except AdminSetting.DoesNotExist: aso = AdminSetting() aso.key = key aso.value = value aso.type = valtype aso.choices = choices aso.save() sic += 1 self.stdout.write( 'Admin settings: Inserted %d row(s) (out of %d listed settings)' % (sic, len(ss)))
def handle(self, *args, **options): tld_filename = 'tld_list.txt' exclusion_filename = 'exclusion_domains.txt' settings_filename = 'clean_admin.txt' tldf = open(tld_filename) tlds = [line.strip() for line in tldf if line[0] not in '/\n'] tldf.close() exf = open(exclusion_filename) exl = [line.strip() for line in exf] exf.close() sf = open(settings_filename) ss = [line.strip() for line in sf] sf.close() tic = 0 for tld in tlds: try: t = TLD.objects.get(domain=tld) except TLD.DoesNotExist: t = TLD() t.domain = tld t.is_recognized = False t.is_api_registerable = False t.description = None t.type = '' t.save() tic += 1 self.stdout.write('TLDs: Inserted %d row(s) (out of %d TLDs)' % (tic, len(tlds))) eic = 0 for exd in exl: try: ed = ExcludedDomain.objects.get(domain=exd) except ExcludedDomain.DoesNotExist: ed = ExcludedDomain() ed.domain = exd ed.save() eic += 1 self.stdout.write('Excluded domains: Inserted %d row(s) (out of %d listed domains)' % (eic, len(exl))) sic = 0 for s in ss: if len(s) == 0: continue vals = s.split('\t') key = vals[0] value = vals[1] valtype = vals[2] choices = None if len(vals) > 3: choices = vals[4] try: aso = AdminSetting.objects.get(key=key) except AdminSetting.DoesNotExist: aso = AdminSetting() aso.key = key aso.value = value aso.type = valtype aso.choices = choices aso.save() sic += 1 self.stdout.write('Admin settings: Inserted %d row(s) (out of %d listed settings)' % (sic, len(ss)))