Ejemplo n.º 1
0
def initial_setup():
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = generate_password_hash(
            form.password.data).decode("utf-8")
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        user.save()
        flash(
            f"Account created for {form.username.data} -  Bands Added! You can now log in.",
            "success")
        uid = user.id
        if not Towns.objects():
            setup_towns()
        resource_path = os.path.join(app.root_path, 'setup')
        with open(os.path.join(resource_path, 'band.json')) as f:
            file_data = json.load(f)
        bandz = []
        for band in file_data:
            if 'date_created' in band.keys():
                date_created = datetime.strptime(band.pop('date_created'),
                                                 "%Y-%m-%dT%H:%M:%S")
            else:
                date_created = datetime.utcnow
            bandz.append(
                Band(**band, created_by=uid, date_created=date_created))
        bands = Band.objects.insert(bandz)
        return redirect(url_for("user.login"))
    if len(list(Band.objects())) == 0:
        return render_template("setup.html", form=form, fullpage=True)
    else:
        flash(f"Initial Setup Complete", "success")
        return redirect(url_for("public.home"))
Ejemplo n.º 2
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("manage.mhome"))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = generate_password_hash(
            form.password.data).decode("utf-8")
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        user.save()
        flash(
            f"Account created for superuser {form.username.data}! Please restart server.",
            "success")
        return redirect(url_for("user.restart"))
    return render_template("register.html",
                           title="Register",
                           form=form,
                           display_breadcrumbs=True)
def mhome():
    page = request.args.get("page", 1, type=int)
    user = User.objects(id=current_user.id).first()
    bands = Band.objects(created_by=user).order_by(
        '-date_updated', '-date_created').paginate(per_page=PER_PAGE,
                                                   page=page)
    alphabet = {key: 0 for key in ALPHABET}
    return render_template("manage_bands.html",
                           bands=bands,
                           display_breadcrumbs=True,
                           alphabet=alphabet)
Ejemplo n.º 4
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for("user.account"))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.objects(email=form.email.data).first()
        if user and check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get("next")
            return redirect(next_page) if next_page else redirect(
                url_for("manage.mhome"))
        else:
            flash(f"Login Unsuccessful. Please check email and password",
                  "danger")
    return render_template("login.html", form=form, display_breadcrumbs=True)
 def validate_email(self, email):
     if email.data != current_user.email: 
         user = User.objects(email=email.data).first()
         if user:
             raise ValidationError('That email is already assocaited with a user. Please reset your password.')
 def validate_username(self, username): # template for validation error
     if username.data != current_user.username:
         user = User.objects(username=username.data).first()
         if user:
             raise ValidationError('That username is already taken. Please choose somthing else.')
