def site_edit(request,pk): title=_('Edit Site') subtitle=_('update details of a site') breadcrumb=_('Edit Site') site=get_object_or_404(Sites, pk=pk) if request.method=='POST': form=SiteEditForm(request.POST or None, request.FILES or None) if form.is_valid(): site.name=form.cleaned_data['name'] site.url=form.cleaned_data['url'] if form.cleaned_data['client_cert']: site.client_cert.delete() site.client_cert=form.cleaned_data['client_cert'] if form.cleaned_data['client_key']: site.client_key.delete() site.client_key=form.cleaned_data['client_key'] if form.cleaned_data['ca_cert']: site.ca_cert.delete() site.ca_cert=form.cleaned_data['ca_cert'] site.ssl_verify=form.cleaned_data['ssl_verify'] site.save() messages.success(request, _('Site updated!')) return redirect("sites-list") else: print(form.errors) else: form=SiteEditForm() return render(request, "sites/site_edit.html", { "site": site, "title": title, "breadcrumb": breadcrumb, "subtitle": subtitle, "form": form, })
class ProjectForm(forms.Form): project = forms.CharField( label=_('Project Name'), required=True, error_messages={'required': _('Please enter your project name!')}) tos = forms.BooleanField( required=True, error_messages={'required': _('You must accept TOS.')})
def site_create(request): title = _('New Site') subtitle = _('create new site') if request.method=='POST': form=SiteCreateForm(request.POST or None, request.FILES or None) if form.is_valid(): site=Sites( name=form.cleaned_data['name'], url=form.cleaned_data['url'], client_cert=form.cleaned_data['client_cert'], client_key=form.cleaned_data['client_key'], ca_cert=form.cleaned_data['ca_cert'], ssl_verify=form.cleaned_data['ssl_verify'], ) site.save() messages.success(request,_('New site has been created!')) return redirect('sites-list') else: print(form.errors) else: form=SiteCreateForm() return render(request, "sites/site_create.html", { "title": title, "subtitle": subtitle, "form": form, })
class ContainerCreateForm(forms.Form): name = forms.CharField( label=_('Name'), required=True, ) image = forms.CharField(label=_('Image'), required=True) command = forms.CharField(label=_('Command'), required=True) network = forms.CharField(label=_('Network'), required=True)
def sites_list(request): title=_('List of Sites') subtitle=_('list all sites') breadcrumb=_('Sites') sites=Sites.objects.all() return render(request, "sites/sites_list.html", {"sites": sites, "title": title, "breadcrumb": breadcrumb, "subtitle": subtitle,})
def save_profile(backend, user, response, *args, **kwargs): if backend.name == 'github': logging.info(_('Github user {} is logged in'.format(user))) elif backend.name == 'google-oauth2': logging.info(_('Google user {} is logged in'.format(user))) elif backend.name == 'facebook': logging.info(_('Facebook user {} is logged in'.format(user)))
def volume_list(request): title = _("Manage Volume") subtitle = _("create,list and manage volumes") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect_api() volumes = client.volumes() return render(request, "volume/volume_list.html", {"volumes": volumes, "title": title, "subtitle": subtitle, })
def network_list(request): title = _("Manage Network") subtitle = _("create,list and manage networks") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect_api() networks = client.networks() networks = sorted(networks) return render(request, "network/network_list.html", {"networks": networks, "title": title, "subtitle": subtitle,})
def image_list(request): title = _("Manage Images") subtitle = _("create,list and manage images") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() images = client.images.list(all=True) return render(request, "image/image_list.html", {"images": images, "title": title, "subtitle": subtitle,})
def image_list(request): title = _("Manage Images") subtitle = _("create,list and manage images") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() images = client.images.list(all=True) return render(request, "image/image_list.html", { "images": images, "title": title, "subtitle": subtitle, })
def volume_list(request): title = _("Manage Volume") subtitle = _("create,list and manage volumes") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect_api() volumes = client.volumes() return render(request, "volume/volume_list.html", { "volumes": volumes, "title": title, "subtitle": subtitle, })
def network_list(request): title = _("Manage Network") subtitle = _("create,list and manage networks") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect_api() networks = client.networks() networks = sorted(networks) return render(request, "network/network_list.html", { "networks": networks, "title": title, "subtitle": subtitle, })
def container_details(request,container_id): title = _("Container Details") subtitle = _("details of the container") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() container = client.containers.get(container_id) return render(request, "container/container_details.html", {"title": title, "subtitle": subtitle, "container": container })
def clean_email(self): email = self.cleaned_data['email'] try: User.objects.get(email=email) except ObjectDoesNotExist: return email raise forms.ValidationError(_('Email address is not available.'))
def container_list(request): title = _("Manage Container") subtitle = _("create,list and manage containers") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() containers = client.containers.list(all=True, filters={"label": ["domain_id=%s" % user_profile.selected_domain], "label": ["project_id=%s" % user_profile.selected_project], }) current_date = datetime.datetime.now() return render(request, "container/container_list.html", {"containers": containers, "title": title, "subtitle": subtitle, "current_date": current_date, })
def clean_password2(self): if 'password1' in self.cleaned_data: password1 = self.cleaned_data['password1'] password2 = self.cleaned_data['password2'] if password1 == password2: return password2 raise forms.ValidationError(_('Password do not match.'))
def clean_project(self): project = self.cleaned_data['project'] try: Project.objects.get(name=project) Domain.objects.get(name=project) except ObjectDoesNotExist: return project raise forms.ValidationError(_('Project name is not available.'))
def container_stop(request, container_id): user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() container = client.containers.get(container_id) container.stop() messages.success(request, _('Container "%s" stopped!' % container.name)) return redirect(request.META['HTTP_REFERER'])
class SiteCreateForm(forms.Form): name = forms.CharField(label=_('Name'), max_length=64, required=True) url = forms.CharField(label=_('URL'), max_length=512, required=True) client_cert = forms.FileField(label=_('Client Certificate'), required=True) client_key = forms.FileField(label=_('Client Key'), required=True) ca_cert = forms.FileField(label=_('CA Certificate'), required=True) ssl_verify = forms.BooleanField(label=_('Verify SSL'), required=False)
def domain_select(request, domain_id, user_id): user = Profile.objects.get(user_id=user_id) user.selected_domain = domain_id assigned_projects = Assignment.objects.filter( type=1, actor=user_id, target_domain_id=user.selected_domain) assigned_project = assigned_projects.first() # try to assign first project as selected project. # if there is no project for domain, raise exception and assign none to selected_project if assigned_project: user.selected_project = assigned_project.target_project_id else: user.selected_project = None logging.warning(_('Domain does not have any project!')) messages.warning(request, 'Domain does not have any project!') user.save() return redirect(request.META['HTTP_REFERER'])
def domain_select(request,domain_id, user_id): user = Profile.objects.get(user_id=user_id) user.selected_domain = domain_id assigned_projects = Assignment.objects.filter(type=1, actor=user_id, target_domain_id=user.selected_domain) assigned_project = assigned_projects.first() # try to assign first project as selected project. # if there is no project for domain, raise exception and assign none to selected_project if assigned_project: user.selected_project = assigned_project.target_project_id else: user.selected_project = None logging.warning(_('Domain does not have any project!')) messages.warning(request, 'Domain does not have any project!') user.save() return redirect(request.META['HTTP_REFERER'])
def wrap(request, *args, **kwargs): user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() client_api = conn.docker_connect_api() container = client.containers.get(kwargs['container_id']) site_id = int(container.attrs['Config']['Labels']['site_id']) domain_id = int(container.attrs['Config']['Labels']['domain_id']) project_id = int(container.attrs['Config']['Labels']['project_id']) if (user_profile.selected_site == site_id and user_profile.selected_domain == domain_id and user_profile.selected_project == project_id): return function(request, *args, **kwargs) else: logging.error(request, 'You do not have permission for this action!') messages.error(request, _('You do not have permission for this action!')) return redirect('container-list')
def wrap(request, *args, **kwargs): user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() client_api = conn.docker_connect_api() container = client.containers.get(kwargs['container_id']) site_id = int(container.attrs['Config']['Labels']['site_id']) domain_id = int(container.attrs['Config']['Labels']['domain_id']) project_id = int(container.attrs['Config']['Labels']['project_id']) if (user_profile.selected_site == site_id and user_profile.selected_domain == domain_id and user_profile.selected_project == project_id): return function(request, *args, **kwargs) else: logging.error(request, 'You do not have permission for this action!') messages.error(request, _('You do not have permission for this action!')) return redirect('container-list')
class UserRegisterForm(forms.Form): email = forms.EmailField(label=_('Email')) password1 = forms.CharField( label=_('Password'), widget=forms.PasswordInput(), ) password2 = forms.CharField( label=_('Password (Again)'), widget=forms.PasswordInput(), ) project = forms.CharField( label=_('Project Name'), required=True, error_messages={'required': _('Please enter your project name!')} ) tos = forms.BooleanField( required=True, error_messages={'required': _('You must accept TOS.')} ) def clean_email(self): email = self.cleaned_data['email'] try: User.objects.get(email=email) except ObjectDoesNotExist: return email raise forms.ValidationError(_('Email address is not available.')) def clean_password2(self): if 'password1' in self.cleaned_data: password1 = self.cleaned_data['password1'] password2 = self.cleaned_data['password2'] if password1 == password2: return password2 raise forms.ValidationError(_('Password do not match.')) def clean_project(self): project = self.cleaned_data['project'] try: Project.objects.get(name=project) Domain.objects.get(name=project) except ObjectDoesNotExist: return project raise forms.ValidationError(_('Project name is not available.'))
def create_project(request): if request.method == 'POST': form = ProjectForm(request.POST) if form.is_valid(): u = request.user salt = hashlib.sha1(str(random.random())).hexdigest()[:5] usernamesalt = request.user.email profile = Profile.objects.get(user_id=u.id) profile.activation_key = hashlib.sha1(salt + usernamesalt).hexdigest() profile.key_expires = timezone.now() + timedelta(days=2) domain = Domain.objects.create( name=form.cleaned_data['project'], ) # create project for new user project = Project.objects.create( name=form.cleaned_data['project'], description='Project for %s ' % (u.username), domain=domain ) # get role r = Role.objects.get(name="user") # assign new user as domain user assign_domain = Assignment.objects.create( type=3, actor=u, target_domain=domain, target_project=project, role=r ) # assign new user as project user assign_project = Assignment.objects.create( type=1, actor=u, target_domain=domain, target_project=project, role=r, ) # get first site site = Sites.objects.first() # get domain assignment domain = Assignment.objects.filter(actor=u.id) project = Assignment.objects.filter(actor=u.id) # get domain assignment domain = Assignment.objects.filter(actor=u.id) project = Assignment.objects.filter(actor=u.id) # if there is a site if site: # save site id to profile profile.selected_site = site.id profile.selected_domain = domain[0].target_domain_id profile.selected_project = project[0].target_project_id profile.save() # create domain for new user site_url = settings.SITE_ROOT_URL send_email({'u': u, 'profile': profile, 'site_url': site_url}, _('Welcome to our cloud'), u.email, settings.DEFAULT_EMAIL_FROM, ) return render(request, 'authcp/success.html', {'u': u, 'profile': profile}) else: print(form.errors) else: form = ProjectForm() title = _("Create Project") return render(request, 'project/create.html', {'form': form, 'title': title})
def container_create(request): title = _("Create Container") subtitle = _("create a new container") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() client_api = conn.docker_connect_api() images = client.images.list(all=True) networks = sorted(client_api.networks()) if request.method == "POST": form = ContainerCreateForm(request.POST) if form.is_valid(): title = _("Create Container") subtitle = _("create a new container") user_profile = request.user.profile conn = ConnectDocker(user_profile.selected_site) client = conn.docker_connect() client_api = conn.docker_connect_api() images = client.images.list(all=True) networks = sorted(client_api.networks()) # define variables for container image = form.cleaned_data['image'] command = form.cleaned_data['command'] labels = { "site_id": str(user_profile.selected_site), "domain_id": str(user_profile.selected_domain), "project_id": str(user_profile.selected_project), "user_id": str(request.user.id) } name = form.cleaned_data['name'] networking_config = client_api.create_networking_config({ form.cleaned_data['network']: client_api.create_endpoint_config() }) try: # create the container container = client_api.create_container(image, command, detach=True, labels=labels, name=name, networking_config=networking_config, ) # starts to container client_api.start(container) messages.success(request, _('Container "%s" created and started successfully' % name)) return redirect('container-list') except APIError as error: logging.error(request, error) messages.error(request, error.explanation) except ImageNotFound as error: logging.error(request, error) messages.error(request, error.explanation) return render(request, "container/container_create.html", {"title": title, "subtitle": subtitle, "form": form, "images": images, "networks": networks, }) else: print(form.errors) else: form = ContainerCreateForm() return render(request, "container/container_create.html", {"title": title, "subtitle": subtitle, "form": form, "images": images, "networks": networks, })
def site_delete(request,pk): site=get_object_or_404(Sites,pk=pk) site.delete() messages.success(request, _('Site deleted!')) return redirect("sites-list")