Skip to content
This repository

Client ID field and functionality for Cloud Sites Clients' DNS #10

Merged
merged 3 commits into from over 1 year ago

2 participants

Tommy McNeely Major Hayden
Tommy McNeely
TJM commented

I re-factored my previous attempt. This time I decided to use a session variable, and if you looked at the previous one, I am sure you will agree this is a much cleaner option.

This change creates a field in layout (nav header) to specify a clientId (from Cloud Sites client list), in order to edit domains associated with Cloud Sites customers.

Major Hayden major merged commit 79dd862 into from
Major Hayden major closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
99  clouddns-gui.py
@@ -16,7 +16,7 @@
16 16
 #
17 17
 from clouddns import connection
18 18
 import clouddns.consts
19  
-from flask import Flask, render_template, g, request, flash, redirect
  19
+from flask import Flask, render_template, g, session, request, flash, redirect
20 20
 import json
21 21
 import re
22 22
 
@@ -53,6 +53,9 @@ def connect_clouddns():
53 53
 def index(domainname=None):
54 54
     """All of the HTML for the entire app flows through here"""
55 55
 
  56
+    # Set AccountID (from session)
  57
+    setAccount()
  58
+
56 59
     # Pick up a list of domains from the API
57 60
     domainlist = g.raxdns.get_domains()
58 61
 
@@ -78,9 +81,15 @@ def index(domainname=None):
78 81
 def add_domain():
79 82
     """Handles adding domains"""
80 83
 
  84
+    # Set AccountID (from session)
  85
+    setAccount()
  86
+    
81 87
     # Find out the name of the domain we're adding
82 88
     domain = request.form['domain']
83 89
 
  90
+    # Set AccountID (from session)
  91
+    setAccount()
  92
+
84 93
     # Issue a domain creation request to the API and flash a message