def update_band_profile(bname, letter, band_view):
    # flask-login uses a proxy that doesn't play nice with mongoengine
    # so current_user must be cast as user
    user = User.objects(id=current_user.id).first()
    band = Band.objects(band_name=bname).first()
    if band.created_by.id != current_user.id:
        abort(403)
    form = CreateUpdateBandForm()
    if form.validate_on_submit():
        if form.media_assets.featured_image.data:
            picture_file = save_picture(form.media_assets.featured_image.data,
                                        band=True)
            band.media_assets.featured_image = picture_file
        if form.media_assets.featured_video.data:
            band.media_assets.featured_video = get_video_service_and_id(
                form.media_assets.featured_video.data)
        contact = Contact()
        contact.contact_name = form.contact_details.contact_name.data
        contact.contact_title = form.contact_details.contact_title.data
        contact.contact_generic_title = form.contact_details.contact_generic_title.data
        phone = phonenumbers.parse(
            form.contact_details.contact_numbers.phone.data,
            form.contact_details.contact_numbers.region.data)
        new_phone = Phone(mobile=bool(
            form.contact_details.contact_numbers.mobile.data),
                          number=phonenumbers.format_number(
                              phone, phonenumbers.PhoneNumberFormat.E164))
        contact.contact_numbers = new_phone
        new_email = Email(
            email_title=form.contact_details.contact_emails.email_title.data,
            email_address=form.contact_details.contact_emails.email_address.
            data)
        contact.contact_emails = new_email
        weblinks = Links()
        weblinks.enquiries = form.enquiries_url.data
        band.solo = bool(form.solo.data)
        band.band_name = form.band_name.data
        band.catalogue_name = de_article(form.band_name.data)
        band.date_updated = datetime.utcnow
        band.description = form.description.data
        band.genres.clear()
        returned_genrelist = [
            genre.strip().replace(' ', '-').lower()
            for gl in request.form.getlist('genres') for genre in gl.split(',')
        ]
        returned_genrelist.extend(genre.strip().replace(' ', '-').lower()
                                  for genre in form.genres_other.data.split(
                                      ',')) if form.genres_other.data else None
        band.genres = list(filter(None, set(returned_genrelist)))
        band.hometown = {
            "town": form.hometown.origin_town.data,
            "county": form.hometown.origin_county.data
        }
        band.profile = form.profile.data
        band.strapline = form.strapline.data
        band.contact_details = contact
        band.links = weblinks
        band.band_members.clear()
        band.band_members = [
            BandMember(**member) for member in form.members.data
        ]
        band.save()
        flash("Band details updated!", "success")
        return redirect(url_for('manage.manage_bands_home', toggle='recent'))
    elif request.method == "GET":
        form.solo.data = band.solo
        form.band_name.data = band.band_name
        form.description.data = band.description
        form.strapline.data = band.strapline
        form.profile.data = band.profile
        form.contact_details.contact_name.data = band.contact_details.contact_name
        form.contact_details.contact_title.data = band.contact_details.contact_title
        form.contact_details.contact_generic_title.data = band.contact_details.contact_generic_title
        form.contact_details.contact_generic_title.data = band.contact_details.contact_generic_title
        form.contact_details.contact_numbers.mobile.data = band.contact_details.contact_numbers.mobile
        form.contact_details.contact_numbers.region.data = "None"
        form.contact_details.contact_numbers.phone.data = band.contact_details.contact_numbers.number
        form.contact_details.contact_emails.email_title.data = band.contact_details.contact_emails.email_title
        form.contact_details.contact_emails.email_address.data = band.contact_details.contact_emails.email_address
        form.media_assets.featured_video.data = (
            ("https://www.youtube.com/watch?v="
             if band.media_assets.featured_video["service"] == "youtube" else
             "https://www.vimeo.com/") +
            band.media_assets.featured_video["vid"]
        ) if band.media_assets.featured_video else None
        form.genres.data = ",".join(map(str, band.genres))
    selected_county = band.hometown["county"] if band.hometown[
        "county"] is not None else "Antrim"
    selected_town = band.hometown["town"] if band.hometown[
        "town"] is not None else "none"
    genres = list_genres()
    form_legend = "Edit Band Profile"
    return render_template("manage_band_update_form.html",
                           form=form,
                           genrelist=genres,
                           form_legend="Edit Band Profile",
                           formpage=True,
                           selected_county=selected_county,
                           selected_town=selected_town,
                           band=band,
                           display_breadcrumbs=True)
