def save(request, site): if request.method != 'POST': return redirect('admin.sites.settings.views.index', site) active_site = Site.objects.get(id=site) form = SiteForm(request.user, request.POST, auto_id='%s') if not form.is_valid(): request.session['form_post_data'] = request.POST return redirect('admin.sites.settings.views.index', site) site_forening = form.cleaned_data['forening'] type = form.cleaned_data['type'] title = form.cleaned_data['title'] template_main = form.cleaned_data['template_main'] template_type = form.cleaned_data['template_type'] template_description = form.cleaned_data['template_description'] domain = request.POST['domain'].strip().lower().replace('http://', '').rstrip('/') errors = False active_site.forening = site_forening active_site.type = type active_site.title = title active_site.template_main = template_main active_site.template_type = template_type active_site.template_description = template_description if domain == active_site.domain: # Special case; the domain wasn't changed - so just pretend that it's updated pass else: result = Site.verify_domain(domain) if not result['valid']: messages.error(request, result['error']) errors = True if result['error'] == 'site_exists': request.session['message_context'] = {'existing_forening': result['existing_forening']} else: active_site.domain = result['domain'] active_site.prefix = result['prefix'] active_site.is_published = 'published' in request.POST active_site.save() # If this is a main template, clear other templates of this type in case any of them were previous main if active_site.type == 'mal' and active_site.template_main: Site.objects.filter( type=active_site.type, template_type=active_site.template_type, ).exclude( id=active_site.id, ).update( template_main=False ) request.session.modified = True if not errors: messages.info(request, 'settings_saved') return redirect('admin.sites.settings.views.index', active_site.id)
def clean_domain(self): domain = self.cleaned_data['domain'].strip().lower() domain = re.sub('https?://', '', domain) domain = domain.strip('/').strip('.') # Requiring test-domain at the moment domain = '%s-test.dnt.no' % domain result = Site.verify_domain(domain) if not result['valid']: if result['error'] == 'malformed': raise forms.ValidationError( "Du må fylle inn et gyldig domenenavn. Hør med DNT sentralt dersom du er usikker på hva dette bør være.", code=result['error'], ) elif result['error'] == 'site_exists': raise forms.ValidationError( "<strong>%s</strong> har allerede denne adressen. Hør med DNT sentralt dersom du er usikker på hva domenenavnet deres bør være." % result['existing_forening'].name, code=result['error'], ) elif result['error'] == 'prefix_not_supported': raise forms.ValidationError( "Du kan ikke ha noe mappenavn bak domenenavnet.", code=result['error'], ) elif result['error'] == 'test_period_requires_test_domain': # Shouldn't happen since we're explicitly appending the test-domain, but handle it nevertheless raise forms.ValidationError( "I test-perioden <strong>må</strong> dere ha \"<strong>-test.dnt.no</strong>\" bak domenet. For eksempel: <strong>http://stf-test.dnt.no</strong>.", code=result['error'], ) else: # Unknown verify_domain error code :( logger.error("Ukjent domenevaliderings-feilkode '%s'" % result['error']) raise forms.ValidationError( "Domeneadressen er ikke gyldig, dobbeltsjekk at du har skrevet riktig", code=result['error'], ) else: return result['domain']