85 94
     g.raxdns.create_domain(
86 95
         name=request.form['domain'],
@@ -94,6 +103,9 @@ def add_domain():
94 103
 def duplicate_domain():
95 104
     """Adds a new domain and adds records from an existing domain"""
96 105
 
  106
+    # Set AccountID (from session)
  107
+    setAccount()
  108
+
97 109
     # Dig up the old domain and records
98 110
     olddomain = g.raxdns.get_domain(name=request.form['olddomain'])
99 111
     oldrecords = olddomain.get_records()
@@ -145,6 +157,9 @@ def duplicate_domain():
145 157
 def add_domain_bind():
146 158
     """Handles adding domains via a BIND zone file"""
147 159
 
  160
+    # Set AccountID (from session)
  161
+    setAccount()
  162
+
148 163
     # Get the BIND zone file from the user
149 164
     zone_file = request.form['zone_file']
150 165
 
@@ -159,6 +174,9 @@ def add_domain_bind():
159 174
 def delete_domain():
160 175
     """Handles deleting domains"""
161 176
 
  177
+    # Set AccountID (from session)
  178
+    setAccount()
  179
+
162 180
     # Pick up the form fields
163 181
     confirmation = request.form['confirmation']
164 182
     domain_name = request.form['domain']
@@ -183,6 +201,9 @@ def delete_domain():
183 201
 def adjust_ttl(domainname=None):
184 202
     """Changes TTL values on all records"""
185 203
 
  204
+    # Set AccountID (from session)
  205
+    setAccount()
  206
+
186 207
     # Get the domain from the API
187 208
     domain = g.raxdns.get_domain(name=domainname)
188 209
 
@@ -203,6 +224,9 @@ def adjust_ttl(domainname=None):
203 224
 def domain_comment(domainname=None):
204 225
     """Edits the comment on a domain"""
205 226
 
  227
+    # Set AccountID (from session)
  228
+    setAccount()
  229
+
206 230
     # Get the domain from the API
207 231
     domain = g.raxdns.get_domain(name=domainname)
208 232
 
@@ -216,6 +240,9 @@ def domain_comment(domainname=None):
216 240
 def add_record(domainname=None):
217 241
     """Handles adding records"""
218 242
 
  243
+    # Set AccountID (from session)
  244
+    setAccount()
  245
+
219 246
     # Get the domain from the API
220 247
     domain = g.raxdns.get_domain(name=domainname)
221 248
 
@@ -257,6 +284,9 @@ def add_record(domainname=None):
257 284
 def update_record(domainname=None, recordid=None):
258 285
     """Handles record updates"""
259 286
 
  287
+    # Set AccountID (from session)
  288
+    setAccount()
  289
+
260 290
     # Get the domain and record from the API
261 291
     domain = g.raxdns.get_domain(name=domainname)
262 292
     record = domain.get_record(id=recordid)
@@ -278,6 +308,9 @@ def update_record(domainname=None, recordid=None):
278 308
 def delete_record(domainname=None, recordid=None):
279 309
     """Handles record deletions"""
280 310
 
  311
+    # Set AccountID (from session)
  312
+    setAccount()
  313
+
281 314
     # Get the domain and delete the record
282 315
     domain = g.raxdns.get_domain(name=domainname)
283 316
     domain.delete_record(recordid)
@@ -287,7 +320,69 @@ def delete_record(domainname=None, recordid=None):
287 320
 
288 321
     return redirect("/domains/%s" % domainname)
289 322
 
  323
+@app.route("/account", methods=['GET','POST'])
  324
+def change_accountId():
  325
+    """Handles setting the accountId from the Nav Bar"""
  326
+
  327
+    # Handle a blank GET request to reset the accountId
  328
+    if request.method == 'GET':
  329
+        session.pop('accountId', None) # Remove the accountId from the session
  330
+        return redirect("/domains")
  331
+        
  332
+    accountId = request.form['accountId']
  333
+
  334
+    ### TODO: VALIDATE!! (numeric, length, etc?)
  335
+
  336
+    if accountId is None or accountId == "" or accountId == "default":
  337
+        session.pop('accountId', None) # Remove the accountId from the session
  338
+    else:
  339
+        session['accountId'] = accountId # Update the accountId in the session
  340
+    return redirect("/domains")
  341
+
  342
+
  343
+# No Application route, this is an internal function
  344
+def getAccount():
  345
+    """Internal Function to get the accountId (wrapper to python-clouddns function)"""
  346
+
  347
+    ## Try the proper method, but fallback to a local implementation
  348
+    try:
  349
+        accountId = g.raxdns.get_accountId()
  350
+    except AttributeError:
  351
+        ## work around for missing get_accountId()
  352
+        (baseUri, sep , accountId) = g.raxdns.uri.rstrip('/').rpartition('/')
  353
+        #app.logger.debug('Local Implementation get_account: %s' % accountId)
  354
+    return accountId
  355
+
  356
+
  357
+# No Application route, this is an internal function
  358
+def setAccount():
  359
+    """Internal Function to set the accountId in g.raxdns (wrapper to python-clouddns function)"""
  360
+
  361
+    # Figure out the accountId
  362
+    if 'accountId' in session:
  363
+        accountId = session['accountId']
  364
+    else:
  365
+        accountId = getAccount()
  366
+        session['accountId'] = accountId
  367
+
  368
+    # Set g.accountId (to pass it to the GUI)
  369
+    g.accountId = accountId
  370
+
  371
+    # Set the accountId in the raxdns object
  372
+    ## Try the proper method, but fallback to a local implementation
  373
+    try:
  374
+        g.raxdns.set_account(accountId)
  375
+    except AttributeError:
  376
+        # This works around not having the method by implementing it here, but
  377
+        # I do not think that it is "proper" to be tweaking object attributes from
  378
+        # outside the object
  379
+        (baseUri, sep , oldAccountId) = g.raxdns.uri.rstrip('/').rpartition('/')
  380
+        g.raxdns.uri = baseUri + '/' + accountId
  381
+        #app.logger.debug('Local Implementation set_account(%s): %s' % (accountId, g.raxdns.uri))
  382
+    return
  383
+
  384
+
290 385
 if __name__ == "__main__":
291 386
     # Only for running this app via python directly.  This is ignored if you
292 387
     # run it through mod_wsgi.
293  
-    app.run(host='0.0.0.0')
  388
+    app.run(host='127.0.0.1')
9  templates/layout.html
@@ -33,6 +33,13 @@
33 33
           </a>
34 34
           <a class="brand" href="/">Rackspace Cloud DNS GUI</a>
35 35
           <div class="nav">
  36
+            <li>
  37
+              <form method="post" action="/account" class="navbar-search">
  38
+                Client ID:
  39
+                <input class="search-query span1" name="accountId" type="text" placeholder="{{g.accountId}}">
  40
+                <button class="btn" type="submit" style="display: none;">Set Account</button>
  41
+              </form>
  42
+            </li>
36 43
             <li class="dropdown" id="menu1">
37 44
               <a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">
38 45
                 Active Domains
@@ -84,4 +91,4 @@
84 91
       </footer>
85 92
     </div>
86 93
   </body>
87  
-</html>
  94
+</html>
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.