def add_band(stage):
    genres = list_genres()
    if stage == 1:
        form = CreateBandForm1()
        if request.method == "POST" and form.validate_on_submit():
            if form.band_name.validate(form):
                catalogue_name = de_article(form.band_name.data) if int(
                    form.solo.data) == 0 else de_singularise(
                        form.band_name.data)
            session["band"] = {
                "band_name": form.band_name.data,
                "catalogue_name": catalogue_name,
                "hometown": {
                    "town": form.hometown.origin_town.data,
                    "county": form.hometown.origin_county.data
                },
                "solo": form.solo.data
            }
            form = CreateBandForm2()
            stage = 2
            return render_template("manage_new_stage2_band_details.html",
                                   form=form,
                                   formpage=True,
                                   display_breadcrumbs=True,
                                   stage=stage,
                                   genrelist=genres,
                                   bname=session["band"]["band_name"])
        else:
            form = CreateBandForm1()
            return render_template('manage_new_stage1_band_check.html',
                                   form=form,
                                   formpage=True,
                                   display_breadcrumbs=True,
                                   stage=1,
                                   title="Band/Artist Name")

    if stage == 2:
        form = CreateBandForm2()
        if request.method == "POST" and form.validate_on_submit():
            returned_genrelist = [
                genre.strip().replace(' ', '-').lower()
                for gl in request.form.getlist('genres')
                for genre in gl.split(',')
            ]
            returned_genrelist.extend(
                genre.strip().replace(' ', '-').lower()
                for genre in form.genres_other.data.split(
                    ',')) if form.genres_other.data else None
            session["band"]["strapline"] = form.strapline.data
            session["band"]["description"] = form.description.data
            session["band"]["genres"] = list(
                filter(None, set(returned_genrelist)))
            session.modified = True
            form = CreateBandForm3()
            stage = 3
            return render_template("manage_new_stage3_band_profile.html",
                                   form=form,
                                   formpage=True,
                                   display_breadcrumbs=True,
                                   stage=stage,
                                   bname=session["band"]["band_name"])
        else:
            stage = 2
            return render_template("manage_new_stage2_band_details.html",
                                   form=form,
                                   formpage=True,
                                   display_breadcrumbs=True,
                                   stage=stage,
                                   genrelist=genres,
                                   bname=session["band"]["band_name"])

    if stage == 3:
        form = CreateBandForm3()
        if request.method == "POST" and form.validate_on_submit():
            if form.featured_image.data:
                picture_file = save_picture(form.featured_image.data,
                                            band=True)
                featured_image = picture_file if picture_file else 'defaultband.jpg'
            else:
                featured_image = "defaultband.jpg"
            session["band"]["featured_image"] = featured_image
            session["band"]["profile"] = form.profile.data
            session["band"]["members"] = [{
                **member
            } for member in form.members.data]
            session.modified = True
            form = CreateBandForm4()
            stage = 4
            return render_template("manage_new_stage4_band_contact.html",
                                   form=form,
                                   formpage=True,
                                   display_breadcrumbs=True,
                                   stage=stage,
                                   bname=session["band"]["band_name"])
        else:
            stage = 3
            return render_template("manage_new_stage3_band_profile.html",
                                   form=form,
                                   formpage=True,
                                   display_breadcrumbs=True,
                                   stage=stage,
                                   bname=session["band"]["band_name"])

    if stage == 4:
        form = CreateBandForm4()
        if request.method == "POST" and form.validate_on_submit():
            user = User.objects(id=current_user.id).first()
            contact = Contact()
            contact.contact_name = form.contact_details.contact_name.data
            if len(form.contact_details.contact_title.data.rstrip()) == 0:
                contact.contact_title = "Enquiries"
            else:
                contact.contact_title = form.contact_details.contact_title.data
            if len(form.contact_details.contact_generic_title.data.rstrip()
                   ) == 0:
                contact.contact_generic_title = "Enquiries"
            else:
                contact.contact_generic_title = form.contact_details.contact_generic_title.data
            _phone = phonenumbers.parse(
                form.contact_details.contact_numbers.phone.data,
                form.contact_details.contact_numbers.region.data)
            contact.contact_numbers = Phone(
                mobile=bool(form.contact_details.contact_numbers.mobile.data),
                number=phonenumbers.format_number(
                    _phone, phonenumbers.PhoneNumberFormat.E164))
            contact.contact_emails = Email(email_title=form.contact_details.
                                           contact_emails.email_title.data,
                                           email_address=form.contact_details.
                                           contact_emails.email_address.data)
            weblinks = Links(enquiries=form.enquiries_url.data)
            assets = Assets(featured_image=session["band"]["featured_image"])
            if form.featured_video.data:
                assets["featured_video"] = get_video_service_and_id(
                    form.featured_video.data)

            band = Band(band_name=session["band"]["band_name"],
                        catalogue_name=session["band"]["catalogue_name"],
                        genres=session["band"]["genres"],
                        hometown=session["band"]["hometown"],
                        description=session["band"]["description"],
                        strapline=session["band"]["strapline"],
                        profile=session["band"]["profile"],
                        band_members=[
                            BandMember(**member)
                            for member in session["band"]["members"]
                        ],
                        media_assets=assets,
                        contact_details=contact,
                        links=weblinks,
                        created_by=user,
                        solo=bool(session["band"]["solo"]))
            band.save()
            band_id = band.id
            # user.update(push__posts=post) if associating band to user
            band = Band.objects(id=band_id).first()
            return redirect(
                url_for('manage.preview_band',
                        band_view='alpha',
                        letter=band.catalogue_name[0],
                        bname=band.band_name))
        else:
            stage = 4
            return render_template("manage_new_stage4_band_contact.html",
                                   form=form,
                                   stage=stage,
                                   bname=session["band"]["band_name"],
                                   display_breadcrumbs=True)
