Beispiel #1
0
 def update_password(self, old_pass, new_pass):
     from server import app, mail
     session = SessionManager.Session()
     try:
         user = session.query(User).filter(User.id == self.id).one()
         if check_password_hash(user.password, old_pass):
             user.password = UserCredential.get_pass_hash(new_pass)
             session.commit()
             if user.email is not None and user.email_confirmed:
                 # send notification mail
                 subject = '[{0}] Password Update Notification'.format(
                     app.config['SITE_NAME'])
                 email_content = render_template(
                     'update-pass-notification.html',
                     info={
                         'title': subject,
                         'user_name': user.name,
                         'site_name': app.config['SITE_NAME']
                     })
                 msg = Message(subject,
                               recipients=[self.email],
                               html=email_content)
                 try:
                     mail.send(msg)
                 except SMTPAuthenticationError:
                     raise ServerError('SMTP authentication failed', 500)
             return True
         else:
             raise ClientError(ClientError.PASSWORD_INCORRECT)
     except NoResultFound:
         raise ServerError('user not found')
     finally:
         SessionManager.Session.remove()
Beispiel #2
0
 def register_user(name, password, email, invite_code):
     email_patteren = re.compile(
         "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
     if not email_patteren.match(email):
         raise ClientError(ClientError.INVALID_EMAIL)
     session = SessionManager.Session()
     try:
         code = session.query(InviteCode).filter(
             InviteCode.code == invite_code).one()
         if code.used_by is not None:
             raise ClientError(ClientError.INVALID_INVITE_CODE)
         user = User(name=name,
                     password=UserCredential.get_pass_hash(password),
                     email=email,
                     level=0)
         session.add(user)
         session.commit()
         code.used_by = user.id
         session.commit()
         return True
     except NoResultFound:
         raise ClientError(ClientError.INVALID_INVITE_CODE)
     except DataError:
         raise ClientError(ClientError.INVALID_INVITE_CODE)
     except IntegrityError as error:
         if error.diag.column_name == 'name':
             raise ClientError(ClientError.DUPLICATE_NAME)
         elif error.diag.column_name == 'email':
             raise ClientError(ClientError.DUPLICATE_EMAIL)
     except ClientError as error:
         raise error
     except Exception as error:
         raise ServerError(error.message)
     finally:
         SessionManager.Session.remove()
Beispiel #3
0
 def register_user(name, password, invite_code):
     session = SessionManager.Session()
     try:
         code = session.query(InviteCode).filter(
             InviteCode.code == invite_code).one()
         if code.used_by is not None:
             raise ClientError(ClientError.INVALID_INVITE_CODE)
         user = User(name=name,
                     password=generate_password_hash(password),
                     level=0)
         session.add(user)
         session.commit()
         code.used_by = user.id
         session.commit()
         return True
     except NoResultFound:
         raise ClientError(ClientError.INVALID_INVITE_CODE)
     except DataError:
         raise ClientError(ClientError.INVALID_INVITE_CODE)
     except IntegrityError:
         raise ClientError(ClientError.DUPLICATE_NAME)
     except ClientError as error:
         raise error
     except Exception as error:
         raise ServerError(error.message)
     finally:
         SessionManager.Session.remove()
Beispiel #4
0
 def update_email(self, new_email):
     from server import app, mail
     session = SessionManager.Session()
     try:
         user = session.query(User).filter(User.id == self.id).one()
         if user.email is not None and user.email_confirmed:
             # send notification mail
             subject = '[{0}] Email Address Update Notification'.format(
                 app.config['SITE_NAME'])
             email_content = render_template(
                 'email-change-notification.html',
                 info={
                     'title': subject,
                     'user_name': user.name,
                     'site_name': app.config['SITE_NAME']
                 })
             msg = Message(subject,
                           recipients=[self.email],
                           html=email_content)
             try:
                 mail.send(msg)
             except SMTPAuthenticationError:
                 raise ServerError('SMTP authentication failed', 500)
         # update
         user.email = new_email
         user.email_confirmed = False
         self.email = new_email
         self.email_confirmed = False
         # send email
         self.send_confirm_email()
         session.commit()
         return json_resp({'message': 'ok'})
     except IntegrityError:
         raise ClientError('duplicate email')
     except NoResultFound:
         raise ServerError('user not found')
     finally:
         SessionManager.Session.remove()
Beispiel #5
0
 def update_password(self, old_pass, new_pass):
     try:
         session = SessionManager.Session()
         user = session.query(User).filter(User.id == self.id).one()
         if check_password_hash(user.password, old_pass):
             user.password = generate_password_hash(new_pass)
             session.commit()
             return True
         else:
             raise ClientError(ClientError.PASSWORD_INCORRECT)
     except NoResultFound:
         raise ServerError('user not found')
     except ClientError as error:
         raise error
     except Exception as error:
         raise error
     finally:
         SessionManager.Session.remove()
Beispiel #6
0
    def send_pass_reset_email(email):
        """
        Send a password reset email which includes a link to navigate user to a endpoint to reset his/her password.
        The link contains a token get from self.generate_reset_email_token method. end point has the responsibility 
        to verify the token.
        :param email: the user email from user input. this must be a confirmed email of a valid user.
        :return: 
        """
        from server import app, mail
        session = SessionManager.Session()
        try:
            user = session.query(User). \
                filter(User.email == email). \
                one()
            if not user.email_confirmed:
                raise ClientError(ClientError.EMAIL_NOT_CONFIRMED)

            # generate token
            token = UserCredential.generate_reset_email_token(user)

            reset_url = '{0}://{1}/reset-pass?token={2}'.format(
                app.config['SITE_PROTOCOL'], app.config['SITE_HOST'], token)
            subject = '[{0}] Password Request for {1}'.format(
                app.config['SITE_NAME'], user.name)
            reset_content = render_template('reset-pass.html',
                                            info={
                                                'reset_title':
                                                subject,
                                                'reset_url':
                                                reset_url,
                                                'site_name':
                                                app.config['SITE_NAME'],
                                                'user_name':
                                                user.name
                                            })
            msg = Message(subject, recipients=[email], html=reset_content)
            mail.send(msg)
            session.commit()
        except SMTPAuthenticationError:
            raise ServerError('SMTP authentication failed', 500)
        except NoResultFound:
            raise ClientError(ClientError.EMAIL_NOT_EXISTS, 400)
        finally:
            SessionManager.Session.remove()
Beispiel #7
0
 def login_user(cls, name, password):
     session = SessionManager.Session()
     try:
         user = session.query(User).filter(User.name == name).one()
         if check_password_hash(user.password, password):
             credential = cls(user)
             SessionManager.Session.remove()
             return credential
         else:
             raise ClientError(ClientError.LOGIN_FAIL)
     except NoResultFound:
         raise ClientError(ClientError.LOGIN_FAIL)
     except DataError:
         raise ClientError(ClientError.LOGIN_FAIL)
     except ClientError as error:
         raise error
     except Exception as error:
         raise ServerError(error.message)
     finally:
         SessionManager.Session.remove()
Beispiel #8
0
    def reset_pass(name, password, invite_code):
        session = SessionManager.Session()
        try:
            user = session.query(User).filter(User.name == name).one()
            code = session.query(InviteCode).filter(
                and_(InviteCode.code == invite_code,
                     InviteCode.used_by == user.id)).one()

            user.password = generate_password_hash(password)

            session.commit()
            return True
        except NoResultFound:
            raise ClientError(ClientError.INVALID_INVITE_CODE)
        except DataError:
            raise ClientError(ClientError.INVALID_INVITE_CODE)
        except ClientError as error:
            raise error
        except Exception as error:
            raise ServerError(error.message)
        finally:
            SessionManager.Session.remove()
Beispiel #9
0
 def __send_email_to_all(self, bangumi_url, episode_dict, video_file_id,
                         user_dict, admin_list, message):
     from server import mail
     for admin in admin_list:
         admin_name = admin.name
         admin_email = admin.email
         if admin_email is None:
             continue
         mail_content = render_template('feed-back-mail.html',
                                        bangumi_url=bangumi_url,
                                        admin_name=admin_name,
                                        episode=episode_dict,
                                        video_file_id=video_file_id,
                                        user=user_dict,
                                        message=message)
         msg = Message('用户反馈信息',
                       recipients=[admin_email],
                       html=mail_content)
         try:
             mail.send(msg)
         except SMTPAuthenticationError:
             raise ServerError('SMTP authentication failed', 500)
Beispiel #10
0
 def send_confirm_email(self):
     """
     Send an confirm email to user. contains a link to confirm the email
     confirm link is not provide by this app, a client must implement this endpoint to complete the confirmation.
     """
     from server import app, mail
     token = self.generate_confirm_email_token()
     confirm_url = '{0}://{1}/email-confirm?token={2}'.format(
         app.config['SITE_PROTOCOL'], app.config['SITE_HOST'], token)
     subject = '[{0}] Email Address Confirmation'.format(
         app.config['SITE_NAME'])
     email_content = render_template('email-confirm.html',
                                     info={
                                         'confirm_title': subject,
                                         'confirm_url': confirm_url,
                                         'site_name':
                                         app.config['SITE_NAME'],
                                         'user_name': self.name
                                     })
     msg = Message(subject, recipients=[self.email], html=email_content)
     try:
         mail.send(msg)
     except SMTPAuthenticationError:
         raise ServerError('SMTP authentication failed', 500)
Beispiel #11
0
    def add_bangumi(self, content, uid):
        try:
            bangumi_data = json.loads(content)

            bangumi = Bangumi(bgm_id=bangumi_data.get('bgm_id'),
                              name=bangumi_data.get('name'),
                              name_cn=bangumi_data.get('name_cn'),
                              type=bangumi_data.get('type'),
                              summary=bangumi_data.get('summary'),
                              eps=bangumi_data.get('eps'),
                              image=bangumi_data.get('image'),
                              air_date=bangumi_data.get('air_date'),
                              air_weekday=bangumi_data.get('air_weekday'),
                              status=self.__get_bangumi_status(
                                  bangumi_data.get('air_date')),
                              created_by_uid=uid,
                              maintained_by_uid=uid)

            # bangumi.dmhy = bangumi_data.get('dmhy')
            # bangumi.acg_rip = bangumi_data.get('acg_rip')
            # bangumi.libyk_so = bangumi_data.get('libyk_so')

            bangumi.eps_no_offset = bangumi_data.get('eps_no_offset')

            session = SessionManager.Session()

            session.add(bangumi)

            bangumi.episodes = []

            for eps_item in bangumi_data['episodes']:
                eps = Episode(bgm_eps_id=eps_item.get('bgm_eps_id'),
                              episode_no=eps_item.get('episode_no'),
                              name=eps_item.get('name'),
                              name_cn=eps_item.get('name_cn'),
                              duration=eps_item.get('duration'),
                              status=Episode.STATUS_NOT_DOWNLOADED)
                if eps_item.get('airdate') != '':
                    eps.airdate = eps_item.get('airdate')

                eps.bangumi = bangumi
                bangumi.episodes.append(eps)

            session.commit()

            bangumi_id = str(bangumi.id)
            try:
                (cover_file_path,
                 cover_path) = self.__save_bangumi_cover(bangumi)
                # get dominant color
                bangumi.cover_color = get_dominant_color(cover_file_path)
                (width, height) = get_dimension(cover_file_path)
                bangumi.cover_image = Image(file_path=cover_path,
                                            dominant_color=bangumi.cover_color,
                                            width=width,
                                            height=height)
                session.commit()
            except Exception as error:
                sentry_wrapper.sentry_middleware.captureException()
                logger.warn(error)
                # delete bangumi for download error
                session.delete(bangumi)
                session.commit()
                raise ServerError('Fail to Download Image')

            return json_resp({'data': {'id': bangumi_id}})
        finally:
            SessionManager.Session.remove()