def manage_bands_home(band_view='recent', letter='a'):
    letter = '_' if letter == '1' else letter
    user = User.objects(id=current_user.id).first()
    page = request.args.get("page", 1, type=int)
    if band_view == 'recent':
        bands = Band.objects(created_by=user).order_by(
            '-date_updated', '-date_created').paginate(per_page=PER_PAGE,
                                                       page=page)
        alphabet = {key: 0 for key in ALPHABET}
        return render_template("manage_bands.html",
                               bands=bands,
                               display_breadcrumbs=True,
                               alphabet=alphabet)
    else:
        pipeline = [{
            "$match": {
                "created_by": user.id
            }
        }, {
            "$facet": {
                "numbers_by_letter": [{
                    "$group": {
                        "_id": {
                            "$substr": ["$catalogue_name", 0, 1]
                        },
                        "number_of_bands": {
                            "$sum": 1
                        }
                    }
                }],
                "bands_by_letter": [{
                    "$match": {
                        "catalogue_name": {
                            "$regex": f"^{str(letter.upper())}"
                        }
                    }
                }, {
                    "$project": {
                        "_id": 1,
                        "band_name": 1,
                        "catalogue_name": 1,
                        "strapline": 1,
                        "hometown": 1,
                        "profile": 1,
                        "description": 1,
                        "genres": 1,
                        "media_assets": 1,
                        "letter": {
                            "$toLower": {
                                "$substr": ["$catalogue_name", 0, 1]
                            }
                        }
                    }
                }, {
                    "$sort": {
                        "catalogue_name": 1
                    }
                }]
            }
        }]
        result = list(Band.objects.aggregate(pipeline))[0]
        bands_by_letter = Pagination(result["bands_by_letter"], int(page), 12)
        _alphabet = 'abcdefghijklmnopqrstuvwxyz_'
        alphabet = {
            obj["_id"].lower(): int(obj["number_of_bands"])
            for obj in result["numbers_by_letter"]
        }
        alphabet = {key: alphabet.setdefault(key, 0) for key in _alphabet}
        alphabet['1'] = alphabet.pop(
            '_')  #swap out for letter links 1 == '#' === '_'
        count = bands_by_letter.pages
        if bands_by_letter.total > 0:
            bands_total = bands_by_letter.total
            #bands = Band.objects(created_by=user).order_by('-catalogue_name').paginate(per_page=PER_PAGE, page=page)
            return render_template("manage_bands.html",
                                   bands=bands_by_letter,
                                   bands_total=bands_total,
                                   count=count,
                                   display_breadcrumbs=True,
                                   alphabet=alphabet)
        else:
            return 'no bands created'  #redirect